Detect if a type is a std::tuple?

后端 未结 5 966
天涯浪人
天涯浪人 2020-12-31 05:01

Currently I have two functions :

template bool f(Type* x);
template bool f(std::tuple* x);
         


        
5条回答
  •  执念已碎
    2020-12-31 05:19

    With C++17, here is a fairly simple solution using if constexpr

    template  struct is_tuple: std::false_type {};
    
    template  struct is_tuple>: std::true_type {};
    

    Then you can do something like:

    template bool f(Type* x) {
        if constexpr (is_tuple::value) {
            std::cout << "A tuple!!\n";
            return true;
        }
    
        std::cout << "Not a tuple\n";
        return false;
    }
    

    A test to ensure it worked:

    f(&some_tuple);
    f(&some_object);
    

    Output:

    A tuple!!
    Not a tuple


    Solution taken in part from an answer found here: How to know if a type is a specialization of std::vector?

提交回复
热议问题