decltype in class method declaration: error when used before “referenced” member is declared

前端 未结 1 957
眼角桃花
眼角桃花 2020-12-20 14:10

Consider the following code:

struct test {    
    auto func() -> decltype(data) {}  // ERROR

    int data;
};

int main() {
    test t;
    t.func();
}
         


        
相关标签:
1条回答
  • 2020-12-20 14:34

    The trailing return type is part of the member function declaration, which does not have access to data members or member functions declared after it, unlike the member function definition, which does. I am not aware of any change in this behaviour in C++14.

    See 3.4.1-7 of the C++11 standard, Unqualified name look-up:

    A name used in the definition of a class X outside of a member function body or nested class definition shall be declared in one of the following ways:

    • before its use in class X or be a member of a base class of X (10.2), or...

    (emphasis mine)

    0 讨论(0)
提交回复
热议问题