2014-09-25 126 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); 
}