Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing of uninstantiated template functions

The following code compiles in Visual C++ 2013 but not under G++ 4.8.2:

template<class T>
int MyFunc(T& t)
{
    return static_cast<int>(CCodes::blah);
}

template<>
int MyFunc(float& t)
{
    return 0;
}

int main() {
    float f = 10.f;
    return MyFunc(f);
}

Visual C++ seems to ignore the general template function because only the specialisation MyFunc<float> is used. G++ parses the general function anyway and spots that the CCodes enumeration has not been defined.

Which is right? Or is this implementation-defined?

like image 491
Tom Avatar asked Jul 23 '14 06:07

Tom


1 Answers

GCC is correct, and every other compiler besides MSVC will do the same thing.

This is a major bug, which actually appeared on one MSVC future roadmap. It was in the "distant future" category. They will have to rewrite their template engine to fix it.

There is a line of argument that diagnosis of an ill-formed template is optional, because it's really a template with no well-formed instantiation, and those are not required to be flagged. However,

  1. The standard requires the template to be parsed, and failure to parse must be diagnosed regardless of instantiation.
  2. Every other compiler makes the diagnosis, so in effect not doing so leads MSVC users to produce unportable code. Complaining is a really good idea, even if it's not required.
like image 174
Potatoswatter Avatar answered Oct 17 '22 03:10

Potatoswatter