繁体   English   中英

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

[英]virtual inheritance with covariant return type and a template class argument, LINK error in vs2013

这是我的代码:

#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({});
}

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)

我尝试了gcc,它可以编译。

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

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

为什么?

确实这可能是VC错误; 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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM