Explicit instantiation of a variable template defined with `auto` keyword

373 Views Asked by At

If one has a variable template with the type deduced from the initializer by means of auto keyword, for example:

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

How to make an instantiation of the variable for a particular type (e.g. to export it from a shared library)?

GCC and Clang way is to replace auto with the specific type:

template int * p<int>;

But it is rejected by MSVC with the error:

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

Demo: https://gcc.godbolt.org/z/66xModTjK

MSVC demands to make the instantiation as:

template auto * p<int>;

which is in turn rejected by GCC and Clang with some weird messages:

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

Demo: https://gcc.godbolt.org/z/7j3nh7Whx

Which compilers are right here according to the standard?

0

There are 0 best solutions below