2

This is a follow up questions from How to convert typename T to string in c++

I am asking because I would really like to generate nice error messages like

static_assert(one_of<T,Components...>::value,
              "Unable to access T because you didn't 
              use it in filter<Components...>.");

Would print

Unable to access Foo because you did not use it in filter<Bar,Baz,Bat>.

Is something like this now possible in C++11 / 14?

Community
  • 1
  • 1
Maik Klein
  • 15,548
  • 27
  • 101
  • 197
  • That's very different to just converting it to a string, because the conversion has to take place at a very specific time in the compilation process and not at runtime. – Puppy Dec 30 '14 at 17:21
  • 2
    Unfortunately, `static_assert`'s second argument must be a *string-literal*, which means you have to assemble the string *prior* to compilation, e.g. by using the preprocessor *shudder*. The preprocessor however does not know that type some template parameter `T` of a specific specialization of a template refers to. – dyp Dec 30 '14 at 17:21
  • I clarified the question's title. – Johannes Schaub - litb Dec 30 '14 at 19:10

2 Answers2

1

Quoting from the poor(lazy?) man's version of the C++ Standard,

Since message(the second argument to static_assert) has to be a string literal, it cannot contain dynamic information or even a constant expression that is not a string literal itself. Typically, it cannot contain the name of the template type argument.

So, there isn't a way of getting the friendly static_assert error messages you desire.

Pradhan
  • 16,391
  • 3
  • 44
  • 59
0

No, no it is not possible to do that.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Mohamad Shiralizadeh Dec 30 '14 at 19:04
  • 4
    @Mohamadshiralizadeh he interpreted his question to be a question actually about `static_assert` and whether it supports a certain feature and not about converting `T` to its string representation in general. And he answered with "no, it is not possible". IMO it is a reasonable interpretation of a (slightly) ambiguous question and a reasonable answer. – Johannes Schaub - litb Dec 30 '14 at 19:08
  • I think the OP's example makes it quite clear what he wants to do, and my answer clearly answers it. – Puppy Dec 30 '14 at 21:12