2

It is legal to throw exceptions in a ternary operator. However, this

struct uncopiable {
    uncopiable(const uncopiable&) = delete;
};

struct foo {
    uncopiable _bar;
    uncopiable& get_bar() {
        return true ? _bar : throw 0;
    }
};

fails to compile with GCC 9.3, with -std=c++14 -pedantic, as shown here in godbolt, complaining about the use of deleted copy operator. Similar errors are reported also in all the ICC versions, as well as in MSVC up to 19.21. GCC 10 and all Clang versions since 3.5 are fine.

Since there are no copies in the code, I suppose they are just compiler bugs fixed in the latest versions. Is the code legal?

Giovanni Cerretani
  • 1,693
  • 1
  • 16
  • 30
  • 1
    [Related question](https://stackoverflow.com/questions/62328409/unexpected-gcc-warning-function-returns-address-of-local-variable-is-it-a-com?noredirect=1#comment110234538_62328409). This is a bug in GCC 9, and is fixed in GCC 10. – IlCapitano Sep 15 '20 at 13:49
  • 2
    It compiles with a little trick: `return true ? _bar : (throw 0, _bar);` [Compiler Explorer](https://godbolt.org/z/ozq65s). – Scheff's Cat Sep 15 '20 at 13:54

0 Answers0