Reputation: 354
I'd like to have different variable value depending on type of input variable. Code:
template <typename T>
int getValue(vector<T> & data)
{
return something; // There should be 0 for int and 1 for double
}
Do anyone know how to achieve such a functionality?
Upvotes: 4
Views: 422
Reputation: 380
Two important operators to type manipulation is typeid and decltype.
typeid return a object type_info with type information.
Some ways of verify types is:
If you are using c++11 the better option should be std::is_same with a delctype (detects the type of variable), because it's compile time resolution.
vector<int> integers;
vector<double> doubles;
cout << is_same<vector<int>, decltype(integers)>::value << endl;
cout << is_same<vector<int>, decltype(doubles)>::value << endl;
cout << is_same<vector<double>, decltype(integers)>::value << endl;
cout << is_same<vector<double>, decltype(doubles)>::value << endl;
If you are using standard C++ (c++98), you can use typeid operator
vector<int> vectorInt;
vector<int> integers;
vector<double> doubles;
cout << ( typeid(integers) == typeid(vectorInt) ) << endl;
cout << ( typeid(doubles) == typeid(vectorInt) ) << endl;
You can use function overload and templates to resolve this types without unexpected broke.
At this way do you need write a function to each type to identify or the template function will return -1 (unknow) like identification.
template<typename T>
int getTypeId(T) {
return -1;
}
int getTypeId(vector<int>) {
return 1;
}
main() {
vector<int> integers;
vector<double> doubles;
vector<char> chars;
cout << getTypeId(integers) << endl;
cout << getTypeId(doubles) << endl;
cout << getTypeId(chars) << endl;
}
Upvotes: 0
Reputation: 180805
If you are just dealing with an int
and double
then you could just overload the function for the differnt types of vectors.
int getValue(vector<int> & data)
{
return 0;
}
int getValue(vector<double> & data)
{
return 1;
}
If you want to keep getValue
as a template function and specialize for int
and double
then you could use
template<typename T>
int getValue(std::vector<T> & data)
{
return -1;
}
template <>
int getValue(std::vector<int> & data)
{
return 0;
}
template <>
int getValue(std::vector<double> & data)
{
return 1;
}
Upvotes: 6
Reputation: 3249
You can provide non-template overloads while keeping the template. Since function resolution prefers non-template matches over templates. Example:
template <typename T>
int getValue( std::vector<T> & data )
{
return -1; // default template
}
int getValue( std::vector<int> & data )
{
return 0; // int overload
}
int getValue( std::vector<double> & data )
{
return 1; // double overload
}
Here's an example using specialization:
template <typename T>
int getValue( std::vector<T> & data )
{
return -1; // default template
}
template <>
int getValue<int>( std::vector<int> & data )
{
return 0; // int specialization
}
template <>
int getValue<double>( std::vector<double> & data )
{
return 1; // double specialization
}
Upvotes: 2