Member function call in decltype

前端 未结 6 1067
北荒
北荒 2021-02-05 09:23

The following code:

struct A
{
    int f(int);
    auto g(int x) -> decltype(f(x));
};

Fails to compile with the error:

erro         


        
6条回答
  •  野性不改
    2021-02-05 09:37

    Comeau doesn't like auto as a top level return type, but the following compiles successfully:

    template  R get_return_type(R (C::*)(A1));
    
    struct A
    {
        int f(int);
        decltype(get_return_type(&A::f)) g(int x);
    };
    

    Basically, you have to declare at least one additional construct that gets you the type you want. And use decltype directly.

    EDIT: Incidentally, this works fine for diving into the return type of a member function as well:

    template  R get_return_type(R (C::*)(A1));
    
    struct B { int f(int); };
    
    struct A
    {
        int f(int);
        B h(int);
    
        decltype(get_return_type(&A::f)) g(int x);
    
        decltype(get_return_type(&A::h).f(0)) k(int x);
    };
    
    int main()
    {
        return A().k(0);
    }
    

    Granted, it doesn't have the same convenience of auto f()-> ..., but at least it compiles.

提交回复
热议问题