将对象强制转换为std :: string时发生错误 [英] Error during cast of an object to std::string with proper operator-overloading

查看:160
本文介绍了将对象强制转换为std :: string时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Follwing短程序将与VS 2013完美并达到标记点。但在XCode中,编译器将显示一个错误,由于模糊的构造函数。如何解决?

Follwing short programm will run perfect with VS 2013 and reach the marked point. But in XCode the compiler will show an error due ambiguous constructor. How to work around?

#include <iostream>
#include <string>

class atest
{
public:
    explicit operator const char *()
    {
        return "";
    }
    template<class T> operator T()
    {

    }
    operator std::string()
    {
        return std::string("Huhuhu");
    }
    template<class T> atest &operator =(T value)
    {

    }
    atest &operator =(const std::string &value)
    {
        return *this; // I want to reach this point
    }
};

int main(int argc, char* argv[])
{
    atest tst;

    //   auto a = (std::string)tst;
    std::string astr;

    // do some stuff

    astr=tst; // I wanna keep this line

    return 0;
}

Clang无法区分VS2013正确使用的不同构造函数。我现在搜索一种排除赋值运算符的const char *模板的方法。

Clang is not able to distinguish between different constructor where VS2013 is taking the right one. I search now for a way to exclude the "const char *" template of the assignment operator.

推荐答案

std :: string 具有多个构造函数使用单个参数,因为您提供了<$ c $的转换运算符c> std :: string 通用的任何类型的转换操作符,编译器根本不知道选择哪个构造函数。

std::string have multiple constructors taking single arguments, and since you provide both a conversion operator for std::string and a generic any-type conversion operator, the compiler simply don't know which constructor to pick.

这篇关于将对象强制转换为std :: string时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆