c++ - Fix of warning "comparison is always false due to limited range of data type [-Wtype-limits]" -


i have problems fixing gcc warning in 1 of template classes (using c++11). have following member function in class:

void throwinvalidargumentexceptionifvalueislessthanminimumallowedvalue() const {   if (static_cast<std::intmax_t>(value_) < kminimumvalue) {     throw std::invalid_argument{"the specified number " + std::to_string(static_cast<std::intmax_t>(value_)) + " less minimum number " + std::to_string(static_cast<std::intmax_t>(kminimumvalue)) + "."};   } } 

the class has following template signature (using crtp idiom):

template <class tclass,           typename tvalue,           std::intmax_t const kminimumvalue,           std::intmax_t const kmaximumvalue> 

the following warning raised line if condition (which makes sense) when using template signature <decimalpercentage, std::uint8_t, 0, 100>:

warning: comparison false due limited range of data type [-wtype-limits] 

i think problematic if condition may not compiled if (std::numeric_limits<tvalue>::is_unsigned && kminimumvalue == 0), have no clue how modify code in order avoid warning.

the following version of gcc used:

gcc (suse linux) 4.7.1 20120723 [gcc-4_7-branch revision 189773] 

here complete example:

#include <cstdint> #include <stdexcept>  template <class tclass,           typename tvalue,           std::intmax_t const kminimumvalue,           std::intmax_t const kmaximumvalue> struct {   a(tvalue const kvalue) : value_{kvalue} {     // noop   }    void some_func() const {     if (static_cast<std::intmax_t>(value_) < kminimumvalue) {       throw std::runtime_error{"foo"};     }   }    tvalue value_; };  struct b : public a<b, std::uint8_t, 0, 100> {   b(std::uint8_t const kvalue) : a{kvalue} {     // noop   } };  int main() {   b temp{0};   temp.some_func(); } 

and command compile example: g++ main.cc -std=c++11 -wall -wextra -pedantic -wtype-limits

edit:: came across std::enable_if, have no idea how use it. want conditionally compile method, think need std::enable_if<std::is_signed<tvalue>::value>::type. can guide me right direction?

unsigned int (uint8_t) can never negative check fail.

static_cast<std::intmax_t>(value_) < kminimumvalue 

you're casting intmax_t compiler knows passed constant in main , you're never going use negative integer value.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -