简体   繁体   中英

Bug in std::variant implementation of gcc7

Consider this code:

#include <variant>

constexpr auto cc = 'c';
constexpr std::variant<const int*,const char*> pp{&cc}; // passes
constexpr std::variant<const int&,const char&> rr{cc};  // FAILS ?!

With g++ (GCC) 7.0.0 20161023 (experimental) the last line fails with error message:

variant|199 col 9| error: call to non-constexpr function ‘void* operator new(std::size_t, void*)’          
||        { ::new (&_M_storage) _Type(std::forward<_Args>(__args)...); }

pointing to constructor:

template<typename... _Args>
constexpr _Uninitialized(in_place_index_t<0>, _Args&&... __args)
: _M_storage{} // This was added manually
{ ::new (&_M_storage) _Type(std::forward<_Args>(__args)...); }

Actually, before this there were two more issues:

First it was complaining call to non-constexpr function 'std::_Enable_default_constructor<false, _Tag>::_Enable_default_constructor(std::_Enable_default_constructor_tag) I've fixed it by making constexpr the constructor _Enable_default_constructor<false, _Tag>::_Enable_default_constructor(_Enable_default_constructor_tag) { }

Then it was complaining _M_storage' must be initialized by mem-initializer in 'constexpr' constuctor I've fixed it by adding : _M_storage{} to the member initialization list, though I don't think it should complain as the added line should be generated implicitly.

So the first question is: this is certainly a bug right?

And the second question is: how can I workaround it by changing the library codes before gcc will fix it?

That's "fixed" in the standard by disallowing references as variant alternatives (for now). GCC 7.0.1's snapshot from 2017-03-29 seems to do that correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM