如何在C ++中为函数名分配别名? [英] How do I assign an alias to a function name in C++?
问题描述
很容易为类型,变量或命名空间创建新名称。但是如何为一个函数分配一个新的名字呢?例如,我想为 printf
使用名称 holler
。 #define is obvious ... any other way?
解决方案:
-
#define holler printf
-
void(* p)()= fn ; // function pointer
-
void(& r)()= fn; // function reference
-
inline void g(){f } 有不同的方法:
-
使用非模板非重载函数的C ++ 11,您可以简单地使用:
const auto& new_fn_name = old_fn_name;
-
如果此函数有多个重载,您应该使用
static_cast
:
const auto& new_fn_name = static_cast< OVERLOADED_FN_TYPE>(old_fn_name);
示例:有两个重载函数
std :: stoi
int stoi(const string& size_t *,int);
int stoi(const wstring& size_t *,int);
如果您想为第一个版本设置别名,您应该使用以下命令:
const auto& new_fn_name = static_cast< int(*)(const string& size_t *,int)>(std :: stoi);
注意:没有办法使别名重载函数
-
使用C ++ 14,您可以进一步使用
constexpr
模板变量。这允许你为模板函数别名:template< typename T&
constexpr void old_function(/ * args * /);
template< typename T>
constexpr auto alias_to_old = old_function< T> ;;
-
此外,从C ++ 11开始你有一个函数
std :: mem_fn
允许别名成员函数。请参阅以下示例:struct A {
void f(int i){
std :: cout < Argument:< i<< '\\\
';
}
};
A a;
auto greet = std :: mem_fn(& A :: f); // alias to member function
// printsArgument:5
greet(a,5); //你应该在每次使用这个别名时提供一个对象
//如果你想永久使用`std :: bind`绑定一个对象
greet_a = std :: bind(greet ,a,std :: placeholders :: _ 1);
greet_a(3); //等于greet(a,3)=> a.f(3);
It's easy to create a new name for a type, a variable or a namespace. But how do I assign a new name to a function? For example, I want to use the name
holler
forprintf
. #define is obvious... any other way?Solutions:
#define holler printf
void (*p)() = fn; //function pointer
void (&r)() = fn; //function reference
inline void g(){ f(); }
解决方案There are different approaches:
With C++11 with non-template non-overloaded functions you can simply use:
const auto& new_fn_name = old_fn_name;
If this function has multiple overloads you should use
static_cast
:const auto& new_fn_name = static_cast<OVERLOADED_FN_TYPE>(old_fn_name);
Example: there are two overloads of function
std::stoi
int stoi (const string&, size_t*, int); int stoi (const wstring&, size_t*, int);
If you want to make an alias to the first version you should use the following:
const auto& new_fn_name = static_cast<int(*)(const string&, size_t*, int)>(std::stoi);
Note: there is no way to make an alias to overloaded function such that all its overloaded versions work, so you should always specify which exact function overload you want.
With C++14 you can go even further with
constexpr
template variables. That allows you to alias templated functions:template<typename T> constexpr void old_function(/* args */); template<typename T> constexpr auto alias_to_old = old_function<T>;
Moreover, starting with C++11 you have a function called
std::mem_fn
that allows to alias member functions. See the following example:struct A { void f(int i) { std::cout << "Argument: " << i << '\n'; } }; A a; auto greet = std::mem_fn(&A::f); // alias to member function // prints "Argument: 5" greet(a, 5); // you should provide an object each time you use this alias // if you want to bind an object permanently use `std::bind` greet_a = std::bind(greet, a, std::placeholders::_1); greet_a(3); // equivalent to greet(a, 3) => a.f(3);
这篇关于如何在C ++中为函数名分配别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-