How can I avoid writing `::value` and `::type` when using `std::enable_if`? [cppx]

后端 未结 3 2050
死守一世寂寞
死守一世寂寞 2021-02-01 07:39

Note: This is a question-with-answer in order to document a technique that others might find useful, and in order to perhaps become aware of others’

3条回答
  •  再見小時候
    2021-02-01 07:57

    In C++14, variable templates make type traits a lot more comfortable to look at. Combine that with C++11 template aliases, and all the cruft disappears:

    template 
    bool is_base_of_v = std::is_base_of::value;
    
    template 
    using enable_if_t = typename std::enable_if::type;
    

    Usage:

    template 
    enable_if_t, Foo> some_function(B & b, D & d) { /* ... */ }
    

    "Type" aliases of the form _t are in fact planned as part of the standard library for C++14, see [meta.type.synop].

提交回复
热议问题