[generic programming] header file for general "default" operations for C++ classes
In C++, a default assignment operator is generated for a class if all of its components (data members and base classes) have an assignment operator. There are plans in C++17 to do something similar for comparison operators. This header, in https://github.com/wkaras/C-plus-plus-library-default-operators , facilitates that, more generally, if there is an operation on all the components of a class, a single macro invocation can provide this operation on the class as well. A big drawback is that it's very intrusive into the class definition. Each class component must be defined using a macro, COMPOSITE_OP_MBR_T(int) i; for examle. The class will in truth be a template, with the template parameter being defaulted for normal usage. I don't know which, if any, existing Boot library this header would belong in.
On Wednesday, 4 May 2016 17:01:49 MSK Walt Karas wrote:
In C++, a default assignment operator is generated for a class if all of its components (data members and base classes) have an assignment operator. There are plans in C++17 to do something similar for comparison operators. This header, in https://github.com/wkaras/C-plus-plus-library-default-operators , facilitates that, more generally, if there is an operation on all the components of a class, a single macro invocation can provide this operation on the class as well. A big drawback is that it's very intrusive into the class definition. Each class component must be defined using a macro, COMPOSITE_OP_MBR_T(int) i; for examle. The class will in truth be a template, with the template parameter being defaulted for normal usage. I don't know which, if any, existing Boot library this header would belong in.
I think, Boost.Fusion already provides this for all adapted types. http://www.boost.org/doc/libs/1_60_0/libs/fusion/doc/html/fusion/sequence/ operator/comparison.html
Andrey Semashev
On Wednesday, 4 May 2016 17:01:49 MSK Walt Karas wrote:
In C++, a default assignment operator is generated for a class if all of its components (data members and base classes) have an assignment operator. There are plans in C++17 to do something similar for comparison operators. This header, in https://github.com/wkaras/C-plus-plus-library-default-operators , facilitates that, more generally, if there is an operation on all the components of a class, a single macro invocation can provide this operation on the class as well. A big drawback is that it's very intrusive into the class definition. Each class component must be defined using a macro, COMPOSITE_OP_MBR_T(int) i; for examle. The class will in truth be a template, with the template parameter being defaulted for normal usage. I don't know which, if any, existing Boot library this header would belong in.
I think, Boost.Fusion already provides this for all adapted types.
http://www.boost.org/doc/libs/1_60_0/libs/fusion/doc/html/fusion/sequence/ operator/comparison.html
Yes, but there is not full overlap of the capabilities. Fusion doesn't seem to provide a way to define a class with access control, base classes, and member functions as a sequence. I'm assuming BOOST_FUSION_DEFINE_STRUCT has no runtime overhead, although I could not find and explicit statement to that effect.
On 5/05/2016 07:45, Walt Karas wrote:
Yes, but there is not full overlap of the capabilities. Fusion doesn't seem to provide a way to define a class with access control, base classes, and member functions as a sequence. I'm assuming BOOST_FUSION_DEFINE_STRUCT has no runtime overhead, although I could not find and explicit statement to that effect.
If you put all the members that should participate in the comparison into the fusion sequence, you can then wrap this sequence in another class with base classes and other methods. Depending on requirements, the fusion sequence could either be another base class, or it could be a member of the wrapper class with the wrapper class defining delegating operators for the comparisons.
Gavin Lambert
On 5/05/2016 07:45, Walt Karas wrote:
Yes, but there is not full overlap of the capabilities. Fusion doesn't seem to provide a way to define a class with access control, base classes, and member functions as a sequence. I'm assuming BOOST_FUSION_DEFINE_STRUCT has no runtime overhead, although I could not find and explicit statement to that effect.
If you put all the members that should participate in the comparison into the fusion sequence, you can then wrap this sequence in another class with base classes and other methods.
Depending on requirements, the fusion sequence could either be another base class, or it could be a member of the wrapper class with the wrapper class defining delegating operators for the comparisons.
Good point. However, I don't see that BOOST_FUSION_DEFINE_STRUCT (or any other Fusion facility) can create a class with base classes that are also in the sequence along with the data members. Also, under the approach you describe, all data members would have to have the same access (private, protected or public). To clarify, what I propose (as well as the approach using fusion) is more general than just comparison operators. Please see the example function pad_bytes() in tst.cpp.
participants (3)
-
Andrey Semashev
-
Gavin Lambert
-
Walt Karas