Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::declval vs crtp, cannot deduce method return type from incomplete type

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.

like image 976
463035818_is_not_a_number Avatar asked Jun 07 '19 16:06

463035818_is_not_a_number


1 Answers

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

like image 174
Vittorio Romeo Avatar answered Oct 19 '22 21:10

Vittorio Romeo