C++11 static_assert and template instantiation

C++C++11

C++ Problem Overview


In C++11, should the operation of static_assert within a template depend on whether that template has been instantiated or not? For example, with the following code

template <int I>
void sa() { static_assert(0,"Hello."); }

int main(int argc, char *argv[]) { return 0; }

GCC 4.5.0 will fail the assertion, and produce the "Hello." message. The Digital Mars Compiler version 8.42n on the other hand, gives no message.

C++ Solutions


Solution 1 - C++

GCC is correct and the other compiler is correct too. Refer to 14.6p8 in the spec

> If no valid specialization can be generated for a template definition, and that template is not instantiated, the template definition is ill-formed, no diagnostic required.

Therefor, a compiler is free to reject the following

template<typename T>
void f() {
  static_assert(0, "may trigger immediately!");
  static_assert(sizeof(T) == 0, "may trigger immediately!");
}

If you want to go safe, you have to arrange it so the compiler cannot know until instantiation whether the boolean expression will be true or false. For example, get the value by getvalue<T>::value, with getvalue being a class template (one could specialize it, so the compiler cannot possibly know the boolean value already).

Solution 2 - C++

I believe that the compiler is well within it's rights to expand any static assertions that are not dependent on template parameters without needing an instantiation- but I don't believe this is required. Remember also that different draft Standards may have different rules about when this may occur.

Solution 3 - C++

The C++0x draft (N3242) says in 14.6p8:

> "If no valid specialization can be > generated for a template definition, > and that template is not instantiated, > the template definition is ill-formed, > no diagnostic required."

The same words appear in the C++03 standard.

In the case of the example from the question, no valid instantiation can be made for this template, so the quoted wording applies.

Since no diagnostic is required, the compiler may compile the program if the template is not instantiated. Of course, if it is instantiated then the program is ill-formed, with a diagnostic required.

Solution 4 - C++

I used a helper function to make the false dependent on the template parameter:

template<typename T> 
bool dependentFalse<T>()
{
    return false;
}

template<typename T>
Foo foo()
{
    static_assert(dependentFalse<T>(), "this template shouldn't be instantiated");
}

Solution 5 - C++

This is 14.6.2 section in C++ standard.

Your static_assert is related to support binding nondependent names when initially parsing a template. The Digital Mars Compiler does not currently support binding nondependent names. GCC 4.5.0 does support binding nondependent names.

If your expression does not depend on the template parameters then such expression is known when initially parsing a template. Compiler must show the error message. GCC 4.5.0 does it.

Replace your 0 in static_assert with I*I < 0, for expample and you get dependent name. Error message will appear for the case of uninstantiated template only.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2023370View Question on Stackoverflow
Solution 1 - C++Johannes Schaub - litbView Answer on Stackoverflow
Solution 2 - C++PuppyView Answer on Stackoverflow
Solution 3 - C++Anthony WilliamsView Answer on Stackoverflow
Solution 4 - C++starblueView Answer on Stackoverflow
Solution 5 - C++Alexey MalistovView Answer on Stackoverflow