views:

106

answers:

2

I'm trying to create a set of overloaded templates for arrays/pointers where one template will be used when the compiler knows the size of the array and the other template will be used when it doesn't:

template <typename T, size_t SZ>
void moo(T (&arr)[SZ])
{ ... }

template <typename T>
void moo(T *ptr)
{ ... }

The problem is that when the compiler knows the size of the array, the overloads are ambiguous and the compile fails.

Is there some way to resolve the ambiguity (perhaps via SFINAE) or is this just not possible.

+4  A: 

It is possible as it can be determined wether a template parameter is an array or not:

template<class T> struct is_array {
    enum { value = false }; 
};
template<class T, size_t N> struct is_array<T[N]> {
   enum { value = true }; 
};
template<class T> void f(T const&) { 
    std::cout << is_array<T>::value << std::endl;
}

Combining that with enable_if, the above can be made unambiguous. For example using Boost.TypeTraits:

template <typename T, size_t SZ>
typename boost::enable_if<boost::is_array<T>, void>::type
f(T (&arr)[SZ]) {}

With references however there is no need for SFINAE at all:

template<class T, size_t SZ> void f(T (&arr)[SZ]) {}
template<class T>            void f(T* const& t)  {}

Johannes brings up another option that fits the situation at hand better - using SFINAE for the problematic pointer overload instead:

template <typename T, size_t SZ> void f(T (&arr)[SZ]) {}
template <typename T>
typename boost::enable_if<boost::is_pointer<T>, void>::type
f(T ptr) {}
Georg Fritzsche
Johannes Schaub - litb
@Johannes: I guess i tend to overlook rvalues until they hit me. As usual, thanks for the input :)
Georg Fritzsche
A: 

You can call the function explicitly:

int a[1] = {0}
moo<int,1>(a);

Or you can overload on const:

template<class T>
void moo(T const* p) { }

// ...
moo(a); // not const, array

int* p = 0;
moo(p); // pointer
wilhelmtell