BOOST_STATIC_WARNING

I recently had problems with C ++ implicit casting, so I'm looking for a way to warn people if someone is trying to assign int32_t to uint64_t or something else. BOOST_STATIC_ASSERT will work wonders for this, except that the code base I work with is quite large and relies on a lot of implicit casting, so immediately breaking everything with statements is unrealistic.

It sounds like BOOST_STATIC_WARNING would be ideal for me, however I cannot get it to actually issue a warning. Something like this will do nothing:

  typedef boost::is_same<int64_t, int32_t> same_type; BOOST_STATIC_WARNING(same_type::value); 

My compiler is g ++ 4.4.3 with --std = C ++ 0x -Wall -Wextra. My increase is 1.46.1.




The problem I'm trying to solve here is that we have a buffer type that has methods like uint8_t GetUInt8(size_type index) , void SetUInt32(size_type index, uint32_t value) , etc. So you see the following:

 x = buffer.GetUInt16(96); 

The problem is that there is no guarantee that while you are reading a 16-bit unsigned integer, x is actually 16-bit. Although the person who originally wrote this line did it right (hopefully), if the type x changes, this line will break silently.

My solution is to create type safe_convertable<T> , for example:

 template <typename T> struct safe_convertable { public: template <typename TSource> safe_convertable(const TSource& val) { typedef boost::is_same<T, TSource> same_type; BOOST_STATIC_WARNING(same_type::value); _val = val; } template <typename TDestination> operator TDestination () { typedef boost::is_same<T, TDestination> same_type; BOOST_STATIC_WARNING(same_type::value); return _val; } private: T _val; }; 

and change the methods to return and accept these safe links: safe_reference<uint8_t> GetUInt8(size_type index) , void SetUInt32(size_type index, safe_reference<uint32_t> value) (that there are other operators in the short version and what you can do for the links).

In any case, this works fine with BOOST_STATIC_ASSERT , except that I need warnings, not errors.




For the curious, I myself implemented a warning that works great, but I would prefer a variety of Boost to get all the other Boost functions (this only works inside the function).

 namespace detail { template <typename TIntegralContant> inline void test_warning(const TIntegralContant&) { static_cast<void>(1 / TIntegralContant::value); } } #define MY_STATIC_WARNING(value_) \ ::detail::test_warning(::boost::integral_constant<bool, value_ >()) 
+4
c ++ boost compiler-warnings
May 25 '11 at 17:14
source share
1 answer

What version of Boost are you using? This comment may be the reason that your own warning is working, but the accelerated version is not working:

 // 6. replaced implementation with one which depends solely on // mpl::print<>. The previous one was found to fail for functions // under recent versions of gcc and intel compilers - Robert Ramey 

I assume that if you upgraded to the Boost version (e.g. 1.46.1), it would be nice to go. crosses fingers

+1
May 26 '11 at 16:19
source share



All Articles