显式int类型作为参数 [英] Explicit int type as parameter
本文介绍了显式int类型作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以编写一个函数:
void func(uint64_t val) {...}
其中如果使用任何其他整数类型调用编译时错误 uint64_t
,而不修改我的 #pragma
警告?
where a compile time error is generated if it's called with any other integer type than uint64_t
, without modifying my #pragma
warnings?
ie:
uint32_t x = 0;
func(x) {...} // Error!
func(uint64_t(x)) {...} // Succes!
推荐答案
使用函数模板重载函数。函数模板将更好地匹配除了 uint64_t
之外的所有参数类型。您可以定义函数模板,以便在使用时创建错误。
Overload the function with a function template. The function template will be a better match for all argument types except uint64_t
. You can define the function template, so that it will create an error if used.
void func(uint64_t val) { ... }
template <typename T>
void func(T)
{
static_assert(false, "argument type is not uint64_t");
}
使用C ++ 11可以使用以下模板:
With C++11 you can use the following template:
template <typename T>
void func(T&&) = delete;
这篇关于显式int类型作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文