[英]GCC warnings for user types using QVariant::Type in a switch block
Basically we're keeping the track of object types in a variable of QVariant::Type and then doing something like that 基本上我们将对象类型的跟踪保存在QVariant :: Type的变量中,然后执行类似的操作
switch(values[i].type)
{
case QVariant::Bool: logic; break;
case QVariant::Int: logic; break;
case QVariant::LongLong: logic; break;
case QVariant::String: logic; break;
case QVariant::Double: logic; break;
case QVariant::DateTime:
case QVariant::Date:
case QVariant::Time: logic; break;
case QVariant::User+1:
{
logic;break;
}
case QVariant::User+2:
{
logic;break;
}
default: break;
}
The problem is: gcc produces warnings along these lines for User+X statements: 问题是:gcc为User + X语句沿着这些行产生警告:
warning: case value ‘1025’ not in enumerated type ‘QVariant::Type’ [-Wswitch]
Now, I could suppress that of course, but is that the recommended way or am I doing something fundamentally wrong here? 现在,我当然可以压制它,但这是推荐的方式还是我在这里做了一些根本错误的事情?
PS The question isn't about why the warning is produced: I understand why. PS的问题不是为什么产生告警:我明白为什么。 The queston is more about using QVariant::Type with user types in a proper way and if simply suppressing is correct or what am I doing here is just plain wrong and warning is an indication of a bigger design decision problem.
问题更多的是以正确的方式使用QVariant :: Type和用户类型,如果简单地抑制是正确的,或者我在这里做的事情是完全错误的,并且警告表明更大的设计决策问题。
If you are going to use user types in the QVariant, you should use the member function userType : 如果要在QVariant中使用用户类型,则应使用成员函数userType :
Unfortunately, this will make things uglier: 不幸的是,这会让事情变得更加丑陋:
auto u = [](auto& v){return static_cast<int>(v);};
switch(values[i].userType())
{
case u(QVariant::Bool): logic; break;
case u(QVariant::Int): logic; break;
case u(QVariant::LongLong): logic; break;
case u(QVariant::String): logic; break;
case u(QVariant::Double): logic; break;
case u(QVariant::DateTime):
case u(QVariant::Date):
case u(QVariant::Time): logic; break;
case u(QVariant::User)+1:
{
logic;break;
}
case u(QVariant::User)+2:
{
logic;break;
}
default: break;
}
To clarify: Note that the type()
member function will always return QVariant::UserType
when the type value is greater than QMetaType::User
. 澄清:请注意,当类型值大于
QMetaType::User
时, type()
成员函数将始终返回QVariant::UserType
。 This means it will never return QVariant::UserType+n
! 这意味着它永远不会返回
QVariant::UserType+n
! How could it? 怎么可能呢? Those values are not part of the enumeration.
这些值不是枚举的一部分。
This is what gcc is warning you about: 这就是gcc警告你:
warning: case value ‘1025’ not in enumerated type ‘QVariant::Type’ [-Wswitch]
Since 1025
( QVariant::User+1
) is not part of QVariant::Type
, this case label is essentially dead code. 由于
1025
( QVariant::User+1
)不是QVariant::Type
一部分,因此该案例标签基本上是死代码。
The userType
member function, however, returns int
. 但是,
userType
成员函数返回int
。 It will return the same underlying value as type
when that value is below QMetaType::User
, and also the correct user value when above. 当该值低于
QMetaType::User
时,它将返回与type
相同的底层值,并且当上面的值时也返回正确的用户值。
From QVariant::type enum
: 来自
QVariant::type enum
:
QMetaType::User 1024 Base value for user types
this is like: 这就像:
switch(1024) {
...
case (1024 + 1): break;
default: break;
}
the governing rule in switch for enum
is to be explicit . enum
中switch的管理规则应该是明确的 。
(1024 + 1)
is not explicit thus the warning. 因此警告
(1024 + 1)
不明确。
UPDATE UPDATE
As per qvariant.html#type 根据qvariant.html #type
Returns the storage type of the value stored in the variant.
返回变量中存储的值的存储类型。 Although this function is declared as returning QVariant::Type, the return value should be interpreted as QMetaType::Type.
虽然此函数声明为返回QVariant :: Type,但返回值应解释为QMetaType :: Type。 In particular,
QVariant::UserType
is returned here only if the value is equal or greater than QMetaType::User.特别是, 仅当值等于或大于QMetaType :: User 时才返回
QVariant::UserType
。
Thus, QVariant::UserType + 1
will be dead code. 因此,
QVariant::UserType + 1
将是死代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.