[C++11] recursive late-specified return type

Nathan Ridge zeratul976@hotmail.com
Thu Nov 10 23:37:00 GMT 2011

> > 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).


More information about the Gcc-help mailing list