简体   繁体   中英

virtual inheritance with covariant return type and a template class argument, LINK error in vs2013

This is my code:

#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 gave me a link error.

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)

I tried gcc, and it compiles.

If I do any one of the following things, VC will compile:

  1. change the line (1) to a non-template type
  2. remove the "virtual" in the line (2)
  3. change the return type to A& in the line (3)
  4. uncomment the line (4)

Why?

This is likely a VC bug indeed; Clang and G++ both accept this code. Interestingly enough, changing the code to not use an initializer-list in the call to B.test() as below also eliminates the error, which leads me to believe that there is a problem with VC++'s initializer list support that is causing this.

#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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM