Is it possible using macro magic or TMP to insert the length into a string at compile time?
For example:
const wchar_t* myString = L"Hello";
I would want the buffer to actually contain "[length] [string constant]".
I'm using MSVC 2010 which lacks constexpr. I figured there must be some trick to make this work as its possible to do:
const wchar_t* myString = L"\x005Hello";
My attempt so far:
template<int Size>
wchar_t* toBstr(const wchar_t* str)
{
#pragma pack(push)
#pragma pack(1)
struct BStr
{
int len;
wchar_t data[Size];
};
#pragma pack(pop)
static BStr ret;
ret.len = Size;
// don't want to have to copy here, how else could this work??
//ret.data = str;
return ret.data;
}
const wchar_t* m = toBstr<_countof(L"Hello")>(L"Hello");
This question seems related:
C++ template string concatenation
But not concat for two string constants, rather a constant generated from the length of the 2nd :)