[英]C++: [VS2010]: warning C4396: 'stdext::hash_value' : the inline specifier cannot be used when a friend declaration
I am converting my VS2005 C++ code to VS2010 code. 我将VS2005 C ++代码转换为VS2010代码。 Unfortunately in VS2010 it is giving compiler warning whereas in VS2005 it compiled smoothly. 不幸的是,在VS2010中,它发出了编译器警告,而在VS2005中,它进行了平滑编译。 (FYI: I have set the warning to be treated as errors). (仅供参考:我已将警告设置为错误)。
Pls have a look at the code snippet: 请看一下代码片段:
The error is at the line where the friend declaration is there. 错误在朋友声明所在的行。
class __declspec(dllexport) MyKey
{
friend size_t stdext::hash_value<MyKey>(const MyKey& key); // compiler warning at this line (pls see below for the actual compiler warning)
ubit32 m_uKey1;
};
template<> inline size_t stdext::hash_value<MyKey>(const MyKey& key)
{
return key.m_uKey1;
}
Here is the compiler warning as shown below: 这是编译器警告,如下所示:
warning C4396: 'stdext::hash_value' : the inline specifier cannot be used when a friend declaration refers to a specialization of a function template
Please help to resolve this error for me. 请帮我解决此错误。 Thanks. 谢谢。
I got the fix by adding the following two forward declaration statements before the class declaration for MyKey. 我通过在MyKey的类声明之前添加以下两个前向声明语句来解决此问题。
class MyKey;
template<> size_t stdext::hash_value<MyKey>(const MyKey& key);
Now the error/warning is gone. 现在错误/警告消失了。 Am I doing it correctly ? 我做对了吗?
Since your friend declaration doesn't have the inline
specifier, this is obviously a bug in MSVC compiler. 由于您的朋友声明没有inline
说明符,因此这显然是MSVC编译器中的错误。 You can suppress the warning with a compiler option or a pragma: 您可以使用编译器选项或编译指示取消警告:
#pragma warning(disable: 4396)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.