Variadic functions (no arguments!)

Suppose you want to do this in C ++ 0x:

size_t count_int() { return 0; }
template<typename T, typename... Tn>
size_t count_int(T a0, Tn... an) {
    size_t n = is_integer<T>::value ? 1 : 0;
    return n + count_int(an...);
}

Nice, but you don't need to get around the arguments. Unfortunately this does not work:

size_t count_int() { return 0; }
template<typename T, typename... Tn>
size_t count_int() {
    size_t n = is_integer<T>::value ? 1 : 0;
    return n + count_int<Tn...>();
}

GCC complains about the error: there is no corresponding function to call the count_int () function on the next line. Why and how can this be fixed? Thank.

+5
source share
3 answers

This one works :

template <typename T>
size_t count_int()
{
   return is_integer<T>::value ? 1 : 0;
}

template<typename T, typename T1, typename... Tn>
size_t count_int() {
    size_t n = is_integer<T>::value ? 1 : 0;
    return n + count_int<T1,Tn...>();
};
+3
source

, , , count_int(), , <Tn...>, Tn . . , , .

+1

, , , count_int<Tn...>(); Tn, .

:

template <typename...>
size_t count_int() { return 0; }

, , , .

, . - ( )

template <typename T, typename... Tn>
struct int_counter {
    enum { value = is_integer<T>::value + int_counter<Tn...>::value; }
};

template <>
struct int_counter<> {
    enum { value = 0; }
};

template <typename... Tn>
size_t count_int() { 
    return int_counter<Tn>::value;
}
+1
source

All Articles