Extern pattern using type alias

I was wondering if this is a valid compiler error or not and the compiler is doing the right thing. Let's assume you have a simple templated class specified in your 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 contain an explicit creation of the 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 guess that the type alias would work fine, but I'm 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 extern, how can I be sure the compiler is not creating a type alias here and is actually doing the right thing.

+3


source to share





All Articles