简体   繁体   English

错误:“operator+=”不匹配(操作数类型为“long double”和“std::__cxx11::basic_string”<char> &#39;)

[英]error: no match for 'operator+=' (operand types are 'long double' and 'std::__cxx11::basic_string<char>')

I need to read values from a tuple that could be any datatype and then add them if they are numbers, or concatenate them if they are castable as strings.我需要从可以是任何数据类型的元组中读取值,然后如果它们是数字就添加它们,或者如果它们可转换为字符串则将它们连接起来。

tuple<int32_t, bool, string, float, const char*, char, int> t{10, true, "Modern", 2.3f, "C++", 'e', 13}; // Example Tuple

So I thought a template would be the way to go but I get a compilation error even though it should not be possible for the situation to happen where a number and a const char* are added.因此,我认为模板将是可行的方法,但即使添加数字和 const char* 的情况也不可能发生,但我收到了编译错误。

no match for 'operator+=' (operand types are 'long double' and 'std::__cxx11::basic_string<char>')
   22 |         numericSum += tupleValue;
      |         ~~~~~~~~~~~^~~~~~~~~~~~~
error:   in evaluation of 'operator+=(long double, const char*)'

So what could be a way to modify my code to work around this problem?那么有什么方法可以修改我的代码来解决这个问题呢? Is there an easy way to detect chars and strings similarly to is_arithmetic?是否有一种类似于 is_arithmetic 的简单方法来检测字符和字符串?

// Based on the datatype of the tupleValue, we either add, concatenate or ignore the value
template<typename T>
void interact_with_tuple(T tupleValue, long double &numericSum, string &stringConcatenation, int &argumentsIgnored){
    // Check for chars
    if (is_convertible<T*, string>::value){
        stringConcatenation += tupleValue;
    }
    // Check for strings and char pointers
    else if (is_convertible<T, string>::value){
        stringConcatenation += tupleValue;
    }
    // Check for integers and floating point numbers but exclude bools
    else if (is_arithmetic<T>::value && !is_same<T, bool>::value){
        numericSum += tupleValue;
    }   
    else{
        argumentsIgnored += 1;
    }

    cout << tupleValue << endl;
}

Code from main function:主函数中的代码:

int main(){
    tuple<int32_t, bool, string, float, int> t{10, true, "Modern", 2.3f, 13}; // Example Tuple

    long double NumericSum = 0.0;
    string stringConcatenation = "";
    int argumentsIgnored = 0; // Im going to assume I don't need a long long for this

    // Iterate through Tuple
    apply([&](auto&&... args){((interact_with_tuple(args, NumericSum, stringConcatenation, argumentsIgnored)), ...);}, t); 

    cout << "NumericSum: " << NumericSum << endl;
    cout << "stringConcatenation: " << stringConcatenation << endl;
    cout << "argumentsIgnored: " << argumentsIgnored << endl;
}

You can use the C++17' is new compile-time conditional if constexpr .您可以使用 C++17' is new compile-time conditional if constexpr It will compile only the chunk that you need and discard the rest.它只会编译您需要的块并丢弃其余的块。

if constexpr (std::is_convertible<T*, std::string>::value){
    stringConcatenation += tupleValue;
}
// Check for strings and char pointers
else if constexpr (std::is_convertible<T, std::string>::value){
    stringConcatenation += tupleValue;
}
// Check for integers and floating point numbers but exclude bools
else if constexpr (std::is_arithmetic<T>::value && !std::is_same<T, bool>::value){
    numericSum += tupleValue;
}   
else{
    argumentsIgnored += 1;
}

Live demo: https://godbolt.org/z/rff88hM8r现场演示: https : //godbolt.org/z/rff88hM8r

暂无
暂无

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

相关问题 'operator*' 不匹配(操作数类型为 'const string' {aka 'const std::__cxx11::basic_string<char> '})</char> - no match for 'operator*' (operand type is 'const string' {aka 'const std::__cxx11::basic_string<char>'}) 我该如何解决? (错误:'operator==' 不匹配(操作数类型是 'std::string' {aka 'std::__cxx11::basic_string'} 和 'int') - How do I fix this? (error: no match for ‘operator==’ (operand types are ‘std::string’ {aka ‘std::__cxx11::basic_string’} and ‘int’) 错误:'operator+' 不匹配(操作数类型为 'std::__cxx11::list<int> ' 和 'int')|</int> - Error: no match for 'operator+' (operand types are 'std::__cxx11::list<int>' and 'int')| 错误:为&#39;operator std::string {aka std::__cxx11::basic_string 指定的返回类型<char> }&#39; - Error:return type specified for 'operator std::string {aka std::__cxx11::basic_string<char>}' 错误:无法从“向量”转换“标签” <std::vector<std::__cxx11::basic_string<char> &gt;&gt;' 到 ' 向量<std::__cxx11::basic_string<char> &gt;' </std::__cxx11::basic_string<char></std::vector<std::__cxx11::basic_string<char> - error: could not convert 'tab' from 'vector<std::vector<std::__cxx11::basic_string<char> >>' to 'vector<std::__cxx11::basic_string<char>>' 错误:没有匹配的函数调用&#39;std :: map <std::__cxx11::basic_string<char> - error: no matching function for call to ‘std::map<std::__cxx11::basic_string<char> 错误:与“ operator *”不匹配(操作数类型为“ std::string {aka std basic_string <char> }&#39;和{aka std basic_string <char> }&#39;) - error: no match for 'operator*' (operand types are 'std: :string {aka std basic_string<char>}' and {aka std basic_string<char>}') 错误:无法转换 'std::__cxx11::basic_string<char> ::迭代器'</char> - error: cannot convert ‘std::__cxx11::basic_string<char>::iterator’ 错误:传递&#39;const字符串{aka const std :: __ cxx11 :: basic_string <char> }&#39;作为&#39;this&#39;参数 - Error: passing ‘const string {aka const std::__cxx11::basic_string<char>}’ as ‘this’ argument 错误:没有匹配的函数调用&#39;std::__cxx11::basic_string<char> ::basic_string(int&amp;)&#39; - error: no matching function for call to ‘std::__cxx11::basic_string<char>::basic_string(int&)’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM