Recursive use of noexcept operator compiles in gcc but not in clang and msvc
Asked Answered
F

1

7

I am learning C++ using the books listed here. I wrote the following example(for purely academic reasons) that compiles with GCC but not with Clang and MSVC. Demo.

struct C {
    static bool f() noexcept(!noexcept(C::f())) 
    {
        return true;
    }
};

As we can see here the above example compiles with gcc but not with msvc and clang.

So my question is which compiler is right here(if any).


GCC compiles this.

MSVC says:

<source>(2): fatal error C1202: recursive type or function dependency context too complex

Clang says:

<source>:2:40: error: exception specification is not available until end of class definition
    static bool f() noexcept(!noexcept(C::f())) 
Footmark answered 10/10, 2022 at 12:47 Comment(16)
MSVC <source>(6): fatal error C1202: recursive type or function dependency context too complex - I think this is self explanatory. That gcc compiles this means it can predict the future. +1 for compiler differences.Checkerbloom
@NathanOliver This particular example is not from any book. It's just that I have read about noexcept in books.Footmark
Then where did you encounter "I also came across the following example..."?Milinda
Print the shown code, frame it, hang it in the Louvre.Felecia
The downvote reasons include "not useful". Like it or not, people are allowed to vote on their idea of usefulness, and comments whinging about it aren't encouraged. Language lawyer questions typically fly by despite contrived code (because reproducibility is important). But even in LL land, this example is pretty far from being a reduction of a genuine problem. Intentionally self-contradictory code, really?!Adest
Count me among the several commenters who would like to know which book this example code came from. As mentioned, it's contradictory. C::f() is noexcept only when C::f() is not noexcept.Mousse
Post-edit, I would venture that gcc is wrong, and someone with the time should post a bug report with this exact code example.Mousse
@Ronald: "I am learning C++ using the books listed here." Is there a reason why you feel the need to say this in every question you write here? It's just not important; unless your code came from a book on that list (in which case, you should cite the book, not the question), it's not something we need to know. Also, "language-lawyer" questions are for interrogating the standard, not arbitrary C++ books.Chavarria
@NicolBolas Yes it is very important for me to write that i am learning using books in each of my SO questions because I've seen people on SO saying things like "you should use books to learn C++ instead of by writing random code". People on SO object to anything these days. Even to mentioning that i am using books.Footmark
@Ronald: You seem to be side-stepping the actual criticism in that statement, ignoring the key phrase "instead of." That is, "guess and check" learning of C++ isn't better because you can say "I saw something in a book" beforehand. The point of the criticism is to stop you from trying to learn C++ via "guess and check".Chavarria
At a guess, this is ill-formed, no-diagnostic required, so technically no-one is wrongYseult
@Ronald: Basically, my issue with this question is this: let's say that your recursive, unanswerable code actually did have an answer somewhere in the bowels of the standard. Maybe it just says somewhere, "if a noexcept expression uses a function currently being defined, the answer is always true)." What would you do with that information? Being able to point at a line in the standard that give a nonsense code some behavior doesn't make the code any better. It's code that nobody should ever write. What does it matter if the standard might somehow define it?Chavarria
Interesting effects if you make it mutually recursiveYseult
Because the code compiles in gcc and could be accidently created (in a real world example by simple typo) I think (a) it's worth reporting and (b) what is the actual function signature that gcc thinks f has.Checkerbloom
@Ronald that is correct, you don't have to list the books you've read in order to ask a question here. Your decision to list books and say that you "came across" this code while "using" the books is dishonest, and calling us "toxic" while trying to deceive us may not lead to the results you are trying to get.Mousse
@DrewDormann There is nothing deceptive in my question. It clearly shows the code that that reproduces the problem. The question is about the code and not about the books that i am using. Also note that i've long edited my question to say that i've written this example instead of "came across".Footmark
C
0

So my question is which compiler is right here(if any).

Clang and MSVC are correct here in rejecting the program with contradictory circular noexcept-specifier.

GCC bug report: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107256

Note that it is a regression in recent GCC, and GCC 9.5 also rejected the program: https://gcc.godbolt.org/z/MqeazGx93

Coincidentally answered 7/7 at 19:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.