基于“符号性”的部分模板专门化整数类型?
给定:
template<typename T>
inline bool f( T n ) {
return n >= 0 && n <= 100;
}
当与 unsigned
类型一起使用时会生成警告:
unsigned n;
f( n ); // warning: comparison n >= 0 is always true
是否有任何聪明的方法可以在 T
时不进行比较 n >= 0
是无符号
类型?我尝试添加部分模板专业化:
template<typename T>
inline bool f( unsigned T n ) {
return n <= 100;
}
但 gcc 4.2.1 不喜欢这样。 (无论如何,我认为这种部分模板专业化是不合法的。)
Given:
template<typename T>
inline bool f( T n ) {
return n >= 0 && n <= 100;
}
When used with an unsigned
type generates a warning:
unsigned n;
f( n ); // warning: comparison n >= 0 is always true
Is there any clever way not to do the comparison n >= 0
when T
is an unsigned
type? I tried adding a partial template specialization:
template<typename T>
inline bool f( unsigned T n ) {
return n <= 100;
}
but gcc 4.2.1 doesn't like that. (I didn't think that kind of partial template specialization would be legal anyway.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将
enable_if
与is_unsigned
类型特征一起使用:您可以在
std 中找到
或enable_if
和is_unsigned
std::tr1
命名空间(如果您的编译器分别支持 C++0x 或 TR1)。否则,Boost 有一个类型特征库的实现, Boost.TypeTraits。enable_if
的 boost 实现有点不同;boost::enable_if_c
与 TR1 和 C++0xenable_if
类似。You can use
enable_if
with theis_unsigned
type trait:You can find
enable_if
andis_unsigned
in thestd
orstd::tr1
namespaces if your compiler supports C++0x or TR1, respectively. Otherwise, Boost has an implementation of the type traits library, Boost.TypeTraits. The boost implementation ofenable_if
is a little different;boost::enable_if_c
is similar to the TR1 and C++0xenable_if
.您可以利用无符号整数的环绕行为。
重要的是不要说
>= 0
,以避免再次出现警告。以下内容似乎也欺骗了 GCCYou can take advantage of the wrap-around behavior of unsigned integers.
It's important not to say
>= 0
, to avoid a warning again. The following appears to trick GCC too从 c++17 引入
if constexpr
您甚至不需要为此提供专业知识。与普通的 if 语句不同,如果表达式不为 true,if constexpr
中的代码将被丢弃(不编译)。这意味着你可以像这样重写你的函数Starting in c++17 with the introduction of
if constexpr
you don't even need to provide specializations for this. Unlike a normal if statement the code in theif constexpr
will be discarded (not compiled) if the expression is not true. That means you can rewrite your function like优化器应该删除比较代码,因为它检测到了条件。
对于 Clang,添加
-Wno-tautological-compare
来消除警告。对于 GCC/G++,添加-Wno-type-limits
来消除警告。如果您使用的编译器支持
pragma Diagnostic {push|pop}
,您可以:另请参阅比较始终由于范围有限,错误...
The optimizer should drop the code for the compare since it detected the condition.
For Clang, add
-Wno-tautological-compare
to squash the warning. For GCC/G++, add-Wno-type-limits
to squash the warning.If you are using a compiler that support
pragma diagnostic {push|pop}
you can:Also see Comparison is always false due to limited range…
您可以为
unsigned
类型实现特殊的模板函数实现,例如:UPDATE Unsigned flag
您可以为您想要使用的所有无符号类型实现不同的实现,或添加
bool
标志如下:You can implement a special template function implementation for
unsigned
type like:UPDATE Unsigned flag
You can implement different implementations for all unsigned types you'd like to use or add a
bool
flag like: