How to Assign an Alias to a Function Name in C++

How do I assign an alias to a function name in C++?

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);

How to rename/alias a function in C?

You can't alias library functions, but you can alias your own using preprocessor directives.

For example:

mylib.h:

#include <stdio.h>

void my_puts(char *c);

#define puts(arg) my_puts(arg)

mylib.c:

void my_puts(char *c)
{
(puts)(c);
}

Now, anytime someone calls puts, it substitutes a call to my_puts instead. Also, when you want to call the "real" function in your wrapper, you can put the function name in quotes. Because the macro that does the substitution is a function-like macro, the parenthesis prevent the substitution from happening.

C++11: How to alias a function?

You can define a function alias (with some work) using perfect forwarding:

template <typename... Args>
auto g(Args&&... args) -> decltype(f(std::forward<Args>(args)...)) {
return f(std::forward<Args>(args)...);
}

This solution does apply even if f is overloaded and/or a function template.

How to alias C library functions?

This is just a variation of the method as given by @Michał Górny (I run out of comment space there)...

You could create an include file of the following form:

/* Automatically created file - do not edit or ugly dinosaur will eat you */
#ifndef PREFIX
# define RENAME(f)
#else
# define RENAME(f) PREFIX ## f
#endif

/* list all the function and variables you want to rename here in one place */
#define func_foo RENAME(func_foo)
#define func_bar RENAME(func_bar)
/* ... many more ... */

#undef RENAME

At least gcc allows you to specify the inclusion of a header file from command line with option -include rename.h (assuming this file is called rename.h). Because you use gcc lookalike options (-O3 and Os), I am assuming you use gcc in the rest of this answer. Otherwise, if your C compiler is reasonable, you should be able to do it in some similar way.

You can create easily two or even three versions of your library that can be linked in at the same time if you want, by providing different options for your C compiler (here through CFLAGS setting):

CFLAGS += -include rename.h -DPREFIX=fast_ -D_BUILD_FAST -O3 -DBENCHMARKING
CFLAGS += -include rename.h -DPREFIX=small_ -D_BUILD_SMALL -Os -DBENCHMARKING
CFLAGS += -D_BUILD_FAST -O2

If your library header files look very regular and if you declare the library private functions static, then it is easy to extract the functions from those header files by some dummy script using very simple regular expressions to automatically generate the rename.h file for you. This is a natural build target if you are using make or something similar. All the global variables also need to be renamed using the same method to allow simultaneous use.

There are three main points with this solution:

  1. The ugly renaming business can be hidden in one file, you do not need to edit the actual source files - especially you do not need to clutter the source files but can keep them clean and easy to read.
  2. The renaming can be easily automated, if you follow some simple principles (coding conventions followed for the header files and the header files will declare all the global variables and functions).
  3. There is no reason to make benchmarking more cumbersome by needing to run your test program multiple times (this is relevant if you are as lazy as I am and dislike repetive tasks as violently as I do - I know many people do not care, it is somewhat a matter of preference).

creating an alias for a function name in C#

You can use an extension method

public static class Extensions 
{
public static void B(this Test t)
{
t.A();
}
}

But it is not an alias. It is a wrapper.


EDIT

ps: I agree with the commenters on your question that we'd be able to give better answers if we knew what you really wanted to do, if we understood the problem you're trying to solve.

I don't really see the point of producing the above extension method.

alias of function pointer

Here in this statement

typedef int (*callback) (int,int);

/* now callback can be used as a type */

You are typedefing a function pointer. Here callback is a function pointer which can point to any function, that functions has to take two argument of int type and returns int

Next time when you do like

callback fptr;

that means fptr is function pointer and that can point to any function who satisfies callback declaration properties.

Next when you initializes fptr like this

struct gate var;

var.fptr = sum; /* here sum is the function which adds 2 int and returns int */

What's the safest way to define short function name aliases in C++?

Instead of macro, you could write inline function that forwards the call to the actual function:

inline double ldn(double x)
{
return Utility::longDescriptiveName(x);
}

That is certainly safer than macro.



Related Topics



Leave a reply



Submit