显式int类型作为参数

Vik*_*ehr 3 c++ type-conversion

是否可以编写一个函数:

void func(uint64_t val) {...}
Run Code Online (Sandbox Code Playgroud)

如果使用任何其他整数类型调用uint64_t,而不修改我的#pragma警告,则会生成编译时错误?

即:

uint32_t x = 0;
func(x) {...} // Error!
func(uint64_t(x)) {...} // Succes!
Run Code Online (Sandbox Code Playgroud)

nos*_*sid 5

使用功能模板重载该功能.函数模板将更好地匹配所有参数类型,除了uint64_t.您可以定义函数模板,以便在使用时创建错误.

void func(uint64_t val) { ... }

template <typename T>
void func(T)
{
    static_assert(false, "argument type is not uint64_t");
}
Run Code Online (Sandbox Code Playgroud)

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

template <typename T>
void func(T&&) = delete;
Run Code Online (Sandbox Code Playgroud)