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>();
}
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:
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.