c++ - 有没有办法在变量中存储类型说明符?

标签 c++ type-conversion decltype typeid

有没有办法在变量中存储类型说明符?我四处搜索并找到了 typeid 和 decltype,但它们都不能像我希望的那样工作。 decltype不生成变量,typeid通常用于typeid().name()。

由于 typeid().name() 将类型说明符输出为 const char*,可以存储和传递,是否有将 const char* 转换回类型说明符的函数?喜欢:

int a = 1;
auto t = typeid(a).name();
some_function(t) b = 1; //gives int b

我知道 decltype 正是这样做的,但它不能“存储”类型说明符。

在我最近的项目中,我遇到了一个奇怪的情况,即每次调用时函数参数的类型可能会有所不同。通常使用函数模板就可以,但它要么需要将所有代码放在本项目不允许的 header 中,要么使用显式实例化,这可能会变得困惑,因为很难列出所有类型。此外,针对每种类型编写多个函数重载也非常困惑。

如果有办法将类型说明符存储为变量,那么我可能会使用 void* 传递输入的地址,并将包含类型说明符的变量传递给函数,然后将 void* 转换回其原始类型?

在某种程度上,typeid().names() 已经提供了一种存储和传递变量类型的方法,我想知道是否有一种优雅的方法可以将它转换回类型说明符而无需编写长长的“strcmp”列表或其他手动转换。

谢谢!

编辑: 关于我的项目的更多信息,该函数用作私有(private)类成员的 setter ,第一个输入是关于成员 ID 的枚举,第二个是值。该类也可以被继承。该项目要求在 header 中放置尽可能少的代码。本项目使用OpenCV。

在 Header.h 中:

#include "some_other_custom_class.h"
#include <string>

enum BASE_PARA
{
    A,
    B,
    C
}
class Base
{
public:
    Base() = default;
    virtual ~Base() = default;

    //<sometype> may vary regarding the member
    virtual void Set(const int& id, const <sometype>& value);

private:
    int a;
    float b;
    map<std::string, double> c;
}

enum DERIVED_A_PARA
{
    D,
    E
}
class DerivedA : public Base
{
public:
    //inherit Set()
    virtual void Set(const int& id, const <sometype>& value) override;

private:
    map<int, <some_custom_class>> d;
    vector<double> e;
}

class DerivedB : public DerivedA //and so on
{
}

在 Cpp.cpp 中:

void Base::Set(const int& id, const <sometype>& value)
{
    //processing the input regarding the private member

    //<do something here>

    switch(id)
    {
    default:
        break;
    case A:
        a = value;//which means in here <sometype> should be int
        break;
    case B:
        b = value;//<sometype> should be float
        break;
    case C:
        //<sometype> should be map<string, double>
        if (<some_code_to_ensure_value_is_not_empty>)
            return;
        c["first"] = value["first"];
        c["second"] = value["first"] + value["second"];
        break;
    }
}

void DerivedA::Set(const int& id, const <sometype>& value)
{
    //something like above
}

我知道函数模板对此很有用,但不能像上面说的那样使用它。所以我希望将“sometype”更改为 void* 以指向地址,以及从 typeid().name() 生成的 const char* 以标记实际类型。但是我找不到将 const char* 转换回类型说明符的好方法。

最佳答案

三个选项

  1. 重载集,
  2. 函数模板或
  3. 类型删除

但在某些时候,您要么必须使用模板,要么编写一定数量的样板文件/重载/分支。

[a] 类型的函数参数 [that] may vary”的最佳选择是函数模板,因为这基本上是模板的用例。

如果它们不能在项目的(可能)公共(public)头文件中,那么某种内部头文件可能会起作用。如果代码仅在特定点使用,则可以在源文件中定义模板(牺牲可重用性)。


std::any 在 C++17 中是某种用于类型删除的语法糖,如果您转换为一个类型不属于任何

void Base::Set(const int& id, std::any const& value)
{
    switch(id)
    {
        case static_cast<int>(BASE_PARA::A):
            this->a = std::any_cast<int>(value);
            break;
        // ... and so on ...
    }
}

关于c++ - 有没有办法在变量中存储类型说明符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56920735/

相关文章:

c# - 颜色转换 CMYK - RGB - Lab with WCS 使用 ICC 配置文件

c# - 从 Json.NET 中的 JToken 隐式转换

c++ - 不应该 decltype 触发其参数的编译吗?

c++ - 命名右值引用的类型是什么?

python - 在Python中将字符串转换为int,只有没有字符的数字

c++ - 一个简单的随机数排序程序

c++ - 单例的静态变量位置

c++ - 引用容器/不可空指针

c++ - 带有双括号的 decltype((x)) 是什么意思?

c++ - 将 int 转换为两个字节的数组