用于装箱和拆箱的 C++ 类

标签 c++ oop templates

我想要一个 Boxed_value 类,它可以 InBox 其他类型(从 intdouble 开始, string...),类似于C#中的object。此类类型可以进一步传递给对其Unbox 的函数。这些函数将收到引用:

void receiver( Boxed_value & bv ) {}

所以,它不应该是一个模板。或者?我需要一个食谱...UnBox 函数可以是独立的和模板化的。在这种情况下,receiver 可以获得 2 个参数:

void receiver( Boxed_value & bv, Engine & eng ) {}

下面的实现是虚拟的,它没有编译。

struct Boxed_value {
    void inbox(int v) { _iv = v; }
    void inbox(double v) { _dv = v; }
    void inbox(std::string & v) { _sv = v; }

    int _iv;
    double _dv;
    std::string _sv;
};

struct Engine {
    template<typename T>
    T unbox(Boxed_value bv) { 
        if (std::is_same<T, typeid(bv._iv).name()>::value) { return _iv };
        if (std::is_same<T, typeid(bv._dv).name()>::value) { return _dv };
        if (std::is_same<T, typeid(bv._sv).name()>::value) { return _sv };
    }
};

最佳答案

  1. 请注意,在编译时,每个 if 子句都必须实例化;这意味着您将在函数 unbox 中返回不同的类型。

  2. 你应该比较 std::is_same 中的成员类型,而不是像 typeid(bv._iv).name() 这样的东西,它是不是类型,而是类型为 const char* 的对象。

可以申请Constexpr If (自 C++17 起)。

If the value is true, then statement-false is discarded (if present), otherwise, statement-true is discarded.

并使用 decltype (C++11 起)获取成员的类型。

例如

template<typename T>
T unbox(const Boxed_value& bv) { 
    if constexpr (std::is_same<T, decltype(bv._iv)>::value) { return bv._iv };
    else if constexpr (std::is_same<T, decltype(bv._dv)>::value) { return bv._dv };
    else if constexpr (std::is_same<T, decltype(bv._sv)>::value) { return bv._sv };
}

在C++17之前,可以申请full specialization .

template<typename T>
T unbox(const Boxed_value& bv);

template<>
decltype(Boxed_value::_iv) unbox<decltype(Boxed_value::_iv)> (const Boxed_value& bv) { return bv._iv; }
template<>
decltype(Boxed_value::_dv) unbox<decltype(Boxed_value::_dv)> (const Boxed_value& bv) { return bv._dv; }
template<>
decltype(Boxed_value::_sv) unbox<decltype(Boxed_value::_sv)> (const Boxed_value& bv) { return bv._sv; }

PS:我将参数类型从Boxed_value更改为const Boxed_value&,以避免不必要的复制。

关于用于装箱和拆箱的 C++ 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58576928/

相关文章:

c# - 如何绕过 dll 中的 VC++ 运行时要求?

c++ - 用 Streambuilder 替换 jsoncpp FastWriter 的方法是什么?

c++ - 如何将 matlab::data::TypedArray<double> 作为指针传递以在 MATLAB mex 文件中构造 Armadillo 矩阵?

c++ - 类链中的转换

c++ - 具有不同模板参数的模板类的默认赋值运算符

C++ 将字符串转换为类型名

c++ - 为什么我的程序多次调用拷贝构造函数?

java - 为什么应用于该对象的 System.out.println 会打印这个短语?

ruby - 在 Ruby 中不是对象的 'Things' 示例

c++ - 使用模板编译错误 - 没有匹配的调用函数