防止主要数据类型 C++ 的隐式转换
Prevent implicit conversion for primary data types C++
BLOT: C++ 有隐式转换,我正在寻找一种方法来防止它。
让我举个例子,下面的代码片段:
#include <iostream>
int incrementByOne(int i) {
return ++i;
}
int main()
{
bool b = true;
std::cout << incrementByOne(b) << std::endl;
return 0;
}
它会输出:2
如何防止此类隐式转换并严格限制仅将 int 作为参数,即使在运行时也是如此?
我能想到的一种方法是重载函数。所以新代码看起来像:
#include <iostream>
int incrementByOne(int i) {
return ++i;
}
int incrementByOne(bool) {
std::cerr << "Please provide integer as argument" << std::endl;
exit(0);
}
int incrementByOne(char) {
std::cerr << "Please provide integer as argument" << std::endl;
exit(0);
}
int main()
{
bool b = true;
std::cout << incrementByOne(b) << std::endl;
return 0;
}
是否有任何其他(推荐的)方法来防止在运行时进行隐式转换?
您可以在 template<>
和 delete
运算符的帮助下完成此操作:
#include <iostream>
int incrementByOne(int x) {
return ++x;
}
template<class T>
T incrementByOne(T) = delete; // deleting the function
int main(void) {
std::cout << incrementByOne(-10) << std::endl;
std::cout << incrementByOne('a') << std::endl; // will not compile
return 0;
}
在此之后,传递给函数的参数必须是整数。
假设在函数参数中给出了一个浮点值,您将得到错误:
main.cpp: In function 'int main()':
main.cpp:11:36: error: use of deleted function 'T incrementByOne(T) [with T = double]'
11 | std::cout << incrementByOne(3.5) << std::endl;
| ^
How can I prevent such implicit conversions and be strict about taking
only int as an argument even at runtime?
你可以static_assert
if the passed argument is not int
eger for a template function. Check the type using std::is_same
.
#include <type_traits> // std::is_same
template<typename Type>
Type incrementByOne(Type x)
{
static_assert(std::is_same<int, Type>::value, " Some valuable messages");
return ++x;
}
BLOT: C++ 有隐式转换,我正在寻找一种方法来防止它。
让我举个例子,下面的代码片段:
#include <iostream>
int incrementByOne(int i) {
return ++i;
}
int main()
{
bool b = true;
std::cout << incrementByOne(b) << std::endl;
return 0;
}
它会输出:2
如何防止此类隐式转换并严格限制仅将 int 作为参数,即使在运行时也是如此?
我能想到的一种方法是重载函数。所以新代码看起来像:
#include <iostream>
int incrementByOne(int i) {
return ++i;
}
int incrementByOne(bool) {
std::cerr << "Please provide integer as argument" << std::endl;
exit(0);
}
int incrementByOne(char) {
std::cerr << "Please provide integer as argument" << std::endl;
exit(0);
}
int main()
{
bool b = true;
std::cout << incrementByOne(b) << std::endl;
return 0;
}
是否有任何其他(推荐的)方法来防止在运行时进行隐式转换?
您可以在 template<>
和 delete
运算符的帮助下完成此操作:
#include <iostream>
int incrementByOne(int x) {
return ++x;
}
template<class T>
T incrementByOne(T) = delete; // deleting the function
int main(void) {
std::cout << incrementByOne(-10) << std::endl;
std::cout << incrementByOne('a') << std::endl; // will not compile
return 0;
}
在此之后,传递给函数的参数必须是整数。
假设在函数参数中给出了一个浮点值,您将得到错误:
main.cpp: In function 'int main()':
main.cpp:11:36: error: use of deleted function 'T incrementByOne(T) [with T = double]'
11 | std::cout << incrementByOne(3.5) << std::endl;
| ^
How can I prevent such implicit conversions and be strict about taking only int as an argument even at runtime?
你可以static_assert
if the passed argument is not int
eger for a template function. Check the type using std::is_same
.
#include <type_traits> // std::is_same
template<typename Type>
Type incrementByOne(Type x)
{
static_assert(std::is_same<int, Type>::value, " Some valuable messages");
return ++x;
}