c++templatestemplate-instantiation

C++ standard requirements to templates that are not instantiated


So I tried to compile the code below and it failed (as expected):

1.cpp: In function ‘int foo()’:
1.cpp:3:5: error: ‘some’ was not declared in this scope
     some ill-formed code
     ^

But if I remove this line the compiler compiles it without any errors (also expected as it is unknown whether the T type has random_name() method or not).

It seems that diagnostic for templates that are not used (not instantiated) is implementation defined to some extent. But perhaps the standard has some requirements for such cases. For example would it conform to the standard to compile the code below without any errors?

I tried to search for the answer on the site but could not find any related questions.

template <class T>
int foo() {
    some ill-formed code
    return T::random_name();
}

template <>
int foo<int>() { return 0; }

int main() {
    return foo<int>();
}

Solution

  • This is a quality of implementation issue, it is ill-formed but if it is not instantiated no diagnostic is required as per [temp.res#8.1]p:

    The validity of a template may be checked prior to any instantiation. [ Note: Knowing which names are type names allows the syntax of every template to be checked in this way. — end note  ] The program is ill-formed, no diagnostic required, if:

    • no valid specialization can be generated for a template or a substatement of a constexpr if statement within a template and the template is not instantiated, or

    and we can see from this live godbolt example MSVC does not diagnose this case. This is because MSVC is not using two-phase lookup but using /permissive- changes this. clang even has an MSVC compatibility mode to emulate this using -fdelayed-template-parsing.

    We can see from this live godbolt using these two options clang no longer produces a diagnostic but MSVC does.