繁体   English   中英

如何比较 C++ 中的类型?

[英]How can I compare types in C++?

有没有办法比较 C++ 中的变量类型? 例如,我想要这样的东西:(使用伪语言)

template <class T> void checkType(T variable) {
    if(type_of(T) == int) cout << "The variable is of type int.\n";
}

编辑 1:我尝试使用 is_same,但它在 Xcode 中不起作用……但是当我尝试在 Atom 中使用 Script 包运行的以下简单代码中使用它时。

#include <iostream>

using namespace std;

template <class T> void print(T value) {
  if(is_same<T, char> :: value) cout << "char\n";
  if(is_same<T, int> :: value) cout << "int\n";
  if(is_same<T, string> :: value) cout << "string\n";
}

int main() {
  string var1;
  int var2;
  char var3;

  print(var1);
  print(var2);
  print(var3);

  return 0;
}

编辑 2:我把不起作用的代码放在这里。 现在我尝试评论有关字符串的部分,并且代码适用于 int 和 char。

template <class keytype, class attrtype>
void LinkedList <keytype, attrtype> :: insert(keytype k, attrtype a) {
    LinkedList <keytype, attrtype> :: position iter = l.head();
    
    if(is_same<keytype, int> :: value) {
        while(iter != NULL and k > iter -> key) {
            iter = l.next(iter);
        }
        
        l.insert(iter, k, a);
    }
    
    else if(is_same<keytype, char> :: value) {
        while(iter != NULL and tolower(k) > tolower(iter -> key)) {
            iter = l.next(iter);
        }
        
        l.insert(iter, k, a);
    }
     //Whatever type I pass by the template in 'keytype' enters this if statement
    else if(is_same<keytype, string> :: value) {
        bool node_filled = false;
        
        if(iter == NULL) {
            l.insert(iter, k, a);
            node_filled = true;
        }
        else {
            unsigned long rif = 0;
            int i = 0;
            
            while(!node_filled and iter != NULL) {
                if(tolower(iter -> key.at(0)) > tolower(k.at(0))) {
                    l.insert(iter, k, a);
                    node_filled = true;
                }
                else if(tolower(iter -> key.at(0)) < tolower(k.at(0))) {
                    iter = l.next(iter);
                }
                else if(tolower(iter -> key.at(0)) == tolower(k.at(0))) {
                    if(k.size() > iter -> key.size())
                        rif = iter -> key.size();
                    else
                        rif = k.size();
                    
                    while((i < rif - 1) and (k.at(i) == iter -> key.at(i))) {
                        i ++;
                    }
                    
                    if(tolower(iter -> key.at(i)) > tolower(k.at(i))) {
                        l.insert(iter, k, a);
                        node_filled = true;
                    }
                    
                    else if(tolower(iter -> key.at(i)) == tolower(k.at(i))) {
                        if(k.size() < iter -> key.size()) {
                            l.insert(iter, k, a);
                            node_filled = true;
                        }
                        else {
                            iter = l.next(iter);
                        }
                    }
                    
                    else if(tolower(iter -> key.at(i)) < tolower(k.at(i))) {
                        iter = l.next(iter);
                    }
                }
            }
            
            if(!node_filled) {
                l.insert(NULL, k, a);
            }
        }
    }
}

查看 EDIT 2 中的代码,您可能希望在那里使用if constexpr (假设编译器符合 C++17)。 这里的问题是,通常编译器需要对 if 语句的所有分支的代码进行语法检查,即使逻辑上对于给定的模板实例化可能只采用一个分支。

if constexpr放宽此要求:未采用的分支被“丢弃”,这意味着如果未采用分支,则不会实例化依赖于模板参数的值。 这意味着它永远不会尝试执行,例如,当keytypeintk.at(0) ,因为如果是这种情况,我们将不会进入该分支。

假设您的编译器支持 C++17(并且该标准已启用!),将代码中的所有if替换为if constexpr应该可以解决此特定问题。

这对我有用。

#include <iostream>
#include <string>
#include <type_traits>

using namespace std;

template <class T>
void print(T value) {
  if (is_same<T, char>::value) cout << "char\n";
  if (is_same<T, int>::value) cout << "int\n";
  if (is_same<T, string>::value) cout << "string\n";
}

int main() {
  string var1;
  int var2;
  char var3;

  print(var1);
  print(var2);
  print(var3);

  return 0;
}

现场演示

在 C++17 中,您可以使用if constexpr让编译器生成更优化的代码生成:

#include <iostream>
#include <string>
#include <type_traits>

using namespace std;

template <class T>
void print(T value) {
  if constexpr (is_same_v<T, char>) cout << "char\n";
  if constexpr (is_same_v<T, int>) cout << "int\n";
  if constexpr (is_same_v<T, string>) cout << "string\n";
}

int main() {
  string var1;
  int var2;
  char var3;

  print(var1);
  print(var2);
  print(var3);

  return 0;
}

现场演示

在 C++ 中有typeid()运算符,它返回一个std::type_info ,这可以以多种方式使用。

你像这样使用std::type_info::hash_code()

template<typename T>
void print()
{
    if (typeid(T).hash_code() == typeid(char).hash_code())
        std::cout << "char\n"
    else if (typeid(T).hash_code() == typeid(int).hash_code())
        std::cout << "int\n"
    else if (typeid(T).hash_code() == typeid(std::string).hash_code())
        std::cout << "std::string\n"
}

或者,您可以像这样使用std::type_info::name()

template<typename T>
void print()
{
    std::cout << typeid(T) << '\n';
}

只是当心后者,如果你把类似std::string东西,你最终会得到类似class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > for一个输出。 这完全是因为std::stringusing定义class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >

编辑:我忘了说,你需要一个#include <typeinfo>来使用std::type_info

你可以试试这个:

template <class T> void checkType(T variable) {
    switch(sizeof(T)){
    case sizeof(int) :
        cout << "The variable is of type int.\n";
        break;
    case sizeof(char):
        cout << "The variable is of type char.\n";
        break;
    }
}

观察:如果您有一个向量,则必须对其进行特殊处理,例如将 switch 参数的 sizeof int 除以向量的分配数。

暂无
暂无

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

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