考虑功能foo
。
template <typename T>
void foo() {
do_something();
if (T == int) {
do_somehting_else();
}
}
换句话说,我想要它do_something()
,然后,如果类型是int
,do_something_else()
当然if (T == int) {
不会编译。还有:在C ++中,有什么方法可以在编译时比较类型吗?
您可以使用
#include <type_traits>
// ...
template <typename T>
void foo() {
do_something();
if (std::is_same<T,int>::value) {
do_somehting_else();
}
}
您可以在此处了解有关类型特征的更多信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句