Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

c++ - decltype as a return type in class member function

I got error compiling below code.

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

struct A {
    auto func() -> decltype(b())
    {
        return b();
    }

    B b;
};

However, if I reorganize the A, it compiles.

gcc 4.8 said that 'b' was not declared in this scope.

struct A {
    B b;
    auto func() -> decltype(b())
    {
        return b();
    }
};

So, what is wrong with the first??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Is it valid?

Your last example is well-formed, while the first one is not (so GCC is correct).

Paragraph 3.4.1/7 on unqualified name lookup specifies:

A name used in the definition of a class X outside of a member function body, default argument, brace-or- equal-initializer of a non-static data member, 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

— [...]

And what follows are other conditions that do not apply in your case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...