Why does GCC allow a deduced return type in this function template when MSVC and Clang don't?
Asked Answered
D

1

9

Code sample:

class A
{
    static constexpr auto GetInt() noexcept { return 6; }

    template<int N>
    std::enable_if_t< N >= GetInt(), int> func() { return N; }
};

https://godbolt.org/z/-0pwIQ

Clang and MSVC both claim that GetInt() can't be used because it's not defined at that point, however GCC compiles with no errors or warnings.

My best guess for why the error occurs is that because the class is incomplete at the point that func(), member functions are considered undefined, and because auto relies on the function definition to deduce the return type, the compiler can't use it to generate a function signature.

However, that doesn't explain why GCC is allowing it. Is it incorrect to do so?

Devlen answered 10/7, 2019 at 11:26 Comment(11)
I think the code is right.Baptista
Can't remember exactly but deduced return types don't fit well with SFINAE...Alurd
Function definitions are processed at the end of the class definition, thus when parsing the template the return type is actually not known...Alurd
@Jean-BaptisteYunès So surely GCC should reject it?Devlen
I am currently unable to find good references in the standard (lack of time to dig more actually)... Did you tried pedantic compilation mode or alike? gcc is sometimes too permissive.Alurd
It doesn't complain even with -Wall -Wextra -WpedanticDevlen
Yes I tried, also... Can't see either if it is correct or not in the standard (but the standard is not always clear :-). Sorry, need to leave, now!Alurd
possible dup to #41843487Pragmatics
Per Hui, possible duplicate of Why can't the type of my class-static auto function be deduced within the class scope?Shang
This is CWG2335.Shang
GCC 8.2 for ARM does compile the code. But ICC 19.0.1 reports some errors godbolt.org/z/V1ekvSPeri
M
0

As it is already mentioned in the comments, this is one of C++ Standard Core Language Active Issues: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#2335

It is still under discussion.

Though it does look like Clang/MSVC behavior might become standard:

Notes from the June, 2018 meeting: The consensus of CWG was to treat templates and classes the same by "instantiating" delayed-parse regions when they are needed instead of at the end of the class.

Mirilla answered 16/10, 2021 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.