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将编译:
为什么?
这确实可能是一个 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)
| 归档时间: |
|
| 查看次数: |
424 次 |
| 最近记录: |