3

如果有一个变量模板,其类型是通过auto关键字从初始化程序推导出来的,例如:

template <typename T>
auto * p = (T*)nullptr;

如何对特定类型的变量进行实例化(例如从共享库中导出)?

GCC 和 Clang 的方式是auto用特定类型替换:

template int * p<int>;

但它被 MSVC 拒绝并出现以下错误:

error C3410: 'p<int>': the type of the explicit instantiation 'int *' does not match the type of the variable template 'auto *'

演示:https ://gcc.godbolt.org/z/66xModTjK

MSVC 要求将实例化为:

template auto * p<int>;

这反过来又被 GCC 和 Clang 拒绝,并带有一些奇怪的消息:

error: 'auto' variable template instantiation is not allowed
error: declaration of 'auto* p<int>' has no initializer

演示:https ://gcc.godbolt.org/z/7j3nh7Whx

根据标准,哪些编译器就在这里?

4

0 回答 0