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

栏目: 编程工具 · 发布时间: 5年前

内容简介:翻译自:https://stackoverflow.com/questions/26042254/virtual-inheritance-with-covariant-return-type-and-a-template-class-argument-li

这是我的代码:

#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)行更改为非模板类​​型

>删除行中的“虚拟”(2)

>将退货类型更改为A&在行(3)

>取消注释线(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);
}

翻译自:https://stackoverflow.com/questions/26042254/virtual-inheritance-with-covariant-return-type-and-a-template-class-argument-li


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Python网络编程基础

Python网络编程基础

John Goerzen / 莫迟 等 / 电子工业出版社 / 2007 / 68.00元

《Python网络编程基础》可以作为各层次Python、Web和网络程序的开发人员的参考书,在实际工作中使用书中的技术,效果更佳。一起来看看 《Python网络编程基础》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具