Check if parameter pack contains a type

后端 未结 3 973
孤街浪徒
孤街浪徒 2020-12-16 05:15

I was wondering if C++0x provides any built-in capabilities to check if a parameter pack of a variadic template contains a specific type. Today, boost:::mpl::contains can be

3条回答
  •  旧巷少年郎
    2020-12-16 05:35

    No, you have to use (partial) specialization with variadic templates to do compile-time computations like this:

    #include 
    
    template < typename Tp, typename... List >
    struct contains : std::true_type {};
    
    template < typename Tp, typename Head, typename... Rest >
    struct contains
    : std::conditional< std::is_same::value,
        std::true_type,
        contains
    >::type {};
    
    template < typename Tp >
    struct contains : std::false_type {};
    

    There is only one other intrinsic operation for variadic templates and that is the special form of the sizeof operator which computes the length of the parameter list e.g.:

    template < typename... Types >
    struct typelist_len
    {
       const static size_t value = sizeof...(Types);
    };
    

    Where are you getting "it has serious compilation-time overhead" with boost mpl from? I hope you are not just making assumptions here. Boost mpl uses techniques such as lazy template instantiation to try and reduce compile-times instead of exploding like naive template meta-programming does.

提交回复
热议问题