How to check for the type of a template parameter?

前端 未结 4 1911
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 04:11

Suppose I have a template function and two classes

class animal {
}
class person {
}

template
void foo() {
  if (T is animal) {
    kill();
          


        
相关标签:
4条回答
  • 2020-11-28 04:22

    Use is_same:

    #include <type_traits>
    
    template <typename T>
    void foo()
    {
        if (std::is_same<T, animal>::value) { /* ... */ }  // optimizable...
    }
    

    Usually, that's a totally unworkable design, though, and you really want to specialize:

    template <typename T> void foo() { /* generic implementation  */ }
    
    template <> void foo<animal>()   { /* specific for T = animal */ }
    

    Note also that it's unusual to have function templates with explicit (non-deduced) arguments. It's not unheard of, but often there are better approaches.

    0 讨论(0)
  • 2020-11-28 04:22

    In C++17, we can use variants.

    To use std::variant, you need to include the header:

    #include <variant>
    

    After that, you may add std::variant in your code like this:

    using Type = std::variant<Animal, Person>;
    
    template <class T>
    void foo(Type type) {
        if (std::is_same_v<type, Animal>) {
            // Do stuff...
        } else {
            // Do stuff...
        }
    }
    
    0 讨论(0)
  • 2020-11-28 04:28

    I think todays, it is better to use, but only with C++17.

    #include <type_traits>
    
    template <typename T>
    void foo() {
        if constexpr (std::is_same_v<T, animal>) {
            // use type specific operations... 
        } 
    }
    

    If you use some type specific operations in if expression body without constexpr, this code will not compile.

    0 讨论(0)
  • 2020-11-28 04:39

    You can specialize your templates based on what's passed into their parameters like this:

    template <> void foo<animal> {
    
    }
    

    Note that this creates an entirely new function based on the type that's passed as T. This is usually preferable as it reduces clutter and is essentially the reason we have templates in the first place.

    0 讨论(0)
提交回复
热议问题