如何检查模板参数的类型?

Wha*_*rld 71 c++ templates

假设我有一个模板函数和两个类

class animal {
}
class person {
}

template<class T>
void foo() {
  if (T is animal) {
    kill();
  }
}
Run Code Online (Sandbox Code Playgroud)

我怎么检查T是动物?我不想在运行时检查一些东西.谢谢

Ker*_* SB 99

用途is_same:

#include <type_traits>

template <typename T>
void foo()
{
    if (std::is_same<T, animal>::value) { /* ... */ }  // optimizable...
}
Run Code Online (Sandbox Code Playgroud)

通常,这是一个完全不可行的设计,你真的想要专攻:

template <typename T> void foo() { /* generic implementation  */ }

template <> void foo<animal>()   { /* specific for T = animal */ }
Run Code Online (Sandbox Code Playgroud)

另请注意,具有显式(非推导)参数的函数模板是不常见的.这并不是闻所未闻,但往往有更好的方法.

  • @WhatABeautifulWorld:你总是可以分解你的代码,以便依赖于类型的部分可以降级为一个特殊的函数...... (5认同)
  • 快速跟进,如果我确实使用 std::is_same,那么它不会减慢其他模板参数的代码,对吗? (3认同)
  • 谢谢!实际上他们共享了很多代码,所以我无法真正复制它 (2认同)
  • @AdriC.S.:由于没有推导出 `T`,所以你无能为力。您可以保留未实现的主模板并创建一个专门化,或者您可以添加一个带有 `is_same` 的静态断言。 (2认同)

Кон*_*ков 24

我想今天,最好使用,但只能使用C++ 17.

#include <type_traits>

template <typename T>
void foo() {
    if constexpr (std::is_same_v<T, animal>) {
        // use type specific operations... 
    } 
}
Run Code Online (Sandbox Code Playgroud)

如果在if表达式主体中没有使用某些特定于类型的操作,则constexpr此代码将无法编译.

  • 而不是`std :: is_same <T,U> :: value`你可以使用更短的:`std :: is_same_v <T,U>` (6认同)
  • @serup 它更好,因为它是新的,而旧的东西总是更糟。`inline constexpr bool is_same_v = is_same&lt;T, U&gt;::value;` 我不知道为什么他们不断添加这些毫无意义的“帮助者”,这只会让人们感到困惑,而不是修复他们破碎的语言。 (2认同)

sca*_*cai 10

std::is_same()仅自 C++11 起可用。对于 C++11 之前的版本,您可以使用typeid()

template <typename T>
void foo()
{
    if (typeid(T) == typeid(animal)) { /* ... */ }
}
Run Code Online (Sandbox Code Playgroud)


Edw*_*att 8

在C ++ 17中,我们可以使用variants

要使用std::variant,您需要包含标题:

#include <variant>
Run Code Online (Sandbox Code Playgroud)

之后,您可以std::variant像这样添加代码:

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...
    }
}
Run Code Online (Sandbox Code Playgroud)

  • T和Type如何连接? (7认同)
  • 这个答案在几个方面都是有问题的。除了实际的错误(“类型”(即类型“类型”的值或此处没有意义的模板)之外,“ is_same_v”在“变量”的上下文中没有意义。对应的“特征”是“ holds_alternative”。 (2认同)

tem*_*boy 5

您可以根据传递到参数中的内容来专门化模板,如下所示:

template <> void foo<animal> {

}
Run Code Online (Sandbox Code Playgroud)

请注意,这会根据传递的类型创建一个全新的函数T.这通常是优选的,因为它减少了混乱,并且基本上是我们首先拥有模板的原因.