Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7173155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:45:53+00:00 2026-05-28T15:45:53+00:00

Consider the following simple map: class MyCoolMap : public unordered_map<const char *, const char

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:45:54+00:00Added an answer on May 28, 2026 at 3:45 pm

    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")));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following simple use case: public class Foo { public virtual int
Consider following simple DAO example: public abstract class DAOFactory { public abstract AccountDAO getAccountDAO();
Consider the following simple code import java.util.*; public class MainTest<T extends Object1<?,?>> { List<T>
Consider the following simple example: [DataContract({0}Base)] public class Base<T> where T : Entity<T> {
Consider the following simple polymorphism ... class Parent { public: someFunc() { /* implementation
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider a following chunk of service: public class ProductService : IProductService { private IProductRepository
Consider the following simple html page markup: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN
Consider the following simple RESTEasy (JAX-RS) service: @Path(/example-service) public interface ExampleService { @Path(/ping) @GET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.