[container / move] return a container of non-movables from a function in C++03
Hi,
I'd like to return a container of non-movables from a function in C++03
with no success.
here is a sample code that does not compile for me:
#include
Ops. I meant I'd like to return a container of non-copyable but movable objects, as shown in the example.
WBR,
Adam Romanek
Dnia 13 sie 2013 o godz. 16:09 Adam Romanek
Hi,
I'd like to return a container of non-movables from a function in C++03 with no success.
here is a sample code that does not compile for me:
#include
#include class non_copyable { BOOST_MOVABLE_BUT_NOT_COPYABLE(non_copyable)
public: non_copyable(){} non_copyable(BOOST_RV_REF(non_copyable)) {} non_copyable& operator=(BOOST_RV_REF(non_copyable)) { return *this; } };
typedef boost::container::vector
nc_vector; nc_vector create() { return nc_vector(); }
int main () { nc_vector v(create()); return 0; }
The compiler says:
(...) /home/A.Romanek/tmp/boost/boost_1_54_0/boost/preprocessor/iteration/detail/local.hpp:37:1: error: passing ‘const non_copyable’ as ‘this’ argument of ‘non_copyable::operator boost::rv
&()’ discards qualifiers [-fpermissive] (...) See below for a full compiler output.
I use Boost v1.54 and gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3.
Is it a bug?
Here is the full compiler output:
In file included from /home/A.Romanek/tmp/boost/boost_1_54_0/boost/container/allocator_traits.hpp:388:0, from /home/A.Romanek/tmp/boost/boost_1_54_0/boost/container/detail/utilities.hpp:28, from /home/A.Romanek/tmp/boost/boost_1_54_0/boost/container/vector.hpp:37, from main.cpp:1: /home/A.Romanek/tmp/boost/boost_1_54_0/boost/preprocessor/iteration/detail/local.hpp: In static member function ‘static void boost::container::allocator_traits<Alloc>::priv_construct_dispatch2(boost::false_type, Alloc&, T*, const P0&) [with T = non_copyable, P0 = non_copyable, Alloc = std::allocator
, boost::false_type = boost::integral_constant ]’: /home/A.Romanek/tmp/boost/boost_1_54_0/boost/preprocessor/iteration/detail/local.hpp:37:1: instantiated from ‘static void boost::container::allocator_traits<Alloc>::priv_construct(boost::true_type, Alloc&, T*, const P0&) [with T = non_copyable, P0 = non_copyable, Alloc = std::allocator , boost::true_type = boost::integral_constant ]’ /home/A.Romanek/tmp/boost/boost_1_54_0/boost/preprocessor/iteration/detail/local.hpp:37:1: instantiated from ‘static void boost::container::allocator_traits<Alloc>::construct(Alloc&, T*, const P0&) [with T = non_copyable, P0 = non_copyable, Alloc = std::allocator ]’ /home/A.Romanek/tmp/boost/boost_1_54_0/boost/container/detail/utilities.hpp:555:10: instantiated from ‘typename boost::container::container_detail::disable_if_memcpy_copy_constructible::type boost::container::uninitialized_copy_alloc_n(A&, I, typename std::iterator_traits<_II>::difference_type, F) [with A = std::allocator , I = non_copyable*, F = non_copyable*, typename boost::container::container_detail::disable_if_memcpy_copy_constructible::type = non_copyable*, typename std::iterator_traits<_II>::difference_type = int]’ /home/A.Romanek/tmp/boost/boost_1_54_0/boost/container/vector.hpp:734:7: instantiated from ‘boost::container::vector ::vector(const boost::container::vector &) [with T = non_copyable, Allocator = std::allocator , boost::container::vector = boost::container::vector ]’ main.cpp:16:20: instantiated from here /home/A.Romanek/tmp/boost/boost_1_54_0/boost/preprocessor/iteration/detail/local.hpp:37:1: error: passing ‘const non_copyable’ as ‘this’ argument of ‘non_copyable::operator boost::rv &()’ discards qualifiers [-fpermissive] WBR, Adam Romanek
It turned out that copy-constructor is chosen over move-constructor when constructing nc_vector "v" in main(). That leads me to a question whether copy-constructor shouldn't be disable_if-ed in case the container's value_type is non-copyable? Is there any way to work-around this issue? WBR, Adam Romanek On 08/13/2013 04:59 PM, Gmail wrote:
Ops. I meant I'd like to return a container of non-copyable but movable objects, as shown in the example.
WBR, Adam Romanek
Dnia 13 sie 2013 o godz. 16:09 Adam Romanek
napisał(a): Hi,
I'd like to return a container of non-movables from a function in C++03 with no success.
here is a sample code that does not compile for me:
#include
#include class non_copyable { BOOST_MOVABLE_BUT_NOT_COPYABLE(non_copyable)
public: non_copyable(){} non_copyable(BOOST_RV_REF(non_copyable)) {} non_copyable& operator=(BOOST_RV_REF(non_copyable)) { return *this; } };
typedef boost::container::vector
nc_vector; nc_vector create() { return nc_vector(); }
int main () { nc_vector v(create()); return 0; }
The compiler says:
(...) /home/A.Romanek/tmp/boost/boost_1_54_0/boost/preprocessor/iteration/detail/local.hpp:37:1: error: passing ‘const non_copyable’ as ‘this’ argument of ‘non_copyable::operator boost::rv
&()’ discards qualifiers [-fpermissive] (...) See below for a full compiler output.
I use Boost v1.54 and gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3.
Is it a bug?
Here is the full compiler output:
In file included from /home/A.Romanek/tmp/boost/boost_1_54_0/boost/container/allocator_traits.hpp:388:0, from /home/A.Romanek/tmp/boost/boost_1_54_0/boost/container/detail/utilities.hpp:28, from /home/A.Romanek/tmp/boost/boost_1_54_0/boost/container/vector.hpp:37, from main.cpp:1: /home/A.Romanek/tmp/boost/boost_1_54_0/boost/preprocessor/iteration/detail/local.hpp: In static member function ‘static void boost::container::allocator_traits<Alloc>::priv_construct_dispatch2(boost::false_type, Alloc&, T*, const P0&) [with T = non_copyable, P0 = non_copyable, Alloc = std::allocator
, boost::false_type = boost::integral_constant ]’: /home/A.Romanek/tmp/boost/boost_1_54_0/boost/preprocessor/iteration/detail/local.hpp:37:1: instantiated from ‘static void boost::container::allocator_traits<Alloc>::priv_construct(boost::true_type, Alloc&, T*, const P0&) [with T = non_copyable, P0 = non_copyable, Alloc = std::allocator , boost::true_type = boost::integral_constant ]’ /home/A.Romanek/tmp/boost/boost_1_54_0/boost/preprocessor/iteration/detail/local.hpp:37:1: instantiated from ‘static void boost::container::allocator_traits<Alloc>::construct(Alloc&, T*, const P0&) [with T = non_copyable, P0 = non_copyable, Alloc = std::allocator ]’ /home/A.Romanek/tmp/boost/boost_1_54_0/boost/container/detail/utilities.hpp:555:10: instantiated from ‘typename boost::container::container_detail::disable_if_memcpy_copy_constructible::type boost::container::uninitialized_copy_alloc_n(A&, I, typename std::iterator_traits<_II>::difference_type, F) [with A = std::allocator , I = non_copyable*, F = non_copyable*, typename boost::container::container_detail::disable_if_memcpy_copy_constructible::type = non_copyable*, typename std::iterator_traits<_II>::difference_type = int]’ /home/A.Romanek/tmp/boost/boost_1_54_0/boost/container/vector.hpp:734:7: instantiated from ‘boost::container::vector ::vector(const boost::container::vector &) [with T = non_copyable, Allocator = std::allocator , boost::container::vector = boost::container::vector ]’ main.cpp:16:20: instantiated from here /home/A.Romanek/tmp/boost/boost_1_54_0/boost/preprocessor/iteration/detail/local.hpp:37:1: error: passing ‘const non_copyable’ as ‘this’ argument of ‘non_copyable::operator boost::rv &()’ discards qualifiers [-fpermissive] WBR, Adam Romanek
Le 13/08/13 16:09, Adam Romanek a écrit :
Hi,
I'd like to return a container of non-movables from a function in C++03 with no success.
Boost.Container should be movable independently on whether their elements are movable or not.
here is a sample code that does not compile for me:
#include
#include class non_copyable { BOOST_MOVABLE_BUT_NOT_COPYABLE(non_copyable)
public: non_copyable(){} non_copyable(BOOST_RV_REF(non_copyable)) {} non_copyable& operator=(BOOST_RV_REF(non_copyable)) { return *this; } };
typedef boost::container::vector
nc_vector; nc_vector create() { return nc_vector(); }
I suspect that this is a limitation on the emulation with gcc-4.6.3. Could you try nc_vector create() { nc_vector res; return boost::move(res); } Best, Vicente
On 08/13/2013 11:03 PM, Vicente J. Botet Escriba wrote:
Le 13/08/13 16:09, Adam Romanek a écrit :
I'd like to return a container of non-movables from a function in C++03 with no success.
Boost.Container should be movable independently on whether their elements are movable or not.
Right. I made a mistake in the opening post - I wanted to ask about a container of non-copyables but movables.
nc_vector create() { return nc_vector(); }
I suspect that this is a limitation on the emulation with gcc-4.6.3. Could you try
nc_vector create() { nc_vector res; return boost::move(res); }
This was the first thing I tried. Unfortunately without success. It turned out that the problem is not in returning the container from function but in constructing the other one from it in main(): int main () { nc_vector v(create()); return 0; } The boost::container::vector offers copy-constructor and move-constructor. In C++03 mode the first one is chosen which leads to a compile error. On the other hand with -std=c++0x the latter one is chosen and the code compiles without any problems. One way to workaround the issue is to make the copy-constructor disable_if-ed in case of non-copyable elements in C++03. The copy-constructor is useless in this case so such a change should be safe, right? What do you think about it? WBR, Adam Romanek
Best, Vicente _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
2013/8/14 Adam Romanek
One way to workaround the issue is to make the copy-constructor disable_if-ed in case of non-copyable elements in C++03. The copy-constructor is useless in this case so such a change should be safe, right?
What do you think about it?
If you use SFINAE to disable the copy-constructor, won't one be autogenerated? Regards, Kris
El 14/08/2013 8:20, Adam Romanek escribió:
On 08/13/2013 11:03 PM, Vicente J. Botet Escriba wrote:
Le 13/08/13 16:09, Adam Romanek a écrit :
I'd like to return a container of non-movables from a function in C++03 with no success.
Boost.Container should be movable independently on whether their elements are movable or not.
Right. I made a mistake in the opening post - I wanted to ask about a container of non-copyables but movables.
This seems a bug. Can you fill a ticket please? Ion
On 08/14/2013 11:35 PM, Ion Gaztañaga wrote:
El 14/08/2013 8:20, Adam Romanek escribió:
On 08/13/2013 11:03 PM, Vicente J. Botet Escriba wrote:
Le 13/08/13 16:09, Adam Romanek a écrit :
I'd like to return a container of non-movables from a function in C++03 with no success.
Boost.Container should be movable independently on whether their elements are movable or not.
Right. I made a mistake in the opening post - I wanted to ask about a container of non-copyables but movables.
This seems a bug. Can you fill a ticket please?
Here you have: https://svn.boost.org/trac/boost/ticket/9003 WBR, Adam Romanek
participants (5)
-
Adam Romanek
-
Gmail
-
Ion Gaztañaga
-
Krzysztof Czainski
-
Vicente J. Botet Escriba