2014-09-25 123 views
8

这是我的代码: 虚拟继承在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 &)" 
([email protected][email protected]@[email protected]@@[email protected]@[email protected]@@Z) 

我试过gcc,它编译。

如果我做下列事情任一项,VC将编译:

  1. 更改的行(1)到非模板类型
  2. 删除“虚拟”的线(2)
  3. 改变在该行返回类型甲&(3)
  4. 取消对线(4)

为什么呢?

+0

现场测试链接:http://rextester.com/XZA77022 – ecatmur 2014-09-25 15:27:47

+4

看起来像一个VC bug。 – 2014-09-25 15:40:09

+0

当您怀疑有错误时,您应该提供确切的编译器版本。 VC++ 2013不是一个确切的版本,有4个更新(如服务包)以及特定的错误修复。您应该打开Visual Studio工具命令提示符并输入'cl'来获得完整版本 – 2014-11-20 22:40:06

回答

0

确实这可能是一个VC错误; Clang和G ++都接受这个代码。有趣的是,在调用如下也改变代码不使用初始化列表也消除了这个错误,这导致我相信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); 
}