Boost logo

Boost Users :

Subject: Re: [Boost-users] [container] Initialization from transformed_range compilation error
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2011-09-07 06:52:24

El 07/09/2011 1:31, Daniel James escribió:
> #include<boost/container/vector.hpp>
> #include<string>
> struct template_symbol
> {
> boost::container::vector<std::string> params;
> };
> int main()
> {
> boost::container::vector<template_symbol> storage;
> template_symbol symbol;
> storage.push_back(symbol);
> }

That's a limitation of the move emulation explained in the documentation:

The macro BOOST_COPYABLE_AND_MOVABLE needs to define a copy constructor
for copyable_and_movable taking a non-const parameter in C++03 compilers:

copyable_and_movable &operator=(copyable_and_movable&){/**/}

Since the non-const overload of the copy constructor is generated,
compiler-generated assignment operators for classes containing
copyable_and_movable will get the non-const copy constructor overload,
which will surely surprise users:

class holder
    copyable_and_movable c;

void func(const holder& h)
    holder copy_h(h); //<--- ERROR: can't convert 'const holder&' to
    //Compiler-generated copy constructor is non-const:
    // holder& operator(holder &)

This limitation forces the user to define a const version of the copy
assignment, in all classes holding copyable and movable classes which
might annoying in some cases.

If you define the copy constructor you avoid the error. Annoying, but
I've not found any workaround. AFter adding such operations, this works.

#include <boost/container/vector.hpp>
#include <string>

struct template_symbol

    template_symbol(const template_symbol &x)
       : params(x.params)

    template_symbol& operator=(const template_symbol &x)
       params = x.params;
       return *this;

    boost::container::vector<std::string> params;

int main()
    boost::container::vector<template_symbol> storage;
    template_symbol symbol;


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at