Hey guys, I am completely new to C++ and have been trying to learn as I solve an assignment problem. The basis of it is that the program iterates through a tuple and prints the sum of all the numeric entries, along with the concatenation of all entries that can be cast to a string. I'm having an issue with the summing part because the specific element is being recognized as an int, but then when I try adding it to the running total it throws a "error: no match for ‘operator+=’ (operand types are ‘float’ and ‘std::__cxx11::basic_string<char>’)". I've tried casting from a string to an int, but this gives an error saying I cannot cast from int to int. Any help would be much appreciated!
The problem is that when, for example, the template is instantiated for a std::string, the line "total += a" still exists, and is an error. Maybe you should use template specialization instead.
You're welcome. :-)
Some other points:
* You should include <typeinfo> to use typeid.
* You should probably compare typeid(a).name() or typeid(a).hash_code() instead of the std::type_info objects themselves.
* You use int32_t in your tuple definition but int in the typeid(). You should use the same type name in both places.
* You should pass the tuples as references instead of copies.
* You can remove tup from the version that just returns. This eliminates a possible compiler warning.
std::typeinfo is EqualityComparable, it can be used to check if two types referred to are the same.
There are no guarantees about the what std::type_info::name() yields;
"in particular, the returned string can be identical for several types and change between invocations of the same program." https://en.cppreference.com/w/cpp/types/type_info/name