public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* [C++11] recursive late-specified return type
@ 2011-11-07  7:41 Nathan Ridge
  2011-11-07  9:21 ` Miles Bader
  0 siblings, 1 reply; 3+ messages in thread
From: Nathan Ridge @ 2011-11-07  7:41 UTC (permalink / raw)
  To: GCC Help Mailing List


Hi,

Could someone please clarify whether the following code (from [1]),
which GCC trunk rejects, is valid (i.e. the error is a bug) or invalid?

#include <iostream>
using namespace std;

template <class T>
T sum(const T& in)
{
   return in;
}

template <class T, class... P>
auto sum(const T& t, const P&... p) -> decltype(t + sum(p...))
{
   return t + sum(p...);
}

int main()
{
   cout << sum(5, 10.0, 22.2) << endl;
}

Errors:

test.cpp: In function 'int main()':
test.cpp:18:29: error: no matching function for call to 'sum(int, double, double)'
test.cpp:18:29: note: candidates are:
test.cpp:5:3: note: template<class T> T sum(const T&)
test.cpp:5:3: note:   template argument deduction/substitution failed:
test.cpp:18:29: note:   candidate expects 1 argument, 3 provided
test.cpp:11:6: note: template<class T, class ... P> decltype ((t + sum(sum::p ...))) sum(const T&, const P& ...)
test.cpp:11:6: note:   template argument deduction/substitution failed:
test.cpp: In substitution of 'template<class T, class ... P> decltype ((t + sum(p ...))) sum(const T&, const P& ...) [with T = int; P = {double, double}]':
test.cpp:18:29:   required from here
test.cpp:11:6: error: no matching function for call to 'sum(const double&, const double&)'
test.cpp:11:6: note: candidate is:
test.cpp:5:3: note: template<class T> T sum(const T&)
test.cpp:5:3: note:   template argument deduction/substitution failed:
test.cpp:11:6: note:   candidate expects 1 argument, 2 provided

Thanks,
Nate

[1] http://stackoverflow.com/questions/8023332/recursive-trailing-return-type)


 		 	   		  

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [C++11] recursive late-specified return type
  2011-11-07  7:41 [C++11] recursive late-specified return type Nathan Ridge
@ 2011-11-07  9:21 ` Miles Bader
  2011-11-10 23:37   ` Nathan Ridge
  0 siblings, 1 reply; 3+ messages in thread
From: Miles Bader @ 2011-11-07  9:21 UTC (permalink / raw)
  To: Nathan Ridge; +Cc: GCC Help Mailing List

Nathan Ridge <zeratul976@hotmail.com> writes:
> Could someone please clarify whether the following code (from [1]),
> which GCC trunk rejects, is valid (i.e. the error is a bug) or invalid?

I can't answer this question, but it will compile if you use
std::common_type<>, from <type_traits> to calculate the return
type.... e.g.:

   #include <type_traits>
   ...
   template <class T, class... P>
   auto sum(const T& t, const P&... p) -> typename common_type<T, P...>::type
   {
     return t + sum(p...);
   }

I can imagine that the reason your version might not compile is
because you're trying to use the multi-parameter version of sum, in
"decltype (t + sum (p...))" before the multi-parameter version of sum
has been fully declared (as the decltype is part of that declaration);
I dunno whether the compiler is supposed to handle that or not...

-Miles

-- 
Corporation, n. An ingenious device for obtaining individual profit without
individual responsibility.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: [C++11] recursive late-specified return type
  2011-11-07  9:21 ` Miles Bader
@ 2011-11-10 23:37   ` Nathan Ridge
  0 siblings, 0 replies; 3+ messages in thread
From: Nathan Ridge @ 2011-11-10 23:37 UTC (permalink / raw)
  To: miles, GCC Help Mailing List


> > Could someone please clarify whether the following code (from [1]),
> > which GCC trunk rejects, is valid (i.e. the error is a bug) or invalid?
>
> I can't answer this question, but it will compile if you use
> std::common_type<>, from <type_traits> to calculate the return
> type.... e.g.:
>
> [snip]
>
> I can imagine that the reason your version might not compile is
> because you're trying to use the multi-parameter version of sum, in
> "decltype (t + sum (p...))" before the multi-parameter version of sum
> has been fully declared (as the decltype is part of that declaration);
> I dunno whether the compiler is supposed to handle that or not...

I am aware that there are workarounds. I would like to know whether
my version is valid C++11 or not.

The use case that made me search for this issue (and thus find that
example on stackoverflow) was the following:


template <typename> class foo {};

template <int N>
auto f() -> foo<decltype(f<N - 1>())>;

template <>
int f<0>();


The idea here is that f<0>() returns int, f<1>() returns foo<int>,
f<2>() returns foo<foo<int>>, etc.

The errors here are different, so I'm not sure if this is the exact
same issue:

test.cpp:4:26: error: 'f' was not declared in this scope
test.cpp:4:26: error: 'f' was not declared in this scope
test.cpp:4:37: error: template argument 1 is invalid
test.cpp:7:6: error: expected initializer before '<' token

Does anyone know whether this should be valid? The workaround
for this is more complicated (you need a separate metafunction
that computes the return type).

Thanks,
Nate
 		 	   		  

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-11-10 22:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-11-07  7:41 [C++11] recursive late-specified return type Nathan Ridge
2011-11-07  9:21 ` Miles Bader
2011-11-10 23:37   ` Nathan Ridge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).