简体   繁体   English

C++ 重载转换操作符hack

[英]C++ Overloading conversion operator hack

I am trying to implement a hack where I am trying to overload return.我正在尝试实现一个 hack,我试图重载返回。 I will let the code talk.我会让代码说话。

namespace tcn
{
    class foo
    {
        std::string blah;

    public:
        class proxy
        {
            int intval;
            std::string strval;
        public:
            proxy(int intv, std::string strv) : intval(intv), strval(strv) {};
            operator int() const { return intval; }
            operator std::string() const { return strval; }

        };

        foo();
        ~foo();

        proxy get();
    };

    foo::foo()
    {
        // Create stuff //
    }

    foo::~foo()
    {
        // Destroy stuff //
    }

    foo::proxy foo::get()
    {
        int intval = 911;
        std::string strval = "Hello!?";

        proxy temp(intval, strval);

        return temp;
    }
}



int main()
{
    tcn::foo bar;

    std::string ts = bar.get(); // OK
    int ti = bar.get(); // OK

    ti = bar.get(); // OK

    ts = bar.get(); // Compile error here

    return 0;
}

If I try to compile the code it gives an error like following如果我尝试编译代码,则会出现如下错误

error: ambiguous overload for 'operator=' (operand types are 'std::string {aka std::basic_string}' and 'tcn::foo::proxy')错误:'operator=' 的不明确重载(操作数类型是 'std::string {aka std::basic_string}' 和 'tcn::foo::proxy')
ts = bar.get(); ts = bar.get();

I was wondering how to overcome this.我想知道如何克服这个问题。 I have seen other ways to implement this by using 'hints' ,But am trying to give an easy interface to the user.我已经看到通过使用“提示”来实现这一点的其他方法,但我试图为用户提供一个简单的界面。 So I am looking for a simple assignment from the user side.所以我正在寻找用户方面的简单分配。 How else could this be implemented?这还能如何实施?

Thank you in advance and if this looks lame- I apologize.提前谢谢你,如果这看起来很蹩脚 - 我道歉。 I am no that good in C++.我不擅长 C++。

The call is ambiguous because std::string::operator= has overloads taking std::string and char .该调用是模棱两可的,因为std::string::operator=具有采用std::stringchar重载。 Both of these can be called using a tcn::proxy : the first using the std::string conversion operator, and the second using the int conversion operator followed by an integral conversion.这两个都可以使用tcn::proxy调用:第一个使用std::string转换运算符,第二个使用int转换运算符,然后是整数转换。

Usually overload resolution would prefer the implicit conversion sequence with no necessary standard conversions over the one requiring an integral conversion, but this choice is only considered if the two conversion sequences go through the same function.通常重载决议更喜欢没有必要标准转换的隐式转换序列,而不是需要整数转换的转换序列,但只有当两个转换序列通过相同的函数时才考虑这种选择。 Since you have two separate paths going through two different functions, the call is ambiguous.由于您有两条不同的路径通过两个不同的函数,因此调用是不明确的。

The solution is to not use integral conversions for things like that;解决方案是不要对此类事情使用积分转换; it just leads to weird edge cases, surprising behaviour and hidden bugs.它只会导致奇怪的边缘情况、令人惊讶的行为和隐藏的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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