c++11 - static_assert fails when test condition includes constants defined with const? -
i'm reading bjarne stroustrup's book, "the c++ programming language" , found example explaining static_assert. understood static_assert works things can expressed constant expressions. in other words, must not include expression that's meant evaluated @ runtime.
the following example used in book (i did changes in code. don't think should change that'd produced original example code given in book.)
#include <iostream> using namespace std; void f (double speed) { constexpr double c = 299792.468; const double local_max = 160.0/(60*60); static_assert(local_max<c,"can't go fast"); } int main() { f(3.25); cout << "reached here!"; return 0; } the above gives compile error. here's compiled using ideone: http://ideone.com/c97of5
the exact code book example:
constexpr double c = 299792.458; void f(double speed) { const double local_max = 160.0/(60∗60); static_assert(speed<c,"can't go fast"); // yes error static_assert(local_max<c,"can't go fast"); }
the compiler not know value of speed @ compile time. makes sense cannot evaluate speed < c @ compile time. hence, compile time error expected when processing line
static_assert(speed<c,"can't go fast"); the language not guarantee floating point expressions evaluated @ compile time. compilers might support that's not relied upon.
even though values of floating point variables "constants" human reader, not evaluated @ compile time. error message compiler link provided makes clear.
static_assert expression not integral constant expression
you'll have find way comparison using integral expressions. however, seems moot point. suspect, want make sure speed within limit. makes sense run time check.
Comments
Post a Comment