c++17

Why can't std::as_const(T &&v) move-return its argument?

痴心易碎 提交于 2021-01-21 09:20:02
问题 Reading Why does as_const forbid rvalue arguments? I understand that we can't convert a rvalue-ref into an lvalue-ref, of course. But why not move the rvalue-ref into a value and return that, i.e. ? template<typename T> const T as_const(T&& val) { return std::move(val); } This ought to work nicely with COW containers as well, as the returned value is const and iterators from it will not cause it to detach. Maybe some godbolt-ing will answer this though, but I can't think of a given scenario

Why can't std::as_const(T &&v) move-return its argument?

倖福魔咒の 提交于 2021-01-21 09:18:21
问题 Reading Why does as_const forbid rvalue arguments? I understand that we can't convert a rvalue-ref into an lvalue-ref, of course. But why not move the rvalue-ref into a value and return that, i.e. ? template<typename T> const T as_const(T&& val) { return std::move(val); } This ought to work nicely with COW containers as well, as the returned value is const and iterators from it will not cause it to detach. Maybe some godbolt-ing will answer this though, but I can't think of a given scenario

C++20 behaviour breaking existing code with equality operator?

孤街浪徒 提交于 2021-01-20 14:13:48
问题 I ran into this while debugging this question. I trimmed it down all the way to just using Boost Operators: Compiler Explorer C++17 C++20 #include <boost/operators.hpp> struct F : boost::totally_ordered1<F, boost::totally_ordered2<F, int>> { /*implicit*/ F(int t_) : t(t_) {} bool operator==(F const& o) const { return t == o.t; } bool operator< (F const& o) const { return t < o.t; } private: int t; }; int main() { #pragma GCC diagnostic ignored "-Wunused" F { 42 } == F{ 42 }; // OKAY 42 == F

Policy class design but without making the whole user class a template

痞子三分冷 提交于 2021-01-19 06:35:32
问题 Consider the following code where the Writer_I acts as an interface. Other classes which fulfil the contract of writing element types in correct form can derive from it. Here, printf and streams are chosen as policies, and Calculator as user. That interface is somehow stored in Calculator and write_i hides all the ugly details of templates so that class member functions remain clean. Most things remain known at compile time, and inline-able. I know this is a classic case of virtual +

Policy class design but without making the whole user class a template

ぃ、小莉子 提交于 2021-01-19 06:32:52
问题 Consider the following code where the Writer_I acts as an interface. Other classes which fulfil the contract of writing element types in correct form can derive from it. Here, printf and streams are chosen as policies, and Calculator as user. That interface is somehow stored in Calculator and write_i hides all the ugly details of templates so that class member functions remain clean. Most things remain known at compile time, and inline-able. I know this is a classic case of virtual +

Policy class design but without making the whole user class a template

梦想的初衷 提交于 2021-01-19 06:32:02
问题 Consider the following code where the Writer_I acts as an interface. Other classes which fulfil the contract of writing element types in correct form can derive from it. Here, printf and streams are chosen as policies, and Calculator as user. That interface is somehow stored in Calculator and write_i hides all the ugly details of templates so that class member functions remain clean. Most things remain known at compile time, and inline-able. I know this is a classic case of virtual +

Policy class design but without making the whole user class a template

試著忘記壹切 提交于 2021-01-19 06:28:33
问题 Consider the following code where the Writer_I acts as an interface. Other classes which fulfil the contract of writing element types in correct form can derive from it. Here, printf and streams are chosen as policies, and Calculator as user. That interface is somehow stored in Calculator and write_i hides all the ugly details of templates so that class member functions remain clean. Most things remain known at compile time, and inline-able. I know this is a classic case of virtual +

Can I use boost::copy_range on the result of boost::adaptors::transformed over std::array to get back another std::array?

半腔热情 提交于 2021-01-05 07:43:45
问题 This code works and generates the correct output ( 23 , as 2 and 3 are the sizes of the two std::vector<int> s in array_of_vectors : #include <array> #include <boost/range/adaptor/transformed.hpp> #include <iostream> #include <vector> using boost::adaptors::transformed; constexpr auto get_size = [](auto const& snip){ return snip.size(); }; int main() { std::array<std::vector<int>,2> array_of_vectors = { {std::vector<int>{1,1}, std::vector<int>{1,1,1}} }; auto array_of_sizes = array_of_vectors

Guaranteed copy elision for uniform braced array initialization - Shouldn't this be mandatory since C++17? [duplicate]

╄→尐↘猪︶ㄣ 提交于 2021-01-01 07:11:53
问题 This question already has answers here : How to initialize array of classes with deleted copy constructor (C++11) (2 answers) Closed 25 days ago . As far as I understand the new rules correctly https://en.cppreference.com/w/cpp/language/copy_elision http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0135r0.html This code should compile for C++17 standard conform compilers struct NonTrivialClass { ~NonTrivialClass( ){ } }; class MainNonTrivialClass { public: MainNonTrivialClass(int t) :

Compile-time Size of Struct Minus Padding

谁说我不能喝 提交于 2021-01-01 06:43:37
问题 I'm trying to use Boost MPL and Fusion to calculate the size of a struct exclusive of any padding. This is my current best attempt: Live example template<class T> constexpr std::size_t sizeof_members(void) { using namespace std; namespace mpl = boost::mpl; namespace fusion = boost::fusion; //This works, but only for structs containing exactly 4 members... typedef typename mpl::apply<mpl::unpack_args<mpl::vector<mpl::_1, mpl::_2, mpl::_3, mpl::_4>::type >, T>::type member_types; typedef