I wish to define a type String such that declaring a variable of type String is at compile-time equivalent to char[N]. I have seen examples of template functions of certain array dimension but none to work on actual concrete types themselves.
namespace myns {
typedef uint32_t LongCount;
typedef uint16_t ShortCount;
typedef uint32_t LongDist;
typedef uint16_t ShortDist;
#define LONG_NAME 20;
#define SHORT_NAME 10;
// this would be better in my opinion:
typedef String<20> LongName;
typedef String<10> ShortName;
template<typename DIST1, typename DIST2, typename COUNT1>
struct SomeStruct1Base
{
DIST1 dist1;
DIST2 dist2;
COUNT1 c;
};
typedef SomeStruct1Base<LongDist, LongDist, LongCount> SomeStruct1Long;
typedef SomeStruct1Base<ShortDist, ShortDist, ShortCount> SomeStruct1Short;
template<typename DIST1, typename COUNT1, int NAME_LEN>
struct SomeStruct2Base
{
DIST1 dist1;
uint32_t distx;
COUNT1 c;
char name[NAME_LEN];
};
typedef SomeStruct2Base<LongDist, LongCount, SHORT_NAME> SomeStruct2Long;
typedef SomeStruct2Base<LongDist, LongCount, SHORT_NAME> SomeStruct2Short;
typedef SomeStruct2Base<LongDist, LongCount, LONG_NAME> SomeStruct2Ext;
The above is not very consistent and I think it would be better to pass in String<6> or whatever instead of passing in an int parameter to the template. No?
Update: I am using this structure to parse network data represented in char buffers so the struct needs to be able to do reinterpret_cast<>able. Sorry I didn't mention this earlier but I'm mentioning it now as I see some of the suggestions will break under the circumstance.
std::array, also known as C++03tr1::arrayandboost::array, for the data structure, and useenumto give names to template numeric arguments.