具有协变返回类型和模板类参数的虚拟继承,vs2013中的LINK错误

wuw*_*bin 8 c++ virtual templates covariance visual-c++

这是我的代码:

#include <vector>
struct A
{
    typedef std::vector<int> vec; //(1) template type
    virtual A& test(vec) = 0; 
};

struct B : public virtual A //(2) virtual inheritance
{
    virtual B& test(vec) override //(3) covariant return type
    {
        return *this;
    }
};

//std::vector<int> vv, cc(vv); //(4) explicit instantiate copy-ctor

int main()
{
    B b;
    b.test({});
}
Run Code Online (Sandbox Code Playgroud)

Visual C++ 2013给了我一个链接错误.

error LNK2001: unresolved external symbol "public: __thiscall
std::vector<int,class std::allocator<int> >::vector<int,class
std::allocator<int> >(class std::vector<int,class 
std::allocator<int> > const &)"
(??0?$vector@HV?$allocator@H@std@@@std@@QAE@ABV01@@Z)
Run Code Online (Sandbox Code Playgroud)

我试过gcc,然后编译.

如果我执行以下任何一项操作,VC将编译:

  1. 将第(1)行更改为非模板类​​型
  2. 删除行中的"虚拟"(2)
  3. 将返回类型更改为A&(第3行)
  4. 取消注释线(4)

为什么?

LTh*_*ode 0

这确实可能是一个 VC bug;Clang 和 G++ 都接受此代码。有趣的是,将代码更改为在调用中不使用初始化列表(B.test()如下所示)也消除了该错误,这使我相信 VC++ 的初始化列表支持存在问题,从而导致了此错误。

#include <vector>
struct A
{
    typedef std::vector<int> vec; //(1) template type
    virtual A& test(vec) = 0; 
};

struct B : public virtual A //(2) virtual inheritance
{
    virtual B& test(vec) override //(3) covariant return type
    {
        return *this;
    }
};

//std::vector<int> vv, cc(vv); //(4) explicit instantiate copy-ctor

int main()
{
    A::vec v;
    B b;
    //b.test({});
    b.test(v);
}
Run Code Online (Sandbox Code Playgroud)