В С++ невозможно передать строки в аргументах шаблона.
Можно обойти это создав прокси-класс.
Правда работа со строкой будет уже во времени выполнения
Использование:
#include <algorithm>
template<typename String>
struct a
{
a()
{
// Copy string argument
std::copy(String::get(), String::get() + String::length + 1, &str[0]);
}
typename String::value_type str[String::length + 1];
};
// Определение строк
DEF_STRING(abc);
DEF_WSTRING(abc);
int main()
{
// Использование
a<abc_tag> x; // x.str == "abc"
a<abc_wtag> y; // y.str == L"abc"
return 0;
}
Код:
#include <cstddef>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/stringize.hpp>
#include <boost/preprocessor/wstringize.hpp>
// Using: sizeof(array_length(array))
template<typename T, size_t N>
char (&array_length(T(&)[N]))[N];
#define STRING_TAG(str) \
BOOST_PP_CAT(str, _tag)
#define WSTRING_TAG(str) \
BOOST_PP_CAT(str, _wtag)
#define DEF_STRING_T(str, type, tag, stringize) \
struct tag(str) \
{ \
static const size_t length = sizeof(array_length(stringize(str))) - 1; \
typedef type value_type; \
typedef type return_type[length + 1]; \
static return_type const& get() { return stringize(str); } \
};
// Using: DEF_STRING(abc)
#define DEF_STRING(str) \
DEF_STRING_T(str, char, STRING_TAG, BOOST_PP_STRINGIZE)
// Using: DEF_WSTRING(abc)
#define DEF_WSTRING(str) \
DEF_STRING_T(str, wchar_t, WSTRING_TAG, BOOST_PP_WSTRINGIZE)