Reputation: 712
For below code:
template<class _InIt,
class _Ty,
class _Fn2> inline
_Ty accumulateSimplePtr(_InIt _First, _InIt _Last, _Ty _Val, _Fn2 _Func)
{ // return sum of _Val and all in [_First, _Last), using _Func
for (; _First != _Last; ++_First)
{
if(is_class<std::iterator_traits<_InIt>::value_type>::value)
_Val = _Func(_Val, (*_First)());
else
_Val=_Func(_Val, *_First);
}
return (_Val);
}
how can i tell if _InIt is an interator that is pointing to a class/struct or to a pod such as double?
If it is pointing to a double, I want to use *_First to get the data, and if it points to a class, I would use (*_First)() to get the actual data(assuming that the class has an overloaded operator().
I tried something as above, but it failed to compile with line " _Val=_Func(_Val, *_First);", saying that MyObj can't be convereted to double.
Upvotes: 0
Views: 416
Reputation: 15916
#include <iostream>
#include <type_traits>
#include <vector>
class A
{
public:
int a;
int b;
};
template<typename T>
struct IsClass
{
enum { Yes = std::is_class<T>::value };
enum { No = !Yes };
};
int main(int argc, char* argv[])
{
std::vector<int> v1;
std::vector<A> v2;
auto it1 = v1.begin();
auto it2 = v2.begin();
std::cout << IsClass<std::decay<decltype(it1)>::type::value_type>::Yes << std::endl;
std::cout << IsClass<std::decay<decltype(it2)>::type::value_type>::Yes << std::endl;
std::cin.get();
return 0;
}
The output for these the lines is 0 (false, for int vector) and 1 (true, for A vector).
Upvotes: 1