在我的一个模板类(使用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>
当使用模板签名if
时,会对带有<DecimalPercentage, std::uint8_t, 0, 100>
条件的行发出以下警告(这是有意义的)
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
编辑::我偶然遇到了std::enable_if,但我不知道如何使用它。我想有条件地编译这个方法,所以我想我需要一些类似std::enable_if<std::is_signed<TValue>::value>::type
的东西。有人能把我引向正确的方向吗?
发布于 2015-07-14 17:59:02
无符号int (uint8_t)永远不会是负的,所以您的检查总是失败的。
static_cast<std::intmax_t>(value_) < kMinimumValue
您正在转换为一个intmax_t,但是编译器知道您在主目录中传递了一个常量,并且永远不会实际使用负整数值。
https://stackoverflow.com/questions/31411271
复制相似问题