Boost logo

Boost Users :

From: Remi Ricard (remi.ricard_at_[hidden])
Date: 2007-12-21 09:40:56


Hi Robert,

> look at static_warning.hpp

My documentation was saying: todo !!!!

But I checked the web and found discussion and this lead me to create
something useful for me.

So I'm sending the info for people interested by the discussion.

// This template will NOT produce warning
template<typename U, typename V>
struct simWarningTypeTruncation
{
  typedef double type;
};

// =====================================================================
// char with other
// =====================================================================
template<>
struct simWarningTypeTruncation<char, float>
{
  typedef char type;
};

template<>
struct simWarningTypeTruncation<char, double>
{
  typedef char type;
};

template<>
struct simWarningTypeTruncation<char, long double>
{
  typedef char type;
};

// =====================================================================
// short with other
// =====================================================================
template<>
struct simWarningTypeTruncation<short, float>
{
  typedef short type;
};

template<>
struct simWarningTypeTruncation<short, double>
{
  typedef short type;
};

template<>
struct simWarningTypeTruncation<short, long double>
{
  typedef short type;
};

// =====================================================================
// int with other
// =====================================================================
template<>
struct simWarningTypeTruncation<int, float>
{
  typedef int type;
};

template<>
struct simWarningTypeTruncation<int, double>
{
  typedef int type;
};

template<>
struct simWarningTypeTruncation<int, long double>
{
  typedef int type;
};

// =====================================================================
// long with other
// =====================================================================
template<>
struct simWarningTypeTruncation<long, float>
{
  typedef long type;
};

template<>
struct simWarningTypeTruncation<long, double>
{
  typedef long type;
};

template<>
struct simWarningTypeTruncation<long, long double>
{
  typedef long type;
};

#define SIM_STATIC_WARNING_TYPE_TRUNCATION(B, C) \
{ \
   {simWarningTypeTruncation<B, C>::type x; x = 2.3;} \
}

template <typename U, typename V>
double
fct(U u, V v)
{
   SIM_STATIC_WARNING_TYPE_TRUNCATION(U, V);

   return u + v;
}

void
main()
{
   std::cout<< fct(1, 2) << std::endl;
   std::cout<<fct(2, 3.0) << std::endl;
   std::cout<<fct(3.0, 4) << std::endl;

}

>
> Robert Ramey
>
> Remi Ricard wrote:
>> Hi,
>>
>> I want to warn user about a possible miss use of a function.
>>
>> Now I'm only able to stop the compilation with the following code
>>
>> template <typename ValueType>
>> class simVec3 {
>> .........
>> /// Unary multiply by scalar.
>> /// @param rsh The scalar to multiply the vector with
>> template<class T> simVec3<ValueType>& operator *= (const T& rhs)
>> {
>> // The function should not be use like this
>> // simVec<int> vec(1, 1, 1);
>> // a *= 0.3; // since what a will contain is [0, 0, 0]
>> BOOST_MPL_ASSERT_NOT(( boost::mpl::and_< \
>> boost::is_floating_point<T>, \
>> boost::is_integral<ValueType> > ));
>>
>> ValueType s=static_cast<ValueType>(rhs);
>> vec[0]*=s; vec[1]*=s; vec[2]*=s; return *this;
>> }
>>
>> What I would want is something like
>> #if A_meta_function_returning_1_if_int_double
>> #pragma message("simVec::Possible type truncation")
>> #endif
>>
>> instead of the BOOST_MPL_ASSERT_NOT
>>
>>
>> I also tried something like:
>>
>> template <bool x> struct AA;
>> template <> struct AA<true> { enum {value = 1 }; };
>> template <> struct AA<false> { enum {value = 0}; };
>>
>> template <typename U, typename V>
>> double
>> myfct(U u, V v)
>> {
>> #if ( AA< boost::mpl::and_< boost::is_integral<U>, \
>> boost::is_floating_point<V> > > ::value )
>> #pragma message("****** Hellop *********")
>> #endif
>>
>> return u + v;
>> }
>>
>> But the compilation is complaining about an unmatched parenthesis
>>
>>
>> Remi
>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net