How to check for the type of a template parameter?

前端 未结 4 1929
佛祖请我去吃肉
佛祖请我去吃肉 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:39

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

    template <> void foo {
    
    }
    

    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.

提交回复
热议问题