[英]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.