return value type auto deduction in static member function fails

Viewed 66

I have this code

struct Foo {

    static constexpr auto get() { return static_cast<int>(3); }

    Foo(int t = get()) {}
};

int main() {
    Foo t;
}

That fails to compile with clang++ with the following error

test.cpp:7:17: error: function 'get' with deduced return type cannot be used before it is defined
    Foo(int t = get()) {}
                ^
test.cpp:5:27: note: 'get' declared here
    static constexpr auto get() { return static_cast<int>(3); }
                          ^
1 error generated.

The error looks like Use of 'auto func(int)' before deduction of 'auto' in C++14 but in my case, the function is defined before... Moving the function as is outside the class allows successful compilation (which is strange to me because I don't really see the difference).

I tried with Gcc and it complains too.

Why would the use of auto here be illformed?

0 Answers
Related