内容简介:翻译自: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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
算法概论
Sanjoy Dasgupta、Christos Papadimitriou、Umesh Vazirani / 王沛、唐扬斌、刘齐军 / 清华大学出版社 / 2008-7 / 39.99元
《国外经典教材·算法概论》涵盖了绝大多数算法设计中的常用技术。在表达每一种技术时,阐述它的应用背景,强调每个算法运转背后的简洁数学思想,注意运用与其他技术类比的方法来说明它的特征,并提供了大量相应实际问题的例子。《国外经典教材·算法概论》同时也注重了对每一种算法的复杂性分析。全书共10章,从基本的数字算法人手,先后介绍了分治、图的遍历、贪心算法、动态规划、线性规划等技术,对NP完全问题进行厂基本而......一起来看看 《算法概论》 这本书的介绍吧!