Skip to content

Commit eb1ee8c

Browse files
authored
Update 006_variant_variant_npos.cpp
1 parent fc1b201 commit eb1ee8c

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed
Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
1-
1
1+
#include <iostream>
2+
#include <stdexcept>
3+
#include <string>
4+
#include <variant>
5+
6+
struct Demon
7+
{
8+
Demon(int) {}
9+
Demon(const Demon&) { throw std::domain_error("copy ctor"); }
10+
Demon& operator= (const Demon&) = default;
11+
};
12+
13+
int main()
14+
{
15+
std::variant<int, Demon> var{42};
16+
std::cout
17+
<< std::boolalpha
18+
<< "index == npos: " << (var.index() == std::variant_npos) << '\n';
19+
20+
try { var = Demon{666}; } catch (const std::domain_error& ex)
21+
{
22+
std::cout
23+
<< "Exception: " << ex.what() << '\n'
24+
<< "index == npos: " << (var.index() == std::variant_npos) << '\n'
25+
<< "valueless: " << var.valueless_by_exception() << '\n';
26+
}
27+
}

0 commit comments

Comments
 (0)