Boost logo

Boost Users :

Subject: [Boost-users] [Boost users] [ublas] Type conversion warning with ublas::bounded_vector
From: Matija Kecman (matijakecman_at_[hidden])
Date: 2012-05-10 16:27:21


Hi Boost users!

Is there a way for boost.ublas to warn of type conversions with
ublas::bounded_vector's? I'm using this library for scientific
computations and I came across this problem, I have quite a large
code so I would be useful to know if this mistake occurs elsewhere.

Here is a code example:

int main()
{
 double d = M_PI;
 unsigned u = d;

 cout << "d = " << d << endl;
 cout << "u = " << u << endl;

 bounded_vector<double,3> dVec = ublas::scalar_vector<double>(3,M_PI);
 bounded_vector<unsigned,3> uVec = dVec; // ATTENTION: type conversion!

 cout << "dVec = " << dVec << endl;
 cout << "uVec = " << uVec << endl;

 return 0;
}

When I compile this using g++ 4.6.1:
g++ -g3 -Wall -Wextra -ansi -pedantic -Wconversion -std=c++0x test.cpp -o test

I get the following warnings:
test.cpp: In function ‘int main()’:
test.cpp:11:22: warning: conversion to ‘unsigned int’ from ‘double’
may alter its value [-Wconversion]

Program output:
$ ./test
d = 3.14159
u = 3
dVec = [3](3.14159,3.14159,3.14159)
uVec = [3](3,3,3)

The compiler produced a warning for the conversion of the scalars but there was
no warning for the ublas conversion, is there a way of having the compiler write
a warning in that case? -Wconversion or any of the other options don't do this.

Best,

Matija


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