【问题标题】:C++ table with names of data types具有数据类型名称的 C++ 表
【发布时间】:2020-10-15 22:00:36
【问题描述】:

我想实现一个包含一些数据类型名称的表,这样我就可以在cout我的函数的循环中使用它们。怎么做?我已经尝试过使用我的代码中的表格,但它不起作用。可能是指针的东西。

#include <iostream> 
#include <limits> 
#include <iomanip> 

using namespace std; 

template<typename T> 

void sizeF(string opis) { 
    int x = sizeof(T);
    if (x==1)
        cout << "Rozmiar typu "+opis+" to " << x << " bajt.\n";
    else if (x==2 || x==4)
        cout << "Rozmiar typu "+opis+" to " << x << " bajty.\n";
    else
        cout << "Rozmiar typu "+opis+" to " << x << " bajtów.\n";
} 

template<typename T> 
void maxMinF(string opis)
{
    cout << opis << ": minimalna wartosc: " << numeric_limits<T>::min() << ", maksymalna wartosc: " << numeric_limits<T>::max() << endl;
}

int main() 
{ 
    char tab[1][15] = {"short int"};
    sizeF<tab[0]>(tab[0]); 
    sizeF<int>("int");
    sizeF<unsigned long long>("unsigned long long");
    sizeF<bool>("bool");
    sizeF<char>("char");
    sizeF<double>("double");
    sizeF<long double>("long double");
    sizeF<long long>("long long");
    sizeF<short>("short");
    sizeF<unsigned short>("usigned short");

    cout << endl;

    maxMinF<short int>("short int"); 
    maxMinF<int>("int");
    maxMinF<unsigned long long>("unsigned long long");
    maxMinF<bool>("bool");
    maxMinF<char>("char");
    maxMinF<double>("double");
    maxMinF<long double>("long double");
    maxMinF<long long>("long long");
    maxMinF<short>("short");
    maxMinF<unsigned short>("usigned short");
    return 0; 
}

【问题讨论】:

  • 您可以使用std::type_info 获取类型名。要使它们解构,您需要使用编译器内部的特殊函数(例如,abi::__cxa_demangle 用于 GCC)。
  • 在 C++ 中使用 std::vector<:variant unsigned long bool char double ...>>。如果 std::variant 不可用,您可以使用 boost::variant 或使用 union : ``` struct Type{ union { int i;无符号长长我; //其他类型 } enum class UsedType { IntType, ... } std::string data; };然后... std::vector 类型; ``` 您现在可以通过 UsedType 枚举来区分类型。
  • cout &lt;&lt; "Rozmiar typu "+opis+" to " &lt;&lt; x 中,您不必要地创建了 2 个临时字符串。将它们直接传递给 cout 以避免那些临时字符串:cout &lt;&lt; Rozmiar typu " &lt;&lt; opis &lt;&lt; " to " &lt;&lt; x
  • @GrzegorzGłowacki 你能告诉我更多关于 IntType 的信息以及接下来如何在我的函数中使用 std::vector 类型吗?

标签: c++ types


【解决方案1】:

我认为你在做你正在做的事情时走的是一条糟糕的路。

您可能应该参考Boost.TypeIndex library。看看这个:

#include <iostream>
#include <boost/type_index.hpp>

class Widget {
};

int main() {
    Widget w1;
    Widget& w2 = w1;
    Widget const w3;
    Widget const& w4 = w1;
    std::cout << boost::typeindex::type_id_with_cvr<decltype(w1)>() << '\n';
    std::cout << boost::typeindex::type_id_with_cvr<decltype(w2)>() << '\n';
    std::cout << boost::typeindex::type_id_with_cvr<decltype(w3)>() << '\n';
    std::cout << boost::typeindex::type_id_with_cvr<decltype(w4)>() << '\n';
}

输出完全符合您的期望:

Widget                                                                                                                       
Widget&                                                                                                                      
Widget const                                                                                                                 
Widget const&

Here's the demo.

【讨论】:

  • 感谢您的回答。我在安装 Boosted 库时遇到了一些问题。最后我不知道如何将它添加到我在 VS Code 中的代码中。有更简单的方法吗?
  • @GoldenRC,坦率地说,我不知道,尤其是如果您使用的是 Windows/Mac。我在 ArchLinux 上,安装普通的东西或前沿的东西很容易。也许您可以搜索现有问题。我猜肯定有一个。也许你可以接受我的,不是吗?我也添加了一个演示。
猜你喜欢
  • 1970-01-01
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-25
  • 2022-06-12
相关资源
最近更新 更多