编译器错误`在if语句中分配不兼容的类型'

sha*_*o12 3 c++ templates if-statement function-templates template-classes

在构建期间,编译器会不断分配不兼容的类型。

错误信息:

error: assigning to 'int' from incompatible type 'QString'
typeduserproperty.cpp:115:28: note: in instantiation of member function 'core::TypedUserProperty<int>::setValue' requested here
Run Code Online (Sandbox Code Playgroud)

样例代码

 /**
  * @brief setValue
  * set value to property
  * @param val
  * value to set to property
  * @return
  * true - successfully set value
  * false - invalid value
  */
template<class T>
void TypedUserProperty<T>::setValue(QVariant val)
{

   if (std::is_same<T, int>::value == true) 
   {
      this->_value = val.toInt();
   }
   else if (std::is_same<T, QString>::value == true)
   {
      this->_value = val.toString();
   }
   else if (std::is_same<T, double>::value == true)
   {
      this->_value = val.toDouble();
   }
}
Run Code Online (Sandbox Code Playgroud)

this->_value = val.toString(); 是发生错误的行

“ _value”是数据类型模板T

在这种情况下,我将T模板设置为“ int”

有谁知道为什么会这样或是否有解决方法。

son*_*yao 5

问题是,即使将template参数指定为intelse也必须在编译时实例化这些部分。

您可以应用Constexpr If(自C ++ 17起)。

如果值为true,则丢弃statement-false(如果存在),否则,丢弃statement-true。

例如

if constexpr (std::is_same<T,int>::value == true) {
    this->_value = val.toInt();
} else if constexpr (std::is_same<T,QString>::value == true) {
    this->_value = val.toString();
} else if constexpr (std::is_same<T,double>::value == true){
    this->_value = val.toDouble();
}
Run Code Online (Sandbox Code Playgroud)


JeJ*_*eJo 5

有谁知道为什么会这样或是否有解决方法?

由于您正在使用(正常)if-else,即使只有一个条件为真,所有剩余的else分支也会在编译时启动。

  1. 解决方案-1

    如果您只能访问,那么SFINE(即“替换失败不是错误”)技术和函数重载将是一种解决方法。这只会TTypedUserProperty<T>类实例化中根据类模板打开(即实例化)正确的方法。

    #include <type_traits> // std::enable_if, std::is_same
    
    // helper traits for `std::enable_if`
    template<typename T, typename ReType = void> using EnableIfInteger = typename std::enable_if<std::is_same<T, int>::value, ReType>::type;
    template<typename T, typename ReType = void> using EnableIfDouble = typename std::enable_if<std::is_same<T, double>::value, ReType>::type;
    template<typename T, typename ReType = void> using EnableIfQString = typename std::enable_if<std::is_same<T, QString>::value, ReType>::type;
    
    template<class T> class TypedUserProperty
    {
       T _value;
    public:
       template<typename Type = T> // this will be instantiated when T = int
       auto setValue(QVariant val)->EnableIfInteger<Type> {
           this->_value = val.toInt();
       }
    
       template<typename Type = T> // this will be instantiated when T = double
       auto setValue(QVariant val)->EnableIfDouble<Type> {
           this->_value = val.toDouble();
       }
    
       template<typename Type = T> // this will be instantiated when T = QString
       auto setValue(QVariant val)->EnableIfQString<Type> {
           this->_value = val.toString();
       }
    };
    
    Run Code Online (Sandbox Code Playgroud)
  2. 解决方案-2

    ,上述解决方案更为冗长,因为它提供了该if constexpr 功能。这样,就可以实例化在编译时为true 的唯一分支@songyuanyao 在他的回答中对此做了解释。