G ++ warning options for a pair of castes?

I just found that C ++ does not give any warnings about casting from pair<double, int> to pair<int, int> , which is a bit surprising. Here is my test_pair.cpp program:

 #include <vector> #include <utility> using namespace std; int main() { std::vector<pair<int, int> > v; pair<double, int> p = make_pair(3.8, 3); v.push_back(p); } 

I will compile it with g++ test_type.cpp -Wall -Wconversion , but still no warnings are generated. I am using g ++ v4.6.1. Has anyone figured out how to get g ++ to generate a warning for this, or simply can't be done?

+8
c ++ g ++
source share
1 answer

Pairs (and tuples) are constructive of almost everything that fits. In particular, each element can be built from everything that is implicitly converted into it. Basically, he "does what you expect." pair has constructor templates that look something like this:

 template <typename U, typename V> pair(U && u, V && v) : first(std::forward<U>(u)), second(std::forward<V>(v)) { } 

However, you should just say:

 v.emplace_back(3.8, 3); 
+4
source share

All Articles