Member function call in decltype

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

    result_of and decltype in combination can give the the return type for a member function

    #include 
    using namespace std;
    
    struct A
    {
        int f(int i) { return i; } 
        auto g(int x) -> std::result_of::type
        { 
            return x;
        }
    };
    
    
    int main() {
        A a;
    static_assert(std::is_same::value, 
                      "should be identical");
    return 0;
    }
    

提交回复
热议问题