Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to express a string literal within a template parameterized by the type of the characters used to represent the literal?

Tags:

c++

Consider the following simple map:

class MyCoolMap : public unordered_map<const char *, const char *>
{
public:
  ProtoTypeMap()
  {
    insert(value_type("in1", "out1"));
    insert(value_type("in2", "out2"));
    ...
    insert(value_type("inN", "outN"));
  }
};

Now, suppose I need to make this map available both for char and wchar_t strings. So, I rewrite it as follows:

template<class C>
class MyCoolMap : public unordered_map<const C *, const C *>
{
public:
  MyCoolMap()
  {
    insert(value_type("in1", "out1"));
    insert(value_type("in2", "out2"));
    ...
    insert(value_type("inN", "outN"));
  }
};

And, of course, this does not work for C=wchar_t. The problem is that I do not know how to template the difference between char literals and wchar_t literals. Right now I see two solutions, both ugly.

Solution 1 - specialize MyCoolMap by wchar_t:

template<>
class MyCoolMap<wchar_t> : public unordered_map<const wchar_t *, const wchar_t *>
{
public:
  MyCoolMap()
  {
    insert(value_type(L"in1", L"out1"));
    insert(value_type(L"in2", L"out2"));
    ...
    insert(value_type(L"inN", L"outN"));
  }
};

This is bad, because the whole logic is duplicated.

Solution 2 - a traits like solution:

#define _TOWSTRING(x) L##x
#define TOWSTRING(x) _TOWSTRING(x)

template <class C, int> struct special_string;
#define DECL_SPECIAL_STRING(STR) \
const int ss_##STR = __LINE__; \
template<> struct special_string<char, ss_##STR> { static const char *get_value() { return #STR; } }; \
template<> struct special_string<wchar_t, ss_##STR> { static const wchar_t *get_value() { return TOWSTRING(#STR); } };

DECL_SPECIAL_STRING(in1)
DECL_SPECIAL_STRING(out1)
DECL_SPECIAL_STRING(in2)
DECL_SPECIAL_STRING(out2)
...
DECL_SPECIAL_STRING(inN)
DECL_SPECIAL_STRING(outN)

template<class C>
class MyCoolMap : public unordered_map<const C *, const C *>
{
public:
  MyCoolMap()
  {
#define INSERT_MAPPING(in, out) insert(value_type(special_string<C, ss_##in>::get_value(), special_string<C, ss_##out>::get_value()))
    INSERT_MAPPING(in1, out1);
    INSERT_MAPPING(in2, out2);
    ...
    INSERT_MAPPING(inN, outN);
#undef INSERT_MAPPING
  }
};

This way I do not need to replicate the logic, but this is so verbose and relies heavily on macros.

There must be a better way; I just do not see it.

I am using VS2010.

EDIT

I am glad that a much simpler solution is proposed - the credits go to https://stackoverflow.com/users/5987/mark-ransom. I had to make minor fixes to make it compile, though:

#define _TOWSTRING(x) L##x
#define TOWSTRING(x) _TOWSTRING(x)

template<typename C> const C * ChooseCW(const char * c, const wchar_t * w);
template<> const char * ChooseCW<char>(const char * c, const wchar_t * w)
{
  return c;
}
template<> const wchar_t *ChooseCW<wchar_t>(const char * c, const wchar_t * w)
{
  return w;
}

#define CW(C, STR) ChooseCW<C>(#STR, TOWSTRING(#STR))

Thanks again.

like image 444
mark Avatar asked Jan 27 '12 21:01

mark


People also ask

How do you write template literals?

Template literals are enclosed by backtick ( ` ) characters instead of double or single quotes. Along with having normal strings, template literals can also contain other parts called placeholders, which are embedded expressions delimited by a dollar sign and curly braces: ${expression} .

How do you write a literal string?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.

What character is used to signify a template literal?

To create a template literal, instead of single quotes ( ' ) or double quotes ( " ) quotes we use the backtick ( ` ) character.

What is an example of a string literal?

A string literal is a sequence of zero or more characters enclosed within single quotation marks. The following are examples of string literals: 'Hello, world!' 'He said, "Take it or leave it."'


1 Answers

Use a macro to generate both forms of the string, and a template function to choose which to use.

template<typename C>
const C * ChooseCW(const char * c, const wchar_t * w);

template<>
const char * ChooseCW<char>(const char * c, const wchar_t * w)
{
    return c;
}

template<>
const wchar_t * ChooseCW<wchar_t>(const char * c, const wchar_t * w)
{
    return w;
}

#define CW(C, STR) ChooseCW<C>(STR, L##STR)

insert(value_type(CW(C, "in1"), CW(C, "out1")));
like image 159
Mark Ransom Avatar answered Oct 25 '22 09:10

Mark Ransom