gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:44:19 30 4
gpt4 key购买 nike

我想要一个 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com