MS 对 C++ 的扩展的有用性
Usefulness of an MS extensions to C++
Microsoft extensions to C++ 中的最后一项 "Passing a Non-Const Pointer Parameter to a Function that Expects a Reference to a Const Pointer Parameter" 是一个扩展,因此它可以被认为是一个 有益的 功能。但是,我看不到它的价值。他们展示的例子表明这是危险的。
typedef int T;
const T acT = 9; // A constant of type 'T'
const T * pcT = & acT; // A pointer to a constant of type 'T'
void func2 ( const T * & rpcT ) // A reference to a pointer to a constant of type 'T'
{
rpcT = pcT;
}
T * pT; // A pointer to a 'T'
void func ()
{
func2 ( pT ); // Should be an error, but isn't detected
* pT = 7; // Invalidly overwrites the constant 'acT'
}
为什么它是扩展而不是错误?
因为"It's not a bug, it's a feature."
是的,这显然是一件坏事。您必须询问负责的 Microsoft 工程师,但我怀疑这是一个错误,并且它得到维护只是因为在某些时候有人的软件依赖于它并且 Microsoft 希望保持向后兼容性。
但是,此文档似乎已过时:我无法让 Visual Studio 2015 或 2013 接受使用此扩展的代码。
Microsoft extensions to C++ 中的最后一项 "Passing a Non-Const Pointer Parameter to a Function that Expects a Reference to a Const Pointer Parameter" 是一个扩展,因此它可以被认为是一个 有益的 功能。但是,我看不到它的价值。他们展示的例子表明这是危险的。
typedef int T;
const T acT = 9; // A constant of type 'T'
const T * pcT = & acT; // A pointer to a constant of type 'T'
void func2 ( const T * & rpcT ) // A reference to a pointer to a constant of type 'T'
{
rpcT = pcT;
}
T * pT; // A pointer to a 'T'
void func ()
{
func2 ( pT ); // Should be an error, but isn't detected
* pT = 7; // Invalidly overwrites the constant 'acT'
}
为什么它是扩展而不是错误?
因为"It's not a bug, it's a feature."
是的,这显然是一件坏事。您必须询问负责的 Microsoft 工程师,但我怀疑这是一个错误,并且它得到维护只是因为在某些时候有人的软件依赖于它并且 Microsoft 希望保持向后兼容性。
但是,此文档似乎已过时:我无法让 Visual Studio 2015 或 2013 接受使用此扩展的代码。