Search code examples
c++templatestemplate-meta-programmingtype-traits

c++ template meta programming: how do you define the type trait "is_xxxxx_v" alias for my custom trait?


So I have been looking through this: https://akrzemi1.wordpress.com/2017/12/02/your-own-type-predicate/

It shows that you can take a custom trait (or predicate) is_thing<T>::type and alias that to is_thing_t<T> - e.g.:

template <bool Cond>
using enable_if_t = typename enable_if<Cond>::type;

Now I want to do the same trick to implement the value alias used in other traits. I have a snippet of code below with my attempt:

// Primary template
// tparam 1 (T) is to pass in the class-under-test. 
// tparam 2 (deafult to void) is to catch all cases not specialised - so they evaluate to false
template <typename T, typename = void>
struct is_really_bob : std::false_type {};

// specialisation
// tparam 1 (T) is to pass in the class-under-test. 
// tparam 2 special meta-function that - if our class best-matches THIS template then it will evaluate to true
template <typename T>
struct is_really_bob <T, std::void_t<
    typename T::result_type,                 // Has a nested type called result_type
    decltype(T::check_bob()),                // Has a static member fn called check_bob
    decltype(std::is_default_constructible_v<T>), // required for below:
    decltype(T{}.set_bob(std::string{""})),  // has member fn set_bob that takes a string (also requires default c'tor)
    decltype(T{}.get_bob())                  // has member fn get_bob (also requires default c'tor)
>> : std::true_type {};

///// THIS FAILS /////
template <bool T>
using is_really_bob_v = typename is_really_bob<T>::value;

The problem here is that ::value is not a type name. I don't know the syntax to generate a value alias. Is there a way to do this? I want to end up being able to do something like:

if constexpr (is_really_bob_v<T>)
{
    // ...
}

Full code example is here: https://godbolt.org/z/dG11vxesW - not it has the attempt at ::value alias commented out so we can at least see the rest of the code working


Solution

  • You have two problems in your definition of is_really_bob_v:

    1. You are declaring it as template <bool T> while the template needs a type.
    2. You are trying to use a type alias to alias a boolean value.

    It should be

    template <typename T>
    //        ^^^^^^^^ not bool
    inline constexpr bool is_really_bob_v = is_really_bob<T>::value;
    //^^^^^^^^^^^^^^^^^^^no type alias     ^ no typename
    

    Using my above correction the following works as expected:

    struct BobTest {
      BobTest() = default;
      using result_type = bool;
    
      void set_bob(std::string) {}
    
      std::string get_bob() { return "bob"; }
    
      static void check_bob() {}
    };
    
    struct NonBobTest {
      NonBobTest() = default;
      // using result_type = bool;
    
      void set_bob(std::string) {}
    
      std::string get_bob() { return "bob"; }
    
      static void check_bob() {}
    };
    
    int main() {
      std::cout << "Is BobTest a real bob? " << is_really_bob_v<BobTest> << "\n";
      std::cout << "Is NonBobTest a real bob? "
                << is_really_bob_v<NonBobTest> << "\n";
    }
    

    Output:

    Is BobTest a real bob? 1
    Is NonBobTest a real bob? 0