Re: Fix conversion from integer to char

Nasos Iliopoulos <[email protected]>
Newsgroups gmane.comp.lib.boost.ublas
Message-ID <[email protected]>
Nice,
The second solution that replaces the general value_type with specific 
value_types seems the correct one to me and how it should have been 
implemented in the first place.

-Nasos



On 01/05/2016 09:37 AM, Joaquim Duran Comas wrote:
> Nasos,
>
> > In line 850 I see:
> > index( ) - it.index()
>
> The error is referring the branch 
> https://github.com/jdurancomas/ublas/tree/warning_promoted_types.
>
> > The change you propose may have far reaching consequences. It would be preferable to find 
> another way around it, but I am afraid we may need to mess with the 
> type definitions of the iterators.
>
> Yes, there is another option. As the definitions of value_type in E1, 
> E2 could be different of value_type definitions in vector_binary so 
> the local variables that stores values of E1 or E2 should be defined 
> as E1::value_type or E2:value_type. Review the commit 
> https://github.com/jdurancomas/ublas/commit/bc211a890595790c421055cc47d5c3be160183f7. 
>
>
> > Can you please check, that promote_type<char, char>::promote_type is 
> an int (as well as the unsigned char, short, unsigned short), after 
> applying this change?
>
> No, the change was introduced to force that when two operands of the 
> same type was promoted then the promoted type was the same as the 
> original one. That is:
>
> promote_type<char, char>::promote_type is char
> promote_type<short, short>::promote_type is short
> .....
>
> Thanks and Best Regards,
> Joaquim Duran
>
>
> 2016-01-04 16:09 GMT+01:00 Nasos Iliopoulos <[email protected] 
> <mailto:[email protected]>>:
>
>     Joaquim,
>
>     In line 850 I see:
>     index( ) - it.index()
>
>     Based on the line numbers of the warning text, I cannot trace how
>     the difference operator is called or how the promote_type is
>     called in the vector_expression.hpp in ublas/develop for the
>     particular case.
>
>     The change you propose may have far reaching consequences. It
>     would be preferable to find another way around it, but I am afraid
>     we may need to mess with the type definitions of the iterators.
>
>     Can you please check, that promote_type<char, char>::promote_type
>     is an int (as well as the unsigned char, short, unsigned short),
>     after applying this change?
>
>
>     In any case I think there is an issue with the promote_traits that
>     are trying to deal with both cases of numerical essence (like as
>     elements of a matrix) and size_types.
>     In the numerical sense the promote_traits should be able to deal with:
>     1. Subtracting two unsigned integral types should return the
>     proper result (i.e. a signed value)
>     2. The promoted type should be able to hold the full range of the
>     unpromotted types
>
>     No. 2 cannot be satisfied in general (for example for the case of
>     unsigned ints, unless an 128-bit is used as the promotion type).
>     This is reflected by the choice that the unsigned int promotions
>     is also an unsigned int instead of just an int.
>
>     -Nasos
>
>
>
>     On 01/03/2016 08:43 PM, Joaquim Duran Comas wrote:
>>     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<constsize_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<constsize_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>
>>
>>
>>
>>
>>     _______________________________________________
>>     ublas mailing list
>>     [email protected] <mailto:[email protected]>
>>     http://lists.boost.org/mailman/listinfo.cgi/ublas
>>     Sent to:[email protected]
>>     <mailto:[email protected]>
>
>
>     _______________________________________________
>     ublas mailing list
>     [email protected] <mailto:[email protected]>
>     http://lists.boost.org/mailman/listinfo.cgi/ublas
>     Sent to: [email protected] <mailto:[email protected]>
>
>
> <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> 
>
>
>
>
> _______________________________________________
> ublas mailing list
> [email protected]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
> Sent to: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.