These kinds of things are handled by templates. Runtime type information will be completely useless for this purpose.
For example, a function to turn a vector into a string representation might look like this:
Code:
#include <iostream>
#include <sstream>
#include <vector>
template <class T>
std::string to_string(const std::vector<T>& vec)
{
std::stringstream result;
result << "(";
if (!vec.empty()) {
result << vec[0];
for (std::size_t i = 1; i != vec.size(); ++i) {
result << ", " << vec[i];
}
}
result << ")";
return result.str();
}
int main()
{
std::vector<int> vec;
std::cout << to_string(vec) << '\n'; // ()
vec.push_back(1);
std::cout << to_string(vec) << '\n'; // (1)
std::vector<std::string> string_vec;
string_vec.push_back("Hello");
string_vec.push_back("world");
std::cout << to_string(string_vec) << '\n'; //(Hello, world)
}
This relies on ostream operator<< being overloaded for the type T (the standard library way to convert something to its string representation).
If you need to use a variable of type T, then you know what it is: T.
Code:
template <class T>
void foo(const std::vector<T>& vec)
{
T first = vec.front();
...
}