This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Function return type depends on its recursive invocation


Hello,
I was experimenting with trailer function return types together with decltype to define a function whose return type depends on the recursive invocation of the function itself.


The basic idea is to be able to define a meta-function which takes a variable number of arguments and arranges the elements into a tree, for example:

makeTree(int,bool,std::string) => std::pair<int,std::pair<bool, std::string>>

Because the return type depends on the number and type of the function parameters, I defined the function in the following way:

template <typename Arg1, typename Arg2, typename Arg3, typename... Args>
auto makeTree(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Args&... args) ->
std::pair<Arg1,decltype(makeTree(arg2,arg3,args...))>
{
return {arg1, makeTree(arg2,arg3,args...)};
}


// termination case which takes 2 arguments
template <typename LhsTy, typename RhsTy>
std::pair<LhsTy,RhsTy> makeTree(const LhsTy& lhs, const RhsTy& rhs) {
return {lhs, rhs};
}

I am using GCC 4.7.1. The code compiles when I invoke the makeTree with 2 and 3 arguments, however it fails when I go beyond 3 with the following error message:

test.cpp: In function ‘int main(int, char**)’:
test.cpp:26:22: error: no matching function for call to ‘makeTree(int, int, int, int)’
test.cpp:26:22: note: candidates are:
test.cpp:8:24: note: template<class LhsTy, class RhsTy> std::pair<_T1, _T2> makeTree(const LhsTy&, const RhsTy&)
test.cpp:8:24: note: template argument deduction/substitution failed:
test.cpp:26:22: note: candidate expects 2 arguments, 4 provided
test.cpp:13:6: note: template<class Arg1, class Arg2, class Arg3, class ... Args> std::pair<Arg1, decltype (makeTree(arg2, arg3, makeTree::args ...))> makeTree(const Arg1&, const Arg2&, const Arg3&, const Args& ...)
test.cpp:13:6: note: template argument deduction/substitution failed:
test.cpp: In substitution of ‘template<class Arg1, class Arg2, class Arg3, class ... Args> std::pair<Arg1, decltype (makeTree(arg2, arg3, args ...))> makeTree(const Arg1&, const Arg2&, const Arg3&, const Args& ...) [with Arg1 = int; Arg2 = int; Arg3 = int; Args = {int}]’:
test.cpp:26:22: required from here
test.cpp:13:6: error: no matching function for call to ‘makeTree(const int&, const int&, const int&)’
test.cpp:13:6: note: candidate is:
test.cpp:8:24: note: template<class LhsTy, class RhsTy> std::pair<_T1, _T2> makeTree(const LhsTy&, const RhsTy&)
test.cpp:8:24: note: template argument deduction/substitution failed:
test.cpp:13:6: note: candidate expects 2 arguments, 3 provided



I wonder if this is a limitation of GCC or this kind of usage of decltype is not compliant with the C++11 standard.


More details are available on the following link: http://cpplove.blogspot.co.at/2012/07/decltype-insanity-aka-when-return-type.html

thanks for your time,

Simone Pellegrini


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]