假设我有一个模板函数和两个类
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)
另请注意,具有显式(非推导)参数的函数模板是不常见的.这并不是闻所未闻,但往往有更好的方法.
Кон*_*ков 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此代码将无法编译.
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)
在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)
您可以根据传递到参数中的内容来专门化模板,如下所示:
template <> void foo<animal> {
}
Run Code Online (Sandbox Code Playgroud)
请注意,这会根据传递的类型创建一个全新的函数T.这通常是优选的,因为它减少了混乱,并且基本上是我们首先拥有模板的原因.