mardi 1 novembre 2016

C preprocessor insert/append token in string literal

I know that the preprocessor is better hands off than hands on. However, there are cases where the preprocessor is indispensable (e.g., library coding).

Based on this spirit I have the following macro:

#define ASSERT_ITERATOR_VALUE_TYPE(Iterator__, Value_type__)                      \
static_assert(std::is_same<Value_type__, typename Iterator__::value_type>::value, \
              "Expected iterator with value type #Value_type__")

In the macro above I'm trying to insert/append the Value_type__ token in the string literal that's feed in as the second input argument in static_assert.

Obviously, this is not what I'm trying to achieve, since if I state the macro as:

ASSERT_ITERATOR_VALUE_TYPE(std::set<int>::iterator, double)

I'll get the message:

error: static assertion failed: Expected iterator with value type #Value_type__
                                                                  ^^^^^^^^^^^^^

where instead I would like to take the message:

error: static assertion failed: Expected iterator with value type double
                                                                  ^^^^^^

Live Demo

Q

Is there some kind of preprocessor sorcery that will help me achieve what I wan't?

Aucun commentaire:

Enregistrer un commentaire