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

C++Alias

C++ Problem Overview


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 for printf. #define is obvious... any other way?

Solutions:

  1. #define holler printf
  2. void (*p)() = fn; //function pointer
  3. void (&r)() = fn; //function reference
  4. inline void g(){ f(); }

C++ Solutions


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

Solution 2 - C++

You can create a function pointer or a function reference:

void fn()
{
}

//...

void (*p)() = fn;//function pointer
void (&r)() = fn;//function reference

Solution 3 - C++

typedef int (*printf_alias)(const char*, ...);
printf_alias holler = std::printf;

Should do you fine.

Solution 4 - C++

int (*holler)(const char*, ...) = std::printf;

Solution 5 - C++

Use an inline wrapper. You get both APIs, but keep the single implementation.

Solution 6 - C++

From fluentcpp : ALIAS_TEMPLATE_FUNCTION(f, g)

#define ALIAS_TEMPLATE_FUNCTION(highLevelF, lowLevelF) \
template<typename... Args> \
inline auto highLevelF(Args&&... args) -> decltype(lowLevelF(std::forward<Args>(args)...)) \
{ \
    return lowLevelF(std::forward<Args>(args)...); \
}

Solution 7 - C++

With C++14 generic lambdas, I was able to do the following, which should also work when the target function has multiple overloads:

constexpr auto holler = [] ( auto &&...args ) {
        return printf( std::forward<decltype(args)>( args )... );
    };

Solution 8 - C++

It is worth mentioning here IMO that while the original question (and great answers) are definitely useful if you want to rename a function (there are good reasons to do so!), if all you want to do is strip out a deep namespace but keep the name, there is the using keyword for this:

namespace deep {
  namespace naming {
    namespace convention {
      void myFunction(int a, char b) {}
    }
  }
}
int main(void){
  // A pain to write it all out every time
  deep::naming::convention::myFunction(5, 'c');

  // Using keyword can be done this way
  using deep::naming::convention::myFunction;
  myFunction(5, 'c');  // Same as above
}

This also has the advantage of it being confined to a scope, though you could always use it at the top level of a file. I often use this for cout and endl so I don't need to bring in ALL of std with the classic using namespace std; at the top of a file, but also useful if you're using something like std::this_thread::sleep_for() a lot in one file or function, but not everywhere, and not any other functions from the namespace. As always, it's discouraged to use it in .h files, or you'll pollute the global namespace.

This is not the same as the "renaming" above, but is often what is really wanted.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAgnel KurianView Question on Stackoverflow
Solution 1 - C++sasha.sochkaView Answer on Stackoverflow
Solution 2 - C++Brian R. BondyView Answer on Stackoverflow
Solution 3 - C++jerView Answer on Stackoverflow
Solution 4 - C++MSaltersView Answer on Stackoverflow
Solution 5 - C++JohnView Answer on Stackoverflow
Solution 6 - C++sailfish009View Answer on Stackoverflow
Solution 7 - C++Anthony HallView Answer on Stackoverflow
Solution 8 - C++Kevin AndersonView Answer on Stackoverflow