boolean - 将 Variant 转换为 bool

标签 boolean c++builder c++builder-10-seattle

如何在 C++Builder 10 中将 Variant 转换为 bool?

在旧的 bcc32 编译器中,我使用以下代码检查是否启用了一些通用 TComponent:

if ((bool)GetPropValue(c, "Enabled", false))
    do_something();

但是,在升级到 C++Builder 10 并启用新的基于 Clang 的编译器后,我收到以下错误:

[CLANG Error] VclHelpers.cpp(1375): ambiguous conversion for C-style cast from 'System::Variant' to 'bool'

完整的编译器消息表明 Variant 的 36 个转换运算符被认为是合法的候选者:operator double()operator wchar_t* 等。

最佳答案

问题是 Variant 提供了太多的转换运算符。特别是,以下运算符使到 bool 的转换不明确:

__fastcall operator bool() const;
__fastcall operator signed char*();
__fastcall operator unsigned char*();
// etc. - Variant has similar operators for short, int, long, float, double...
// It calls these "by ref" conversions.

据我了解,非 const 重载通常优于 const 重载,但是对于非 const 可转换到 bool 指针转换以及 const bool 转换的 >1 替代方案,转换是不明确的。

Variant 将其转换设计为无法在没有歧义的情况下使用它们可能是错误的,但是在@user2672165 和@Remy Lebeau 的评论的帮助下,有几个解决方法:

// Convert to int instead of bool.
if ((int)GetPropValue(c, "Enabled", false)) {}

// Make the Variant const, to avoid the ambiguous non-const conversions.
if (const_cast<const Variant>(GetPropValue(c, "Enabled", false))) {}

// Explicitly invoke the desired conversion operator.
if (GetPropValue(CheckBox1, "Enabled", false).operator bool()) {}

// Use TRttiProperty instead of Variant.
RttiContext ctx;
if (ctx.GetType(c->ClassType())->GetProperty("Enabled")->GetValue(c).AsBoolean()) {}

关于boolean - 将 Variant 转换为 bool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32402059/

相关文章:

openssl - 在 C++Builder 中使用 OpenSSL

java - 检查两个数组是否具有相同顺序的相同元素的方法

Java 二维 boolean 数组值

c++ - 求和 float c++

c++ - 在 C++ Builder 6 中拖放图像

c++builder - 在 C++ Builder 10 西雅图中使用 Web.Win.Sockets

javascript - 如何在React组件中进行两次条件检查

objective-c - 节省 boolean 值的问题

c++builder - C++ Builder 版本的相对健壮性是什么?

c++ - TGridOptions 上二进制表达式的无效操作数