Extern "inconsistent explicit instances" pattern
Considering
#include <vector> // Case I: error
error C2961: 'std :: vector>': inconsistent explicit instances, previous explicit specify "extern pattern"
template class std::vector<int>; extern template class std::vector<int>; // Case II: fine //extern template class std::vector<int>; //template class std::vector<int>; // Case III: fine //extern template class std::vector<int>; //template class std::vector<int>; //template class std::vector<int>; // Case IV: fine //extern template class std::vector<int>; //extern template class std::vector<int>; //template class std::vector<int>; //template class std::vector<int>; int main() { }
I am creating code with version VS2015 version 4.6 and see error C2961. However, I do not see an error with the http://gcc.godbolt.org/ compiler.
Is this a bug with VS2015?
MSVC and Clang have the right to throw an error in this case. [temp.explicit] / 11 in the standard says:
If an object is an object as an explicit instantiation of the declaration and the explicit definition of the instance in the same translation unit, the definition must follow the declaration. [...]
template class std::vector<int>;
is an explicit definition of instantiation. One that has extern
is an explicit creation declaration.
Note that explicitly creating an instance of std::vector<int>
in any case poorly formed in accordance with [namespace.std] / 2:
[...] A program can explicitly create a template defined in the standard library only if the declaration depends on the name, a user-defined type, and instantiation corresponds to the standard library requirements for the original template.
Also note that cases III and IV are also poorly formed according to [temp.spec] / 5:
For a given template and a given set of argument templates
- an explicit definition of instantiation should appear no more than once in a program,
[...]
Implementation is not required to diagnose a violation of this Rule.