explicit icon indicating copy to clipboard operation
explicit copied to clipboard

Add macro to ease creating tagged bools

Open mlimber opened this issue 7 years ago • 3 comments

This pull request adds a macro to eliminate boilerplate code when defining a tagged bool. At present, one has to do this to create some tagged_bools:

class is_ready_tag {};
using is_ready = ak_toolkit::xplicit::tagged_bool< is_ready_tag >;
class has_signaled_tag {};
using is_ready = ak_toolkit::xplicit::tagged_bool< has_signaled_tag >;

Or:

using is_ready = ak_toolkit::xplicit::tagged_bool< class is_ready_tag >;
using is_ready = ak_toolkit::xplicit::tagged_bool< class has_signaled_tag >;

With this change, the macro handles the boilerplate:

AKT_MAKE_TAGGED_BOOL( is_ready );
AKT_MAKE_TAGGED_BOOL( has_signaled );

mlimber avatar May 25 '17 12:05 mlimber

I waffled a bit on this, sorry. The two alternatives are my penultimate commit:

#define AKT_MAKE_TAGGED_BOOL( Name )                                   \
    class Name##ExplicitBoolTag { Name##ExplicitBoolTag() = delete; }; \
    using Name = ::ak_toolkit::xplicit::tagged_bool< Name##ExplicitBoolTag >

and my latest commit (using forward decl only):

#define AKT_MAKE_TAGGED_BOOL( Name ) \
    using Name = ::ak_toolkit::xplicit::tagged_bool< class Name##ExplicitBoolTag >

The difference is whether one can re-use existing class names as tags. The former says no, the latter says yes. I don't see a strong use case for such re-use, and it probably violates the single responsibility principle by making a functional class do double duty as a tag.

mlimber avatar May 25 '17 13:05 mlimber

I find

using is_ready = ak_toolkit::xplicit::tagged_bool< class is_ready_tag >;

quite simple, and even if the DRY principle is not respected completely I fill this kind of macros make the code more obscure.

viboes avatar May 25 '17 14:05 viboes

Right: it's a trade-off, and totally optional. You can choose either to repeat yourself (boilerplate + tag name which is usually just name_tag), or you can use the slightly more obscured macro which gives less repetition, better adherence to the single responsibility principle, and (if the penultimate version above is used) slightly safer alternative since the tags can't be accidentally created and used.

Programmer's choice.

mlimber avatar May 25 '17 16:05 mlimber