我在一个模板类(使用c++ 11)中修复GCC警告时遇到了问题。在类中有以下成员函数:
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_)) + " is less than the minimum number " + std::to_string(static_cast<std::intmax_t>(kMinimumValue)) + "."};
}
}
类具有以下模板签名(使用CRTP习语):
template <class TClass,
typename TValue,
std::intmax_t const kMinimumValue,
std::intmax_t const kMaximumValue>
当使用模板签名<DecimalPercentage, std::uint8_t, 0, 100>
时,对于具有if
条件的行(这是有意义的)会引发以下警告:
warning: comparison is always false due to limited range of data type [-Wtype-limits]
我认为有问题的if
条件可能不会编译if (std::numeric_limits<TValue>::is_unsigned && kMinimumValue == 0)
,但我不知道如何修改代码以避免警告。
使用以下版本的GCC:
gcc (SUSE Linux) 4.7.1 20120723 [gcc-4_7-branch revision 189773]
下面是完整的示例:
#include <cstdint>
#include <stdexcept>
template <class TClass,
typename TValue,
std::intmax_t const kMinimumValue,
std::intmax_t const kMaximumValue>
struct A {
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();
}
和编译示例的命令:g++ main.cc -std=c++11 -Wall -Wextra -pedantic -Wtype-limits
Edit::我遇到了std::enable_if,但我不知道如何使用它。我想有条件地编译方法,所以我认为我需要像std::enable_if<std::is_signed<TValue>::value>::type
这样的东西。有人能给我指路吗?
unsigned int (uint8_t)永远不会是负的,所以你的检查总是会失败。
static_cast<std::intmax_t>(value_) < kMinimumValue
你正在转换为intmax_t,但编译器知道你在main中传递了一个常量,并且你永远不会实际使用负整数值