简体   繁体   English

如何在条件语句中检查变量的类型

[英]How to check the type of variable in conditional statement

How can I check the type of a input variable inside if clause in C++? 如何在C ++中的if子句中检查输入变量的类型? If there is any member function to do this. 如果有任何成员函数可以执行此操作。

It depends on what type checks you want to do. 这取决于您要执行的类型检查。

The simplest is probably 最简单的可能是

 #include <typeinfo>     // for the `std::type_info` type

 if (typeid(input_variable) == typeid(chosen_type))
 {
      // input_variable is of type chosen_type
 } 

It is also possible to check the (implementation defined) name strings that identify a type 还可以检查(实现定义的)标识类型的名称字符串

 if (std::string(typeid(input_variable).name()) == typeid(chosen_type).name())
 {
      // input_variable is of type chosen_type
 } 

The conversion to std::string is needed for comparison operators to work, as the .name() member function returns const char * . 为了使比较运算符起作用,需要转换为std::string ,因为.name()成员函数返回const char * Otherwise compare the name() members using strcmp() (either in C's <string.h> or in C++ <cstring> - within namespace std ). 否则,使用strcmp() (在C的<string.h>或C ++ <cstring> -名称空间std )比较name()成员。

Bear in mind that the character sequence returned by typeid(X).name is implementation defined. 请记住, typeid(X).name返回的字符序列是实现定义的。

In C++11, the type_info type has a hash_code() member, which may also be compared. 在C ++ 11中, type_info类型具有hash_code()成员,也可以将其进行比较。 The values of this are implementation defined, and may vary between executions of the program. 其值是实现定义的,并且在程序执行之间可能有所不同。 Furthermore, as Martin Bonner mentioned in comments, that hash_code() may give false positives for equality (if hash_code() s compare non-equal, the types are different but if they compare equal the types may be different. I mention this, not because I advocate comparing hash_code() s, but because the original question has not explained why comparison of types is desired, so there is no basis to assume a test that might yield false matches is inappropriate. 此外,正如马丁·邦纳(Martin Bonner)在评论中提到的那样, hash_code()可能会给出相等的误报(如果hash_code()的比较不相等,则类型不同,但是如果它们相等,则类型可能会不同。因为我主张比较hash_code() ,但是由于原始问题并未解释为什么要进行类型比较,所以没有理由假设可能产生错误匹配的测试是不合适的。

You can try to use: 您可以尝试使用:

typeid(yourvariable).name()

You need to include the following header to make it working: 您需要包括以下标头才能使其正常工作:

#include <typeinfo>

An easy to use solution would be the following one: 一种易于使用的解决方案如下:

#include<cassert>

struct B { static int cnt; };
int B::cnt = 0;

template<class T>
struct S: B { static int type; };

template<typename T>
int S<T>::type = B::cnt++;

template<typename T, typename U>
bool f(T, U) {
    return S<T>::type == S<U>::type;
}

int main() {
    assert(f(42, 0));
    assert(!f(0, .0));
}

You can use S<T>::type in a guard statement or wherever you want. 您可以在保护声明中或任何需要的地方使用S<T>::type
If you have a variable named x , is a matter of using something like: 如果您有一个名为x的变量,则可以使用类似以下内容的问题:

S<decltype(x)>::type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM