extern template with using type alias

Viewed 425

I was wondering if this is a valid compiler error or not and whether the compile is doing the right thing. Assume a simple template class defined in a header and cpp file.

// MyVector.h
template<class T>
class MyVector
{
public:
    MyVector() = default;
    ~MyVector() = default;

    int function();
private :
    T data;
};

// type alias
using MyVecD = MyVector<double>;

The cpp file will also include an explicit instantiation of a typical type.

// MyVector.cpp
#include "MyVector.h"

template<class T>
int MyVector<T>::function() { return data + 42; }

// explicit instantiation for double
template class MyVector<double>;

I would have expected that the type alias would have worked fine but I am not sure.

#include "MyVector.h"

// Suppresses implicit instantiation
// No problems here 
extern template class MyVector<double>;
// This fails to compile in VS 2017
// extern template class MyVecD;

int main()
{
    MyVecD test;
    auto result = test.function();

    return 0;
}

Even if I cannot use a type alias in the extern, how can I be sure that the compiler is not instantiating my type alias here and is actually doing the right thing.

0 Answers
Related