Question

Consider the following code:

#include <iostream>
#include <vector>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>

int main()
{
    namespace ublas = boost::numeric::ublas;

    double   d = M_PI;
    unsigned u = d;

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

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

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

    return 0;
}

When I compile this using g++ (version 4.6.1) with the following options:

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]

When I run the program I get:

$ ./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? It looks like -Wconversion or any of the other options don't do this.

Was it helpful?

Solution

Why should any compiler warn on this?

The authors of ublas::bounded_vector<T,N> must have defined a conversion from ublas::bounded_vector<U,N>, otherwise it would not be possible at all. And if there is such function/constructor, there is no reason for the compiler to warn you when you use it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top