Boost logo

Boost Users :

Subject: Re: [Boost-users] [MPL] implementing a "trait"?
From: Louis Dionne (ldionne.2_at_[hidden])
Date: 2014-02-04 18:02:01


John M. Dlugosz <ngnr63q02 <at> sneakemail.com> writes:

[...]

> The trait will be false by default, and I'd declare something to nominate
> types that should be seen to have that trait. I want it to follow
> inheritance; if B has been declared to be in my category, and C is derived
> from B, then C will also be in that category without needing to do anything
> more (though some way to turn it _off_ would be available).

This may or may not be what you're looking for:

    #include <type_traits>

    struct make_pretty { };

    std::true_type is_pretty_impl(make_pretty*);
    std::false_type is_pretty_impl(...);

    template <typename T>
    struct is_pretty : decltype(is_pretty_impl((T*)nullptr)) { };

    struct pretty : make_pretty { };
    struct pretty_too : pretty { };

    struct ugly_by_default;

    struct pretty_can_be_made_ugly : make_pretty { };
    template <> struct is_pretty<pretty_can_be_made_ugly>
        : std::false_type
    { };

    static_assert(is_pretty<pretty>::value, "");
    static_assert(is_pretty<pretty_too>::value, "");
    static_assert(!is_pretty<ugly_by_default>::value, "");
    static_assert(!is_pretty<pretty_can_be_made_ugly>::value, "");

Caveat: is_pretty will return false on a type that inherits something
pretty until it is complete (this could be confusing).

Regards,

Louis Dionne


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net