Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;转换运算符重载失败_C++_C++11 - Fatal编程技术网

C++ c++;转换运算符重载失败

C++ c++;转换运算符重载失败,c++,c++11,C++,C++11,我有代码为不同类型重载转换运算符: #include <string> #include <iostream> class MyClass { int m_int; double m_double; std::string m_string; public: MyClass() : m_int{1}, m_double(1.2), m_string{"Test"} {}; // overload the conversion opera

我有代码为不同类型重载转换运算符:

#include <string>
#include <iostream>

class MyClass {
   int m_int;
   double m_double;
   std::string m_string;

public:
   MyClass() : m_int{1}, m_double(1.2), m_string{"Test"} {};

   // overload the conversion operator for std::string
   operator std::string() {
      return m_string;
   }

   // overload the conversion operator for all other types
   template <typename T>
   operator T() {
      if (std::is_same<T, int>::value)
         return m_int;
      else
         return m_double;
   }
};

int main() {

   MyClass o;

   int i = o;
   double d = o;
   std::string s = o;

   std::cout << i << " " << " " << d << " " << s << std::endl;
}
编译失败

error: use of overloaded operator '=' is ambiguous (with operand types 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') and 'MyClass')
错误:重载运算符“=”的使用不明确(操作数类型为“std::string”(又名“basic_string”)和“MyClass”)

显然,编译器实例化了一个不同于std::string()的转换运算符。如果删除模板部分,编译器将被迫使用std::string()转换运算符,它将再次工作。那么我缺少什么呢?

std::string
类中,可以使用
操作符=()
将字符(以及结果整数)分配给
std::string
。这意味着可以接受以下代码,因为
运算符=(char)
确实存在:

std::string s1;

s1 = '1'; // works fine
s1 = 24; // works fine
但不能使用字符(以及整数)复制和构造字符串

因此,在您的例子中,当您使用复制构造时,没有歧义,因为
std::string
不能使用double或int进行复制构造。但是对于
operator=
来说,它是不明确的,因为您可以将整数赋给字符串,而编译器不知道要使用哪个转换运算符。换句话说,编译器可以同时使用字符串和int转换运算符

工作正常(感谢我的同事NB):

虽然这应该相当于

   template <typename T, typename std::enable_if<
           std::is_same<T, int>::value ||
           std::is_same<T, double>::value
           ,T>::type* = nullptr>
   operator T() {
      if (std::is_same<T, int>::value)
         return m_int;
      else
         return m_double;
   }
template::type*=nullptr>
算子T(){
if(std::is_same::value)
返回m_int;
其他的
返回m_double;
}
也不清楚为什么这不起作用:

template <typename T, typename std::enable_if<
        std::is_same<T, int>::value ||
        std::is_same<T, double>::value ||
        std::is_same<T, std::string>::value
        ,T>::type* = nullptr>
operator T() {
   if (std::is_same<T, std::string>::value)
      return m_string;
   else if (std::is_same<T, int>::value)
      return m_int;
   else
      return m_double;
}
template::type*=nullptr>
算子T(){
if(std::is_same::value)
返回m_字符串;
else if(std::is_same::value)
返回m_int;
其他的
返回m_double;
}

也许有人能给我一些启示。

对不起,意思是“转换运算符”,而不是“赋值运算符”,可能想提到赋值运算符只对
char
重载(在
std::basic_string
的情况下),所以
int
double
得到简单的转换我理解。但是我怎样才能说服编译器从字符串赋值而不进行任何转换呢?@Stefan我强烈建议您不要在代码中过度使用隐式转换运算符。请检查以下答案:。顺便说一句,为什么不在你们班上放三个不同的获得者呢?我的意思是,你应该明确你想要返回哪个数据成员,不是吗?@Stefan肯定不会过度使用implict转换操作符,这是一个教科书上的原因,但是如果你必须有一个转换操作符,那么就让它显式,并在它上面使用
static\u cast()
。老实说,尽管使用返回
std::string
@LonesomeParadise的
to_string()
方法要好得多,但您可以使用这种
显式
静态转换
机制在某处重新编写原始的Q代码吗?我现在感兴趣的是它如何解决这个问题。
#include <string>
#include <iostream>

class MyClass {
   int m_int;
   double m_double;
   std::string m_string;

public:
   MyClass() : m_int{1}, m_double(1.2), m_string{"Test"} {};

   // overload the conversion operator for std::string
   operator std::string() {
      return m_string;
   }

   // overload the conversion operator for all other types
   template <typename T, typename std::enable_if<
           std::is_same<T, int>::value ||
           std::is_same<T, double>::value
           ,T>::type* = nullptr>
   operator T() {
      if (std::is_same<T, int>::value)
         return m_int;
      else
         return m_double;
   }
};

int main() {

   MyClass o;

   int i = o;
   double d = o;
   std::string s;
   s = o;

   std::cout << i << " " << " " << d << " " << s << std::endl;
}
operator int() {
   return m_int;
}

operator double() {
   return m_double;
}
   template <typename T, typename std::enable_if<
           std::is_same<T, int>::value ||
           std::is_same<T, double>::value
           ,T>::type* = nullptr>
   operator T() {
      if (std::is_same<T, int>::value)
         return m_int;
      else
         return m_double;
   }
template <typename T, typename std::enable_if<
        std::is_same<T, int>::value ||
        std::is_same<T, double>::value ||
        std::is_same<T, std::string>::value
        ,T>::type* = nullptr>
operator T() {
   if (std::is_same<T, std::string>::value)
      return m_string;
   else if (std::is_same<T, int>::value)
      return m_int;
   else
      return m_double;
}