This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [libstdc++] Assertion in optional
- From: Jonathan Wakely <jwakely at redhat dot com>
- To: Marc Glisse <marc dot glisse at inria dot fr>
- Cc: libstdc++ at gcc dot gnu dot org, gcc-patches at gcc dot gnu dot org
- Date: Mon, 15 May 2017 17:59:23 +0100
- Subject: Re: [libstdc++] Assertion in optional
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=jwakely at redhat dot com
- Dkim-filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 9B6C43680B
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 9B6C43680B
- References: <alpine.DEB.2.02.1705140854260.4227@stedding.saclay.inria.fr>
On 14/05/17 09:19 +0200, Marc Glisse wrote:
Hello,
this patch adds 2 simple __glibcxx_assert in optional that match the
precondition in the comment above. I am not sure if there was a reason
the author wrote that comment instead of the assertion, but constexpr
use still seems to work.
Yes, in a constexpr context we get the following when it's not
engaged:
In file included from /home/jwakely/gcc/8/include/c++/8.0.0/utility:68:0,
from /home/jwakely/gcc/8/include/c++/8.0.0/optional:36,
from opt.cc:1:
/home/jwakely/gcc/8/include/c++/8.0.0/optional: In function ‘int main()’:
opt.cc:11:22: in constexpr expansion of ‘f()’
opt.cc:6:11: in constexpr expansion of ‘o.std::optional<int>::operator*()’
/home/jwakely/gcc/8/include/c++/8.0.0/optional:708:29: in constexpr expansion of ‘((std::optional<int>*)this)->std::optional<int>::<anonymous>.std::_Optional_base<int>::_M_get()’
/home/jwakely/gcc/8/include/c++/8.0.0/optional:390:2: error: call to non-constexpr function ‘void std::__replacement_assert(const char*, int, const char*, const char*)’
__glibcxx_assert(_M_is_engaged());
^
I think that's an improvement over what we have now:
opt.cc: In function ‘int main()’:
opt.cc:11:22: in constexpr expansion of ‘f()’
opt.cc:11:23: error: accessing ‘std::_Optional_payload<int, true, true>::<unnamed union>::_M_payload’ member instead of initialized ‘std::_Optional_payload<int, true, true>::<unnamed union>::_M_empty’ member in constant expression
constexpr int i = f();
^
So the patch is OK for trunk, thanks.