I am trying to do something like this (in c++11):
#include <utility>
template <typename T>
struct base {
using type = decltype( std::declval<T>().foo() );
};
struct bar : base<bar> {
int foo() { return 42;}
};
int main() {
bar::type x;
}
which fails with
prog.cc: In instantiation of 'struct base<bar>':
prog.cc:8:14: required from here
prog.cc:5:46: error: invalid use of incomplete type 'struct bar'
using type = decltype( std::declval<T>().foo() );
~~~~~~~~~~~~~~~~~~^~~
prog.cc:8:8: note: forward declaration of 'struct bar'
struct bar : base<bar> {
^~~
How can I declare an alias to the return type of bar::foo
in base
? Is it not possible?
This question seems to be rather related: Special behavior for decltype of call operator for incomplete types, though I couldnt manage to apply the answer given there to my case.
You can make type
a template type alias, so that users can instantiate it after the definition of bar
is available. This will change the final syntax from bar::type
to bar::type<>
.
template <typename T>
struct base {
template <typename G = T>
using type = decltype( std::declval<G>().foo() );
};
struct bar : base<bar> {
int foo() { return 42;}
};
int main() {
bar::type<> x;
}
live example on godbolt.org
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With