Qt - QList const正确性 [英] Qt - QList const correctness
问题描述
A QList< T *>
不能容易地是const正确的。考虑函数
A QList<T *>
can't easily be const-correct. Consider the function
void f(QList<T *> list)
{
list[0]->constFunction();
}
我可以将f更改为
void f(QList<const T *> list)
$ b b
但我不能做
but then I can't do
f(QList<T *>()); //Compile error
因为编译器不能隐式转换 QList ; T *>
到 QList< const T *>
。然而,我可以明确地重新解释QList如下:
anymore, since the compiler can't implicitely cast QList<T *>
to QList<const T *>
. However, I can explicitely reinterpret-cast the QList as follows:
template <typename T> inline QList<const T *> &constList(const QList<T *> &list)
{
return (QList<const T *> &)list;
}
这使我可以使用 constList
模板函数将任何 QList
转换为 QList
如在
This enables me to use the constList
template function to cast any QList<T *>
into a QList<const T *>
, as in
f(constList(QList<T *>()));
它似乎工作正常,但它是否真的可以安全吗?
and it seems to work fine, but is it actually safe to do this?
推荐答案
您正在考虑的投射功能…
The casting function you're considering, …
template< class T >
inline QList<T const*>& constList( QList<T*> const& list )
{
return reinterpret_cast< QList<T const*>& >(
const_cast< QList<T*>& >( list )
);
}
…可能是实用的(可能 QList
不会根据元素类型的 const
-ness改变其对象表示)但它可以打破 const
正确。
… may be practical (probably QList
does not change its object representation depending on the const
-ness of the element type), but it can break const
correctness.
c $ c> const - 列表本身的值不是 const
正确:它允许你改变一个最初的 const
First, because casting away the const
-ness of the list itself is not const
correct: it allows you to change an originally const
list.
但是即使删除了形式参数 const
,像…
But even if that formal argument const
is removed, like …
template< class T >
inline QList<T const*>& constList( QList<T*>& list )
{
return reinterpret_cast< QList<T const*>& >(
list
);
}
…仍然有一个 const
正确性问题。
… there is still a const
correctness problem.
原因是列表构成一个附加级别的间接,你的函数本身不是 const
。因此,在使用你的函数获得一个引用到指向指向 - const
元素的列表之后,你可以在那个列表中存储一个指向真正 const
。然后,您可以使用原始列表修改真的 const
项目 bang 。
The reason is that the list constitutes an additional level of indirection, and with your function is not itself const
. Thus, after using your function to get a reference to the list with alleged pointer-to-const
elements, you can store in that list a pointer to something that is really const
. And then you can use the original list to modify that really const
item, bang.
It's the same reason that there is no implicit conversion from T**
to T const**
.
没有这样的问题你可以做的是,已经有 const
指向对象的指针列表,使那些指向对象 const
:
What you can do without such problems is, with an already const
list of pointers to objects, make those pointed to objects const
:
template< class T >
inline QList<T const*> const& constList( QList<T*> const& list )
{
return reinterpret_cast< QList<T const*> const& >(
list
);
}
正式还有 reinterpret_cast
作为一个潜在的问题,但任何专门化的表示的元素的元素的const的元素的任何专门化的代表可能应该得到的任何他们得到的$ QList
。 : - )
Formally there's still the reinterpret_cast
as a potential problem, but anyone specializating the representation of QList
on constness of elements would presumably deserve whatever they got. :-)
干杯& hth。,
Cheers & hth.,
这篇关于Qt - QList const正确性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!