Imagine we want to model C struct with dynamic C++ types. I.e. we have a set of fields, each field has a name and a value. The value can be a simple primitive type (let's say just int
for the sake of the example) or another structure, i.e. another set of fields.
It's quite straightforward:
template <typename RecursiveVariant>
struct Field
{
std::string name;
RecursiveVariant value;
};
template <typename RecursiveVariant>
using StructFields = std::vector<Field<RecursiveVariant>>;
typedef typename boost::make_recursive_variant<
int
, StructFields<boost::recursive_variant_>
>::type FieldValue;
int main(int argc, char* argv[])
{
FieldValue fv = 2;
StructFields<FieldValue> sf;
Field<FieldValue> f{"name", 2};
sf.push_back(f);
fv = sf;
return 0;
}
It works as expected. However, if we try to use multi_index_container
instead of std::vector
, it won't compile. If I change the definition of StructFields
to this:
template <typename RecursiveVariant>
using StructFields = multi_index_container<
Field<RecursiveVariant>
, indexed_by<
sequenced<>
, ordered_unique<
member<
Field<RecursiveVariant>
, std::string
, &Field<RecursiveVariant>::name
>
>
>
>;
The compiler (MSVC from VS 15.6.3) will issue
binary '=': no operator found which takes a right-hand operand of type 'boost::multi_index::multi_index_container,boost::multi_index::multi_index_container, ...
complaining on line fv = sf
. It complains on ambiguity between variant& operator=(const variant& rhs)
and variant& operator=(variant&& rhs)
. I am not sure how these operator=
are even involved. Is there any recipe to fix this?