lundi 20 août 2018

private method as trailing return type (decltype)

When I am trying to use decltype() on a private method function I get the error that the private method error: 'm1' has not been declared in this scope

#include <stdint.h>

class C
{
public:
    C()=default;
    ~C()=default;

    auto masterMethod(int opMode) ->decltype(m1())
    {
        switch(opMode)
        {
        case 1:
                return m1(); break;
        case 2:
                return m2(); break;
        default:
                return m1(); break;
        }
    }
private:
    int m1() {return 1;}
    int m2() {return 2;}
};

Now my question is, why the compiler does not lookup in the private section of the class, because removing the trailing return type or putting the private section on top of masterMethod solves the problem (decltype(auto) [in case C++14 is allowed] would be also correct with its automatic return type deduction).

Furthermore, is it bad behaviour when removing the decltype(m1())when m1() and m2() have the same return-type, as this would do it for me too?

Aucun commentaire:

Enregistrer un commentaire