In the example below I am expecting v to have 1 element, whos var will be of type Vector and will contain two "int" Variants 10 and 20.
This is the behaviour I can see with gcc.
With clang, 'v' contains two elements which are the two "int" Variants 10 and 20.
I think gcc's vector is created via the initializer_list constructor while clang's via the move constructor
Is this a bug in one of the compilers? Do I need to make the Variant's constructors explicit (which will force me to use it like Variant::Vector v{Variant{Variant::Vector{10, 20}}};. Is there any other way to avoid this issue if I want to keep the constructors non explicit?
Tried this code with all gcc and clang versions in https://wandbox.org/ and it behaves the same. Here are some links to try it directly: gcc, clang
#include <iostream>
#include <variant>
#include <vector>
struct Variant
{
using Vector = std::vector<Variant>;
Variant(const Vector & value)
{
var = value;
}
Variant(Vector && value)
{
var = std::move(value);
}
Variant(int value)
{
var = value;
}
std::variant<Vector, int> var;
};
int main()
{
Variant::Vector v{Variant::Vector{10, 20}};
std::cout << "v size: " << v.size() << ", index: " << v.at(0).var.index() << std::endl;
return 0;
}