In a previous question I was trying to figure out at compile time if a particular conversion (of the type to double and back) for a particular fundamental type was safe or not.
Eventually, I figured out the following code:
#include <limits>
#include <vector>
template<class one>
class test {
typedef typename one::value_type v; //My code actually takes std:: containers, not the type directly, so get the type.
typedef std::numeric_limits<v> limits; //Easier to read
static_assert(limits::max()==v(double(limits::max())),"MEANINGFUL ERROR MESSAGE"); //See if the conversion works correctly
};
When it is declared with a "safe" type, like:
test<std::vector<int> > a;
It compiles just fine. But when it is compiled with a type that will not convert correctly, such as:
test<std::vector<unsigned long long> > b;
Rather than failing the assertion as I expected, it fails to compile with the error message (using g++ 4.7.1 and 5.3.0 with --std=c++11):
test2.cpp: In instantiation of ‘class test<std::vector<long long unsigned int> >’:
test2.cpp:11:40: required from here
test2.cpp:8:5: error: non-constant condition for static assertion
static_assert(limits::max()==v(double(limits::max())),"MEANINGFUL ERROR MESSAGE");
I suppose that it is still getting the job done (not compiling when there's a problem, but I still can't figure out why limits::max()==v(double(limits::max())) doesn't count as a constant condition, and I don't really like the fact that it makes the error message less comprehensible. What am I doing wrong?
Thanks!
Aucun commentaire:
Enregistrer un commentaire