this is an example inspired by an example from cppreference
struct S { operator int() { throw 42; } }; int main(){ variant<float, int> v{12.f}; // OK cout << std::boolalpha << v.valueless_by_exception() << "\n"; try{ v.emplace<1>(S()); // v may be valueless } catch(...){ } cout << std::boolalpha << v.valueless_by_exception() << "\n"; }
For one compiler, I tried to output it
false true
means that emplacemade the option become useless
emplace
I do not understand how this happened. In particular, I do not understand why it emplaceis called at all, I would expect that the program will not even call it from the moment of converting from S to the arguments of the int argument.
Pay attention to the signature for the relevant overload std::variant::emplace:
std::variant::emplace
template <size_t I, class... Args> std::variant_alternative_t<I, variant>& emplace(Args&&... args);
. , S int ; emplace. int , , .
S
int
, variant , , , , , , , .
variant
Source: https://habr.com/ru/post/1689774/More articles:remove dict from list if one value is equal and another value is lower - pythonUISearchController does not display as expected - iosLoadError for dotenv / load when running Dotenv.load - ruby | fooobar.comMoney data type overflowing - castingkubernetes development environment to reduce development time - dockerSSIS Flat File Destination Data Export - ssisHow to get the position of the component on the screen in the native response? - javascriptHow does the superstructure of structures within unions work? - cКак преобразовать результат Presto `ST_Distance` в метры? - sqlCreating MIDI files on the fly with Audiokit - iosAll Articles