使用类型别名的extern模板

时间:2017-06-03 01:51:55

标签: c++ templates using extern

我想知道这是否是一个有效的编译器错误,以及编译是否正确。 假设在头文件和cpp文件中定义了一个简单的模板类。

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

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

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

cpp文件还将包含典型类型的显式实例化。

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

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

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

我原本预计类型别名会运行正常,但我不确定。

#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;
}

即使我不能在extern中使用类型别名,我怎么能确定编译器没有在这里实例化我的类型别名,并且实际上正在做正确的事情。

0 个答案:

没有答案
相关问题