Member function call in decltype

前端 未结 6 1064
北荒
北荒 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:42

    Seems to me that does not work because the decltype is outside of the method and A is at that moment an incomplete type (so you can't even do A().f(x)).

    But you should not really need that. Outside of the declaration of A this will work as expected, in A you should know the return type of the function that you declared a few lines above. Or you could just write:

    struct A {
        typedef int ret_type;
        ret_type f(int x);
        ret_type g(int x);
    };
    

    This even works with plain c++03.

提交回复
热议问题