The fallowing code works with gcc 4.7. The idea is i have these generic functions, which work on sequences, pointers, tupples, pairs, user-defined types, and whatnot. If one of these functions is defined for a type, then all should be. The problem i'm having is determining how to specialize them. I decided to define a template class that would be specialized for each type, implementing each function, and then a free function that just forwards to the in-class implementation.
#include <utility>
#include <vector>
#include <iterator>
#include <memory>
#include <iostream>
#include <algorithm>
using namespace std;
template< class M > struct Mon;
template< class X, class F,
class M = Mon< typename decay<X>::type > >
auto mon( X&& x, F f ) -> decltype( M::mon(declval<X>(),f) ) {
return M::mon( forward<X>(x), f );
}
template< class C > struct IsSeqImpl {
// Can only be supported on STL-like sequence types, not pointers.
template< class _C > static true_type f(typename _C::iterator*);
template< class _C > static false_type f(...);
typedef decltype( f<C>(0) ) type;
};
template< class C > struct IsSeq : public IsSeqImpl<C>::type { };
/* Enable if is an STL-like sequence. */
template< class C, class R > struct ESeq : std::enable_if<IsSeq<C>::value,R> { };
template< class Seq >
struct Mon : ESeq< Seq, Seq >::type
{
template< class S, class F >
static S mon( const S& s, F f ) {
S r;
transform( begin(s), end(s), back_inserter(r), f );
return r;
}
};
template< class P > struct IsPtrImpl {
template< class X > static true_type f( X );
template< class X > static false_type f( ... );
typedef decltype( f(*declval<P>()) ) type;
};
template< class P > struct IsPtr : public IsPtrImpl<P>::type { };
template< class P, class R > struct EPtr : enable_if<IsPtr<P>::value,R> { };
template< class X > struct Mon< X* >
{
template< class F, class R = decltype( declval<F>()(declval<X>()) ) >
static unique_ptr<R> mon( X* x, F f ) {
typedef unique_ptr<R> U;
return x ? U( new R(f(*x)) ) : U(nullptr);
}
};
int add_one( int x ) { return x + 1; }
int main()
{
vector<int> v = {1,2,3,4,5};
int x = 5;
auto v2 = mon( v, add_one );
auto x2 = mon( &x, add_one );
// Should print 2 and 6.
cout << v2[0] << '\n';
cout << *x2 << '\n';
}
What i'd like to do is specialize Mon for more generic types but when i try to use the enable_if inheritance trick again, gcc complains Mon is already defined. I've also tried the technique of making the second template argument a true_ or false_type for SFINAE as mentioned in this question, but had no luck in getting it to compile.
Ideally, whenever i think of a category of types i want to define an action for, i should be able to write an enable_if and write the entire group of functions in a template specialization. This saves the trouble of writing one enable_if per function. Pessimistically, i'd have to specialize the group for every plausible type in each category in order to really be generic.
Can i possibly write this in a generic and extensible way?
PS: If only concepts were a part of C++11.