1

In a nutshell:

It's possible to convert macro parameter to character string literal (that contains the spelling of the preprocessing token sequence for the corresponding argument, regarding to 16.3.2, n3797) with # operator. Is there a way, some trick, maybe, to convert macro parameter to multibyte character ? So, for example, I will have some macro TO_CHAR(WHATEWER) and TO_CHAR(CONSTANT) will give me 'CONSTANT' multibyte character ?

I need this to work with boost::mpl::string. More precisely, I think so:

I have a lot of constants and I work with them with boost::mpl::vector (boost::mpl::vector_c). Now, I need to print out the name of constant after some work with this constant. Pseudo-pseudo code:

struct Temp
{
    template<typename Value>
    operator()(Value)
    {
        // ...
        std::cout << "The value of " << @Value::value Name << " is " << Value::value;
    };
};
typedef boost::mpl::vector_c<int, MY_CONST1, MY_CONST2> Constants;
boost::mpl::for_each<Constants>(Temp());

The output must be something like this:

The value of MY_CONST1 is 1
The value of MY_CONST2 is 2

I don't want to make some runtime array with a lot of typing:

typedef boost::mpl::vector_c<int, MY_CONST1, MY_CONST2> Constants;
const char* constant_names[] = { "MY_CONST1", "MY_CONST2" };
// Work with this stuff

What I want to have is something like this:

#define MAP_CONST(CONST) boost::mpl::pair<boost::mpl::int_<CONST>, (boost::mpl::string maybe here) #CONST>

typedef boost::mpl::vector<MAP_CONST(MY_CONST1), MAP_CONST(MY_CONST2)> Constants;
// Work with @Constants

Btw, I can't use constexp or new standard in general - I have C++03 compiler.

Thanks

UPD: Sorry for my mismatch - I mean not multibyte character, but "multicharacter literal" (regarding standard - 2.14.3, n3797). Somrthing like this

Community
  • 1
  • 1
grisha
  • 1,247
  • 1
  • 14
  • 20
  • 1
    Why are you using multicharacter literals? You realize, printing out a multicharacter literal like `'MY_CONST1'` is not, in general, going to produce `MY_CONST1` as output. – Brian Bi Aug 05 '14 at 21:06
  • @Brian, yes, but this will do boost::mpl::c_str with boost::mpl::string (http://www.boost.org/doc/libs/1_40_0/libs/mpl/doc/refmanual/string.html) – grisha Aug 05 '14 at 21:08
  • @user2451677 _'"multicharacter literal" (regarding standard - 2.14.3, n3797). Somrthing like this'_ That's a completely different thing vs. _"multibyte character"_ you are completely seem to be confused about this. Multicharacter literals are implementation defined and dependent, don't use them. It's not portable by means of standards. What else did you miss from that link you gave? – πάντα ῥεῖ Aug 05 '14 at 21:16
  • @πάνταῥεῖ, I already told that that was my mistake. I just want to work with boost::mpl::string. The only way to do this is to use multicharacter literal. The link that I gave is just to show what is multicharacter literal. What other cavils to my mistake and bad example ? – grisha Aug 05 '14 at 21:29
  • @user2451677 You're still asking for _"Is there a way, some trick, maybe, to convert macro parameter to multibyte character ?"_ :P ... – πάντα ῥεῖ Aug 05 '14 at 21:39
  • @user2451677 Is it true that the *only* way to use `boost::mpl::string` is to use multicharacter literals? Seems to me it would accept a sequence of single-character literals just as well. – Brian Bi Aug 05 '14 at 21:52
  • @Brian, yes, so here is another question: is there a way to represent MY_CONST as a sequence of single-character literals ? :P – grisha Aug 05 '14 at 22:17

0 Answers0