将 Variant 转换为 bool

Converting a Variant to bool

如何在 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()) {}