MSVC生成代码,而g ++和clang因模棱两可的operator =错误而失败

时间:2018-10-08 13:02:28

标签: c++ visual-c++ g++ clang++

我创建了以下示例,该示例在MSVC(2015、2017)上编译,而在g ++和clang上失败。虽然我知道如何解决该问题(使用显式强制转换而不是隐式强制转换),但是我在徘徊哪个编译器更接近该示例的标准?

#include <iostream>
#include <string>

struct A {
    std::string a;
};

struct B {
    std::string b1;
    std::string b2;
};

struct C {
    int c;
};

class test
{
public:
    std::string t_a;
    std::string t_b1;
    std::string t_b2;
    int t_c;

    template <class ValueT>
    operator ValueT() const
    {
        ValueT tmp;
        get_values(*this, tmp);
        return  tmp;
    };
};

void get_values(const test& t1, A& a)
{
    a.a = t1.t_a;
};

void get_values(const test& t1, B& b)
{
    b.b1 = t1.t_b1;
    b.b2 = t1.t_b2;    
};

void get_values(const test& t1, C& c)
{
    c.c = t1.t_c;
};

template<class Base1, class Base2>
class Mixin : public Base1, public Base2
{
public:
    virtual Mixin& operator= (const Base1& p) { Base1::operator=(p); return     *this; }
    virtual Mixin& operator= (const Base2& p) { Base2::operator=(p); return *this; }

    virtual void merge(const Base1& p1, const Base2& p2) {
        *this = p1;
        *this = p2;
    }
};

template<class Base1, class Base2>
void get_values(const test& j, Mixin<Base1, Base2>& p)
{
    Base1 b1 = j;
    Base2 b2 = j;
    p.merge(b1, b2);
}

using mixed = Mixin<A, Mixin<B,C>>;

int main() 
{
    test tt {
        "123",
        "456",
        "789",
        10
    };

    mixed m{};
    m = tt;

    std::cout << m.a << " " << m.c << "\n";
}

我希望您能得到有关此样本的任何线索。

0 个答案:

没有答案