c++17

Compile-time Size of Struct Minus Padding

≯℡__Kan透↙ 提交于 2021-01-01 06:42:46
问题 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

Enums support for inheritance

笑着哭i 提交于 2020-12-31 06:09:42
问题 I frequently come across a situation where we create a class that acts on some enumeration, but later we derive and we want to add more values to the enumeration without changing the base class. I see this question from 2009: Base enum class inheritance However, I know there were a number of changes to enum in C++11, 14, 17. Do any of those changes allow for extension of enums from base class to derived? class Base { enum State {STATE_1, STATE_2, STATE_3}; }; class Derived : public Base {

Enums support for inheritance

跟風遠走 提交于 2020-12-31 06:09:34
问题 I frequently come across a situation where we create a class that acts on some enumeration, but later we derive and we want to add more values to the enumeration without changing the base class. I see this question from 2009: Base enum class inheritance However, I know there were a number of changes to enum in C++11, 14, 17. Do any of those changes allow for extension of enums from base class to derived? class Base { enum State {STATE_1, STATE_2, STATE_3}; }; class Derived : public Base {

Enums support for inheritance

痞子三分冷 提交于 2020-12-31 06:05:27
问题 I frequently come across a situation where we create a class that acts on some enumeration, but later we derive and we want to add more values to the enumeration without changing the base class. I see this question from 2009: Base enum class inheritance However, I know there were a number of changes to enum in C++11, 14, 17. Do any of those changes allow for extension of enums from base class to derived? class Base { enum State {STATE_1, STATE_2, STATE_3}; }; class Derived : public Base {

Enums support for inheritance

安稳与你 提交于 2020-12-31 06:04:11
问题 I frequently come across a situation where we create a class that acts on some enumeration, but later we derive and we want to add more values to the enumeration without changing the base class. I see this question from 2009: Base enum class inheritance However, I know there were a number of changes to enum in C++11, 14, 17. Do any of those changes allow for extension of enums from base class to derived? class Base { enum State {STATE_1, STATE_2, STATE_3}; }; class Derived : public Base {

Polymorphic value types and interfaces

ⅰ亾dé卋堺 提交于 2020-12-30 02:33:25
问题 I have a polymorphic value type implemented like so: class ShapeValue { public: template<class T> ShapeValue(const T& value) { obj = make_unique<holder<T>>(value); } // ... appropriate copy constructors and such void draw() { obj->draw(); } private: struct base { virtual ~base() {} virtual void draw() = 0; }; template<class T> struct holder<T> : public base { T value; void draw() override { value.draw(); } } unique_ptr<base> obj; }; If you aren't familiar with this sort of thing, here's a

Polymorphic value types and interfaces

痞子三分冷 提交于 2020-12-30 02:33:13
问题 I have a polymorphic value type implemented like so: class ShapeValue { public: template<class T> ShapeValue(const T& value) { obj = make_unique<holder<T>>(value); } // ... appropriate copy constructors and such void draw() { obj->draw(); } private: struct base { virtual ~base() {} virtual void draw() = 0; }; template<class T> struct holder<T> : public base { T value; void draw() override { value.draw(); } } unique_ptr<base> obj; }; If you aren't familiar with this sort of thing, here's a

What do we need std::as_const() for?

你离开我真会死。 提交于 2020-12-29 08:52:30
问题 C++11 has given us std::add_const; with C++17, we have a new structure - std::as_const(). The former just tacks a const before the type you provide it with. The second one is a proper (template of a) function, not type trait, which seems to do the same - except for when the type is an rvalue-reference, in which case it cannot be used. I don't quite understand the motivation for providing std::as_const() . Why do we need it in addition to std::add_const ? 回答1: "Need" is a strong word... std:

What do we need std::as_const() for?

让人想犯罪 __ 提交于 2020-12-29 08:52:23
问题 C++11 has given us std::add_const; with C++17, we have a new structure - std::as_const(). The former just tacks a const before the type you provide it with. The second one is a proper (template of a) function, not type trait, which seems to do the same - except for when the type is an rvalue-reference, in which case it cannot be used. I don't quite understand the motivation for providing std::as_const() . Why do we need it in addition to std::add_const ? 回答1: "Need" is a strong word... std:

What do we need std::as_const() for?

情到浓时终转凉″ 提交于 2020-12-29 08:52:07
问题 C++11 has given us std::add_const; with C++17, we have a new structure - std::as_const(). The former just tacks a const before the type you provide it with. The second one is a proper (template of a) function, not type trait, which seems to do the same - except for when the type is an rvalue-reference, in which case it cannot be used. I don't quite understand the motivation for providing std::as_const() . Why do we need it in addition to std::add_const ? 回答1: "Need" is a strong word... std: