显式int类型作为参数

Explicit int type as parameter

本文关键字:参数 类型 int 显式      更新时间:2023-10-16

可以将函数写为:

void func(uint64_t val) {template <typename T>

void call_with(std :: function&lt; void(t)&gt; f,t val){f(val);}

int main(){自动打印= [](int x){std :: cout&lt;&lt;X;};call_with(打印,42);}}

如果与uint64_t以外的其他整数类型一起调用,则会生成编译时间错误,而无需修改我的#pragma警告?

IE:

uint32_t x = 0;
func(x) {…} // Error!
func(uint64_t(x)) {…} // Succes!

用功能模板重载该函数。除uint64_t以外,该功能模板将是更好的匹配。您可以定义函数模板,以便如果使用使用,它将创建错误。

void func(uint64_t val) { ... }
template <typename T>
void func(T)
{
    static_assert(false, "argument type is not uint64_t");
}

使用C 11,您可以使用以下模板:

template <typename T>
void func(T&&) = delete;

这将有效:

template< typename T >
void func( T param );
template<>
void func<uint64_t>( uint64_t param )
{
}

您将获得链接器错误(足够近)。示例:http://ideone.com/5ft4f