【问题标题】:virtual inheritance with covariant return type and a template class argument, LINK error in vs2013具有协变返回类型和模板类参数的虚拟继承,vs2013 中的 LINK 错误
【发布时间】:2014-11-20 10:38:23
【问题描述】:

这是我的代码:

#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. 将第(1)行改为非模板类​​型
  2. 去掉第(2)行中的“virtual”
  3. 将第(3)行的返回类型改为A&
  4. 取消注释第 (4) 行

为什么?

【问题讨论】:

  • 直播测试链接:rextester.com/XZA77022
  • 看起来像一个 VC 错误。
  • 当您怀疑存在错误时,您应该提供准确的编译器版本。 VC++ 2013 不是一个确切的版本,有 4 个更新(如服务包)和特定的错误修复。您应该通过打开 Visual Studio 工具命令提示符并键入 cl 来获得完整版本

标签: c++ templates visual-c++ virtual covariance


【解决方案1】:

这确实可能是一个 VC 错误; Clang 和 G++ 都接受此代码。有趣的是,将代码更改为在对B.test() 的调用中不使用初始化列表如下 also 消除了错误,这让我相信 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);
}

【讨论】:

    猜你喜欢
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多