Suppose you have the following (ill-formed) program:
struct A
{
A(int, int)
{
}
};
template <typename T>
class B
{
B()
{
if (sizeof (T) == 1)
{
throw A(0); // wrong, A() needs two arguments
}
}
};
int main()
{
return 0;
}
GCC compiles this program without any errors, clang++ refuses it with an error.
- Is it justified to say thats it is not a bug in GCC because the template isnt instantiated?
- What magic does clang do to find this error?
- What does the C++ standard say about those situations?
The template is instantiated when used. However, it should be compiled when it’s defined. Your code
A(0)uses the nameA, which doesn’t depend on the template parameterT, so it should be resolved when the template is defined. This is called two-phase lookup. The way clang finds the error is simply by trying to resolve the callA(0)as soon as it sees it.My version of GCC also compiles this code silently, even with
-pedantic-errors. Both C++03 and C++11 say that no diagnostic is required, even though the program is ill-formed, so GCC conforms. This is 14.6/7 in C++03 and 14.6/8 in C++11: