boostorg / container

STL-like containers from Boost
http://www.boost.org/libs/container/
Boost Software License 1.0
96 stars 116 forks source link

Make `if` statements `constexpr` for flags known at compile time #271

Closed AndreyKulikov2022 closed 1 month ago

AndreyKulikov2022 commented 3 months ago

I am trying to move-assign one container to the other:

void func(boost::container::deque<T>& deque1){
boost::container::deque<T> deque2;
// Fill deque2 with elements.
...
deque1 = std::move(deque2);
}

If T is neither move nor copy constructable, this code does not compile. However, it should and compiles for std containers, because it is possible to just pass memory ownership from deque2 to deque1 since their allocators are the same. The problem is that the choice between transferring resources or moving the elements one by one is written using ordinary if. The compiler generates the error when checking the second option even though it is not used. With if constexpr instead of if the statement is resolved at compile time and no error is generated.

I propose changing if statements for flags known at compile time to if constexpr using BOOST_IF_CONSTEXPR.

igaztanaga commented 1 month ago

Thanks for the report. Reviewing this patch I've found that many containers don't support non move-assignable types when allocator support is appropiate. I created an issue with the general problem and resolved it adding required container changes and modified tests. See:

https://github.com/boostorg/container/issues/280

AndreyKulikov2022 commented 1 month ago

Thank you!