The non-variant version of initializer list works good:
std::map<int, double> a = {{1,0.1}};
Run Code Online (Sandbox Code Playgroud)
But the variant version doesn't:
std::variant<std::map<int, double>, int> b = {{1,0.1}};
Run Code Online (Sandbox Code Playgroud)
Is there a way to initialize b using initializer list? If not, what is the best way to initialize it?
One way is to be more specific:
std::variant<std::map<int, double>, int> b = std::map<int, double>{{1,0.1}};
Run Code Online (Sandbox Code Playgroud)
Not ideal, but the compiler cannot choose the right overload of std::variant constructor from a <brace-enclosed initializer list>. Because of how the relevant std::variant constructor is defined:
template< class T >
constexpr variant( T&& t ) noexcept(/* see below */);
Run Code Online (Sandbox Code Playgroud)
T&& t cannot possibly match a <brace-enclosed initializer list>, only a value of a specific type.
| 归档时间: |
|
| 查看次数: |
88 次 |
| 最近记录: |