1

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?

Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
101010
  • 41,839
  • 11
  • 94
  • 168
  • See http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string – emulbreh Nov 01 '16 at 14:32
  • 1
    Don't spam, tags! This is not C! – too honest for this site Nov 01 '16 at 14:53
  • @Olaf C is pretty much C++. Plus the preprocessor comes from C. – 101010 Nov 01 '16 at 15:26
  • "This question does not appear to be about programming within the scope defined in the help center." Jesus Christ... Going back to work... – 101010 Nov 01 '16 at 15:32
  • @101010: Please provide a reference to the C standard where it allows templates, inheritance, user-namespaces, etc. And to the C++ standard where it allows VLAs, FAMs, etc. Until then: please refrain from spreading such nonsense. And I did not remove the preprocessor tag. The C tag is nonsense, though. – too honest for this site Nov 01 '16 at 18:20

1 Answers1

9
#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__)

You expand the macro parameter into a string literal, and then rely on string literal concatenation.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458