Detect if a type is a std::tuple?

后端 未结 5 964
天涯浪人
天涯浪人 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:22

    Might be a little late but you can also do something like this, in a more modern c++17 style with template variables:

    template 
    constexpr bool IsTuple = false;
    template
    constexpr bool IsTuple>   = true;
    

    And some tests

    struct TestStruct{};
    
    static_assert(IsTuple == false,                "Doesn't work with literal.");
    static_assert(IsTuple == false,         "Doesn't work with classes.");
    static_assert(IsTuple>,       "Doesn't work with plain tuple.");
    static_assert(IsTuple>,     "Doesn't work with lvalue references");
    static_assert(IsTuple>,   "Doesn't work with rvalue references");
    

    You can view it here https://godbolt.org/z/FYI1jS

    EDIT: You will want to run std::decay, std::remove_volatile, std::remove_const to handle special cases.

提交回复
热议问题