Forcing a function to only accept a certain set of data types
is there any way to force functions to only take in vectors of integers (int, unsigned int, uint32_t, etc.) and only those? im trying to write a simple function that returns the sum of all the values with the same return type (since i cant be s开发者_运维百科ure whether or not the value will be greater than (2^32 - 1). however, since std::vector<T>
doesnt have a cout
operator for the entire type, i cannot do sum(vector<vector<T> >)
, since it will return a vector (ignoring the fact that vector + vector doesnt work). i do not wish to overload every type to cout something because i wont be needing it. i just want the function to work when T is some form of an int (and float if possible)
ive tried using try/except
, but codeblocks catches the operators of the types, so i cant compile if i do a sum(vector <vector <T> >)
template <typename T>
T sum(std::vector <T> in){
try{
T total = 0;
for(int x = 0; x < in.size(); x++)
total += in[x];
return total;
}
catch (int e){
std::cout << "Error " << e << " has occured." << std::endl;
exit(e);
}
}
template <typename T>
typename std::enable_if<
std::is_integral<typename std::remove_reference<T>::type>::value,
T>::type
sum(const std::vector<T>& in) { return std::accumulate(in.begin(), in.end(), 0); }
SFINAE to the rescue.
#include <type_traits>
//#include <tr1/type_traits> // for C++03, use std::tr1::
template<bool, class T = void>
struct enable_if{};
template<class T>
struct enable_if<true,T>{
typedef T type;
};
template<class T>
typename enable_if<
std::is_arithmetic<T>::value,
T
>::type sum(std::vector <T> in){
T total = 0;
for(int x = 0; x < in.size(); x++)
total += in[x];
return total;
}
精彩评论