adding string literal to static_assert

Is there a way to combine what's going to be output by static_assert? What I mean is this:

template<class T>
struct X
{
static_assert(std::is_signed<T>::value, "Type " + T + " must be signed.");//this doesn't work
};

Upvotes: 5

Views: 4178

Answers (1)

satnhak
satnhak

Reputation: 9861

On the basis that this does not compile

int main()
{
   const char c[2] = "1";
   static_assert(1==1, c)
}

nor this

int main()
{
   const char* c = "1";
   static_assert(1==1, c)
}

Both with errors error: expected a string literal then I would have to conclude that it is not possible.

"A string literal consists of zero or more characters from the source character set surrounded by double quotation marks ("). A string literal represents a sequence of characters that, taken together, form a null-terminated string." - C++ String Literals

You might want to consider using Boost::StaticAssert as this might give you what you are looking for.

Upvotes: 4

Related Questions