I have the following (admittedly contrived) code that compiles just fine in gcc 6, but doesn't compile in gcc 7. Notice the use of an undeclared constructor in the definition of bar
. This should print an error if the function is ever referenced elsewhere in the code (uncommenting foo.bar()
causes gcc 6 to print an error). However, gcc 7 prints an error even if the function is not used.
Some changes cause the code to also compile with gcc 7 (e.g. if B
is replaced with T
in the definition of A
), while some changes cause it to fail with gcc 6 (e.g. if this->
is not used). What's going on here? When does gcc decide to compile unused template code? Do different versions of gcc use different rules to decide?
struct B {};
template <typename T>
struct A {
B* bar()
{
// undeclared constructor
return new B(this->b);
}
B* b;
};
int main (int argc, char* argv[])
{
A<int> foo;
//foo.bar();
}
A<int>
instantiation, because it can prove that whatever T, the function bar can never be valid. But it doesn't have to, it was added because it is considered helpful. – Earthquake