This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [libstdc++] Assertion in optional


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.



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]