English 中文(简体)
• 如何消除因加固而产生的C4800警告:2008年VS的体重
原标题:How to get rid of C4800 warning produced by boost::flyweight in VS2008
  • 时间:2009-10-08 11:14:38
  •  标签:

我收到警告,2008年武革委在《联邦法典》下汇编成册。 Boost, 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:worksourcecodeoost1390oostfunctionalhashextensions.hpp(72) : warning C4800: const wchar_t * : forcing value to bool true or false (performance warning)
d:worksourcecodeoost1390oostfunctionalhashextensions.hpp(71) : while compiling class template member function size_t boost::hash<T>::operator ()(const T &) const with
[
T=ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t>>
]
d:worksourcecodeoost1390oostmulti_indexhashedindex.hpp(1159) : see reference to class template instantiation boost::hash<T> being compiled with
[
T=ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t>>
]

这一警告通过 ha工厂、MPL等进行。

为什么会发出警告,我如何纠正该守则,以便不发出警告?

Edit:
To fix, add below implementation of hash_value


template<typename CharType, typename TraitsType>
std::size_t hash_value(const ATL::CStringT<CharType, TraitsType>& s)
{
    return CStringElementTraits<typename TraitsType>::Hash(s);
}
最佳回答

空气加权的一个班级可能利用散列函数(或包装类别)从ATL中计算出一个散列值:CString。 这并不是直接推动的,因此,你需要执行:

std::size_t hash_value(const ATL::CString& s)
{
     // ...
}

仅仅看您的汇编者的产出,似乎《统计》本身是模版的,因此你将执行。

template<typename CharType, typename TraitsType>
std::size_t hash_value(const ATL::CString<CharType, TraitsType>& s)
{
     // calculate hash e.g. by calling hash_value(const std::string&)
}
问题回答

我汇编了引诱产生各种警告的节点。 此外,我指示汇编者把所有警告都作为错误处理,因此根本不必发出警告。

为了避免在编篡布人头目时出现任何警告,我使用“ #”警告,暂时将警告水平降低到尽可能低的水平,并在处理“诱人”时撤销任何剩余的警告:

// set minimal warning level
#pragma warning(push,0)
// some warnings still occur at this level
// if necessary, disable specific warnings not covered by previous pragma
#pragma warning(disable:4800)

#include 

// restore warning level
#pragma warning(pop)

这确保了我的准则得到尽可能高的错误核对,而我无法控制法典的编纂工作仍能成功进行。

我所能看到的唯一其他选择是,无视警告,或维持布埃斯特法典的拼凑版本,直到这些警告固定下来,而这两种警告都不是非常令人信服的。

汇编者发出C4800警告,必须把直言变为ool。

例如:

int k = 11;
bool f()
{ return k; }

The int expression k, is transformation from the Internal definition of the

k == 0 => *false*
k != 0 => *true*

页: 1 定义

b == false (internally == 0) => *false*
b == true  (internally == 1) => *true*

由于在C++中任何价值(其他数值为0)都可能为true,汇编者必须将k转换成一个包裹。

正如警告正确指出的那样,这种转换可能会造成业绩的打击。

NB:这一警告可能是多余的,因为汇编者通常从守则中摘取正确的含义,并优化使用。

Pseudo 汇编者将从我的样本代码中产生的编码:

char f()
{
    if( k )
       return (char) 1;
    return (char) 0;
}

我不知道如何纠正这一警告,但你可以通过在你的卷宗中添加以下法典来扭转这一警告:

#pragma warning(disable:4800)

查阅MSDN,供进一步参考。

警告的来源不在你的法典中。 你们必须制定冒犯的《诱杀法》。





相关问题
热门标签