6

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())) 
Alex
  • 318
  • 1
  • 14
  • 4
    `MSVC (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. – Richard Critten Oct 10 '22 at 12:51
  • @NathanOliver This particular example is not from any book. It's just that I have read about noexcept in books. – Alex Oct 10 '22 at 12:53
  • 2
    Then where did you encounter "I also came across the following example..."? – Eljay Oct 10 '22 at 12:54
  • Print the shown code, frame it, hang it in the Louvre. – Sam Varshavchik Oct 10 '22 at 12:55
  • 7
    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?! – StoryTeller - Unslander Monica Oct 10 '22 at 13:22
  • 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. – Drew Dormann Oct 10 '22 at 13:26
  • 3
    Post-edit, I would venture that gcc is wrong, and someone with the time should post a bug report with this exact code example. – Drew Dormann Oct 10 '22 at 13:39
  • 3
    @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. – Nicol Bolas Oct 10 '22 at 14:02
  • @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. – Alex Oct 10 '22 at 14:09
  • 4
    @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". – Nicol Bolas Oct 10 '22 at 14:14
  • 2
    At a guess, this is ill-formed, no-diagnostic required, so technically no-one is wrong – Caleth Oct 10 '22 at 14:15
  • 5
    I think I understand. You link to a list of about 100 books that you are "using" to improve the reception of your questions, not because you are actually reading them? I think that tactic created more confusion here. You said "I also came across the following example" in your original edit of this question, which seems rather disingenuous, given that you are now saying that you authored the code. – Drew Dormann Oct 10 '22 at 14:15
  • 2
    @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? – Nicol Bolas Oct 10 '22 at 14:17
  • [Interesting effects if you make it mutually recursive](https://godbolt.org/z/v3PK73EeM) – Caleth Oct 10 '22 at 14:33
  • 2
    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. – Richard Critten Oct 10 '22 at 14:37
  • 3
    @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. – Drew Dormann Oct 10 '22 at 15:06
  • @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". – Alex Oct 10 '22 at 15:15

0 Answers0