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:

//Generated by BOOST_COPYABLE_AND_MOVABLE
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
'holder&'
    //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(){}

    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;
    storage.push_back(symbol);
}

Ion


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net