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?