下面是使用宏
template < typename CharT >
struct char_t_literal_selector;
template <>
struct char_t_literal_selector<char> {
static const char *select(const char *s, const wchar_t *, const char16_t *, const char32_t *)
{
return s;
}
};
template <>
struct char_t_literal_selector<wchar_t> {
static const wchar_t *select(const char *, const wchar_t *s, const char16_t *, const char32_t *)
{
return s;
}
};
template <>
struct char_t_literal_selector<char16_t> {
static const char16_t *select(const char *, const wchar_t *, const char16_t *s, const char32_t *)
{
return s;
}
};
template <>
struct char_t_literal_selector<char32_t> {
static const char32_t *select(const char *, const wchar_t *, const char16_t *, const char32_t *s)
{
return s;
}
};
#define CHART_LITERAL(str) (char_t_literal_selector<CharT>::select(str, L ## str, u ## str, U ## str))
template<typename CharT, typename TraitsT = std::char_traits<CharT> >
struct Foo
{
std::basic_string<CharT, TraitsT> getFoo(void) const
{
return CHART_LITERAL("Foo"); // + this->member_var1 + this->member_var2...
}
};
假設模板參數名稱始終CharT
的解決方案。如果不是,則向宏添加另一個參數。 HTH
好問題... – 2010-11-04 20:01:00