decltype as a return type in class member function

后端 未结 3 1312
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 18:42

I got error compiling below code.

struct B{
    double operator()(){
        return 1.0;
    }
};

struct A {
    auto func() -> decltype(b())
    {
             


        
3条回答
  •  一个人的身影
    2020-12-01 19:18

    You can also get it to work like this:

    struct B
    {
        double operator()()
        {
            return 1.0;
        }
    };
    
    // my implementation does not have std::declval
    template < typename T > T&& declval();
    
    struct A
    {
        B b;
        auto func() -> decltype(declval().operator()())
        {
            return b();
        }
    };
    

    edit: or since B is in scope already anyway no need for auto, -> decltype and declval

    struct A
    {
        B b;
        decltype(Q()()) func()
        {
            return b();
        }
    };
    

提交回复
热议问题