I think the problem is that the variadic function template is only considered declared after you specified its return type so that sum
in decltype
can never refer to the variadic function template itself. But I'm not sure whether this is a GCC bug or C++0x simply doesn't allow this. My guess is that C++0x doesn't allow a "recursive" call in the ->decltype(expr)
part.
As a workaround we can avoid this "recursive" call in ->decltype(expr)
with a custom traits class:
#include <iostream>
#include <type_traits>
using namespace std;
template<class T> typename std::add_rvalue_reference<T>::type val();
template<class T> struct id{typedef T type;};
template<class T, class... P> struct sum_type;
template<class T> struct sum_type<T> : id<T> {};
template<class T, class U, class... P> struct sum_type<T,U,P...>
: sum_type< decltype( val<const T&>() + val<const U&>() ), P... > {};
This way, we can replace decltype
in your program with typename sum_type<T,P...>::type
and it will compile.
Edit: Since this actually returns decltype((a+b)+c)
instead of decltype(a+(b+c))
which would be closer to how you use addition, you could replace the last specialization with this:
template<class T, class U, class... P> struct sum_type<T,U,P...>
: id<decltype(
val<T>()
+ val<typename sum_type<U,P...>::type>()
)>{};
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…