[libstdc++] Assertion in optional
Jonathan Wakely
jwakely@redhat.com
Mon May 15 16:59:00 GMT 2017
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.
More information about the Libstdc++
mailing list