https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64372

Justin Bassett <jbassett271 at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jbassett271 at gmail dot com

--- Comment #13 from Justin Bassett <jbassett271 at gmail dot com> ---
Reconfirmed with GCC 9.1, from this Stack Overflow question:
https://stackoverflow.com/questions/57315054/unable-to-use-ternary-operator-to-conditionally-assign-istream/57315456

Some variants:

https://godbolt.org/z/gcnglB (GCC 9.1)

class Foo
{
public:
    Foo(int& i)
        : i{i > 0 ? i : throw "invalid"} 
    {}

private:
    int& i;
};

Error:

<source>: In constructor 'Foo::Foo(int&)':
<source>:6:40: error: cannot bind non-const lvalue reference of type 'int&' to
an rvalue of type 'int'
    6 |         : i{i > 0 ? i : throw "invalid"}
      |                                        ^


This one changed from GCC 8.3 and GCC 9.1, which I'm guessing is due to the
work by Jason Merrill:

https://godbolt.org/z/DfKGg4



struct no_copy {
    no_copy(no_copy const&) = delete;

    bool valid() const;
};

class Foo
{
public:
    Foo(no_copy& it)
        : it{it.valid() ? it : throw "invalid"} 
    {}

private:
    no_copy& it;
};



GCC 9.1:

<source>: In constructor 'Foo::Foo(no_copy&)':
<source>:11:47: error: cannot bind non-const lvalue reference of type
'no_copy&' to an rvalue of type 'no_copy'
   11 |         : it{it.valid() ? it : throw "invalid"}
      |                                               ^


GCC 8.3:

<source>: In constructor 'Foo::Foo(no_copy&)':
<source>:11:38: error: use of deleted function 'no_copy::no_copy(const
no_copy&)'
         : it{it.valid() ? it : throw "invalid"}
                                      ^~~~~~~~~
<source>:2:5: note: declared here
     no_copy(no_copy const&) = delete;
     ^~~~~~~
<source>:11:47: error: invalid initialization of non-const reference of type
'no_copy&' from an rvalue of type '<brace-enclosed initializer list>'
         : it{it.valid() ? it : throw "invalid"}
                                               ^

Reply via email to