Boost logo

Ublas :

Subject: [ublas] Fix conversion from integer to char
From: Joaquim Duran Comas (jdurancomas_at_[hidden])
Date: 2016-01-03 20:43:19


Hello,

I'm trying to fix a warning that it is shown when the test
test_matrix_vector is compiled with mscv with all warnings enabled. The
text of the warning is the following:

*<warning text>*
c:\Users\qduran\Documents\development\modular-boost\boost/numeric/ublas/vector_expression.hpp(850)
: warning C4244: 'argument' : conversion from 'int' to 'const char',
possible loss of data

c:\Users\qduran\Documents\development\modular-boost\boost/numeric/ublas/vector_expression.hpp(813)
: while compiling class template member function 'int
boost::numeric::ublas::vector_binary<E1,E2,F>::const_iterator::dereference(boost::numeric::ublas::sparse_bidirectional_iterator_tag)
const'
        with
        [

E1=boost::numeric::ublas::matrix_column<boost::numeric::ublas::mapped_matrix<char,boost::numeric::ublas::row_major,boost::numeric::ublas::map_std<size_t,char,std::allocator<std::pair<const
size_t,char>>>>>
,
 E2=boost::numeric::ublas::vector<value_type,boost::numeric::ublas::unbounded_array<value_type,std::allocator<char>>>
, F=boost::numeric::ublas::scalar_minus<char,value_type>
        ]

c:\Users\qduran\Documents\development\modular-boost\boost/numeric/ublas/vector_expression.hpp(884)
: see reference to function template instantiation 'int
boost::numeric::ublas::vector_binary<E1,E2,F>::const_iterator::dereference(boost::numeric::ublas::sparse_bidirectional_iterator_tag)
const' being compiled
        with
        [

E1=boost::numeric::ublas::matrix_column<boost::numeric::ublas::mapped_matrix<char,boost::numeric::ublas::row_major,boost::numeric::ublas::map_std<size_t,char,std::allocator<std::pair<const
size_t,char>>>>>
,
 E2=boost::numeric::ublas::vector<value_type,boost::numeric::ublas::unbounded_array<value_type,std::allocator<char>>>
, F=boost::numeric::ublas::scalar_minus<char,value_type>
        ]

c:\Users\qduran\Documents\development\modular-boost\boost/numeric/ublas/vector_expression.hpp(1538)
: see reference to class template instantiation
'boost::numeric::ublas::vector_binary<E1,E2,F>::const_iterator' being
compiled
        with
        [

E1=boost::numeric::ublas::matrix_column<boost::numeric::ublas::mapped_matrix<char,boost::numeric::ublas::row_major,boost::numeric::ublas::map_std<size_t,char,std::allocator<std::pair<const
size_t,char>>>>>
,
 E2=boost::numeric::ublas::vector<value_type,boost::numeric::ublas::unbounded_array<value_type,std::allocator<char>>>
, F=boost::numeric::ublas::scalar_minus<char,value_type>
        ]

c:\Users\qduran\Documents\development\modular-boost\boost/numeric/ublas/vector_expression.hpp(1647)
: see reference to class template instantiation
'boost::numeric::ublas::vector_scalar_unary<E,F>' being compiled
        with
        [

E=boost::numeric::ublas::vector_binary<boost::numeric::ublas::matrix_column<boost::numeric::ublas::mapped_matrix<char,boost::numeric::ublas::row_major,boost::numeric::ublas::map_std<size_t,char,std::allocator<std::pair<const
size_t,char>>>>>,boost::numeric::ublas::vector<value_type,boost::numeric::ublas::unbounded_array<value_type,std::allocator<char>>>,boost::numeric::ublas::scalar_minus<char,value_type>>
,
 F=boost::numeric::ublas::vector_norm_inf<boost::numeric::ublas::vector_binary<boost::numeric::ublas::matrix_column<boost::numeric::ublas::mapped_matrix<char,boost::numeric::ublas::row_major,boost::numeric::ublas::map_std<size_t,char,std::allocator<std::pair<const
size_t,char>>>>>,boost::numeric::ublas::vector<value_type,boost::numeric::ublas::unbounded_array<value_type,std::allocator<char>>>,boost::numeric::ublas::scalar_minus<char,value_type>>>
        ]

c:\Users\qduran\Documents\development\modular-boost\boost/numeric/ublas/detail/vector_assign.hpp(33)
: see reference to function template instantiation 'int
boost::numeric::ublas::norm_inf<boost::numeric::ublas::vector_binary<E1,E2,F>>(const
boost::numeric::ublas::vector_expression<boost::numeric::ublas::vector_binary<E1,E2,F>>
&)' being compiled
        with
        [

E1=boost::numeric::ublas::matrix_column<boost::numeric::ublas::mapped_matrix<char,boost::numeric::ublas::row_major,boost::numeric::ublas::map_std<size_t,char,std::allocator<std::pair<const
size_t,char>>>>>
,
 E2=boost::numeric::ublas::vector<value_type,boost::numeric::ublas::unbounded_array<value_type,std::allocator<char>>>
, F=boost::numeric::ublas::scalar_minus<char,value_type>
        ]

c:\Users\qduran\Documents\development\modular-boost\boost/numeric/ublas/detail/vector_assign.hpp(42)
: see reference to function template instantiation 'bool
boost::numeric::ublas::detail::equals<boost::numeric::ublas::matrix_column<Matrix>,C,int>(const
boost::numeric::ublas::vector_expression<boost::numeric::ublas::matrix_column<Matrix>>
&,const boost::numeric::ublas::vector_expression<C> &,S,S)' being compiled
        with
        [

Matrix=boost::numeric::ublas::mapped_matrix<char,boost::numeric::ublas::row_major,boost::numeric::ublas::map_std<size_t,char,std::allocator<std::pair<const
size_t,char>>>>
,
 C=boost::numeric::ublas::vector<value_type,boost::numeric::ublas::unbounded_array<value_type,std::allocator<char>>>
, S=int
        ]

c:\Users\qduran\Documents\development\modular-boost\boost/numeric/ublas/detail/vector_assign.hpp(436)
: see reference to function template instantiation 'bool
boost::numeric::ublas::detail::expression_type_check<boost::numeric::ublas::matrix_column<Matrix>,C>(const
boost::numeric::ublas::vector_expression<boost::numeric::ublas::matrix_column<Matrix>>
&,const boost::numeric::ublas::vector_expression<C> &)' being compiled
        with
        [

Matrix=boost::numeric::ublas::mapped_matrix<char,boost::numeric::ublas::row_major,boost::numeric::ublas::map_std<size_t,char,std::allocator<std::pair<const
size_t,char>>>>
,
 C=boost::numeric::ublas::vector<value_type,boost::numeric::ublas::unbounded_array<value_type,std::allocator<char>>>
        ]
*</warning text>*

The warning is detected in the function
'boost::numeric::ublas::vector_binary<E1,E2,F>::const_iterator::dereference',
when the binary function is called (line of code: return
static_cast<value_type>(functor_type::apply (t1, t2));). The binary
function requires two parameters of type char, however two parameters of
type integer are provided to he function. The parameters provided to the
binary function (named t1 and t2, of type integer) are declared with the
type vector_binary::const_iterator::value_type.
This type is defined from vector_binary::value_type, and this type is
defined from F::result_type, where F is the template parameter of the class
vector_binary that specifies the functor implementing the binary function.
The functor scalar_minus defines the result_type as 'typedef typename
promote_traits<T1, T2>::promote_type result_type;', where T1 and T2 are
template parameters that specifies the types of the parameters of the
binary function.

The problem is located at promote_type struct, in which the promoted type
of two chars, two unsigned chars, two shorts or two unsigned shorts is an
integer and not the same type of the template parameters. Following, there
are the debug traces of promote_traits (located at file
vector_expression.hpp):

promote_traits<char, char>::promote_type: int
promote_traits<unsigned char, unsigned char>::promote_type: int
promote_traits<short, short>::promote_type: int
promote_traits<unsigned short, unsigned short>::promote_type: int
promote_traits<int, int>::promote_type: int
promote_traits<unsigned int, unsigned int>::promote_type: unsigned int
promote_traits<float, float>::promote_type: float
promote_traits<double, double>::promote_type: double

I could fix the warning by adding the following template specialization of
promote_traits in the file traits.hpp:

template<class X>
struct promote_traits<X, X> {
    typedef typename X promote_type;
};

I would like to know if the template specialization is the right way to fix
the problem of the promote_type struct should be revised.

To test it, I've created a reduced version of file test_matrix_vector.cpp.

All these changes could be downloaded from the branch
https://github.com/jdurancomas/ublas/tree/warning_promoted_types.

Thanks and Best Regards,
Joaquim Duran

<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
Este
correo electrónico se ha enviado desde un equipo libre de virus y protegido
por Avast.
www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
<#DDB4FAA8-2DD7-40BB-A1B8-4E2AA1F9FDF2>