Given
#include <vector>
// Case I: error
error C2961: 'std::vector>': inconsistent explicit instantiations, a previous explicit instantiation did not specify 'extern template'
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 build the code with VS2015 Version 4.6 and see the C2961 error. However, I don't see the error with compiler http://gcc.godbolt.org/.
Is this a bug with VS2015?
MSVC and Clang are right to issue an error in this case. [temp.explicit]/11 in the standard says:
If an entity is the subject of both an explicit instantiation declaration and an explicit instantiation definition in the same translation unit, the definition shall follow the declaration. [...]
template class std::vector<int>;
is an explicit instantiation definition. The one with the extern
is an explicit instantiation declaration.
Note that explicitly instantiating std::vector<int>
is ill-formed in any case according to [namespace.std]/2:
[...] A program may explicitly instantiate a template defined in the standard library only if the declaration depends on the name of a user-defined type and the instantiation meets the standard library requirements for the original template.
Also note that cases III and IV are also ill-formed according to [temp.spec]/5:
For a given template and a given set of template-arguments,
- an explicit instantiation definition shall appear at most once in a program,
[...]
An implementation is not required to diagnose a violation of this rule.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With