【发布时间】:2009-10-08 11:14:38
【问题描述】:
在打开 MFC 的 VS2008 中编译以下代码时,我收到警告。提升版本 1.39
include "boost/flyweight.hpp"
include "boost/flyweight/key_value.hpp"
class Foo
{
public:
Foo(const CString& item) : mfoo(item) {}
const CString& getkeyvalue() const {return mfoo;}
private:
const CString mfoo;
};
struct Conversion
{
const CString& operator() (const Foo& item) const {return item.getkeyvalue();}
};
using namespace boost::flyweights;
flyweight<key_value<CString, Foo, Conversion>, tag<Foo> > flyweight_test;
上面代码的最后一行产生了一个警告
d:\work\sourcecode\boost1390\boost\functional\hash\extensions.hpp(72):警告 C4800:'const wchar_t *':强制值为 bool 'true' 或 'false'(性能警告)
d:\work\sourcecode\boost1390\boost\functional\hash\extensions.hpp(71) : 在编译类模板成员函数size_t boost::hash<T>::operator ()(const T &) const 时使用
[T=ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t>>
]
d:\work\sourcecode\boost1390\boost\multi_index\hashedindex.hpp(1159) :请参阅使用
编译的类模板实例化 'boost::hashT=ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t>>
]
此警告通过散列工厂、MPL 等持续不断。
为什么会出现警告以及如何更正代码以不产生警告?
编辑:
要修复,请在下面添加 hash_value 的实现
template<typename CharType, typename TraitsType>
std::size_t hash_value(const ATL::CStringT<CharType, TraitsType>& s)
{
return CStringElementTraits<typename TraitsType>::Hash(s);
}
【问题讨论】:
-
我的代码中有一次类似的警告。当我尝试使用 const char* (或在您的情况下为 const wchar_t* )调用模板化函数并且可以使用多个重载时,就发生了这种情况。我想要隐式转换为字符串,但编译器选择了转换为 bool。
-
我想知道它是否有助于使用 GCC 进行编译,我记得我非常明确地指出了问题的来源?还是使用 MFC 完全不可行?还是 GCC 没有给出这个警告?
标签: c++ mfc boost compiler-warnings