C++11: How to alias a function?

C++LinuxGccC++11

C++ Problem Overview


If I have a class Foo in namespace bar:

namespace bar
{
    class Foo { ... }
};

I can then:

using Baz = bar::Foo;

and now it is just like I defined the class in my namespace with the name Baz.

Is it possible to do the same for functions?

namespace bar
{
    void f();
}

And then:

using g = bar::f; // error: ‘f’ in namespace ‘bar’ does not name a type

What is the cleanest way to do this?

The solution should also hold for template functions.

Definition: If some entity B is an alias of A, than if any or all usages (not declarations or definitions of course) of A are replaced by B in the source code than the (stripped) generated code remains the same. For example typedef A B is an alias. #define B A is an alias (at least). T& B = A is not an alias, B can effectively implemented as an indirect pointer, wheres an "unaliased" A can use "immediate semantics".

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

The constexpr function pointer can be used as a function alias.

namespace bar
{
    int f();
}

constexpr auto g = bar::f;

It is highly likely (but not guaranteed by the language) that using g uses bar::f directly. Specifically, this depends on compiler version and optimization level.

In particular, this is the case for:

  • GCC 4.7.1+, without optimization,
  • Clang 3.1+, without optimization,
  • MSVC 19.14+, with optimization.

See assembly generated by these compilers.

Solution 3 - C++

Classes are types, so they can be aliased with typedef and using (in C++11).

Functions are much more like objects, so there's no mechanism to alias them. At best you could use function pointers or function references:

void (*g)() = &bar::f;
void (&h)() = bar::f;

g();
h();

In the same vein, there's no mechanism for aliasing variables (short of through pointers or references).

Solution 4 - C++

It's possible to introduce the function into a different scope without changing its name. That means that you can alias a function with a different qualified name:

namespace bar {
  void f();
}

namespace baz {
  using bar::f;
}

void foo() {
  baz::f();
}

Solution 5 - C++

Absolutely:

#include <iostream>

namespace Bar
{
   void test()
   {
      std::cout << "Test\n";
   }


   template<typename T>
   void test2(T const& a)
   {
      std::cout << "Test: " << a << std::endl;
   }
}

void (&alias)()        = Bar::test;
void (&a2)(int const&) = Bar::test2<int>;

int main()
{
    Bar::test();
    alias();
    a2(3);
}

Try:

> g++ a.cpp
> ./a.out
Test
Test
Test: 3
>

A reference is an alias to an existing object.
I just created a reference to a function. The reference can be used in exactly the same way as the original object.

Solution 6 - C++

It's not standard C++, but most compilers provide a way of doing this. With GCC you can do this:

void f () __attribute__ ((weak, alias ("__f")));

This creates the symbol f as an alias for __f. With VC++ you do the same thing this way:

#pragma comment(linker, "/export:f=__f")

Solution 7 - C++

You can use good old macros

namespace bar
{
    void f();
}

#define f bar::f

int main()
{
    f();
}

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
QuestionAndrew TomazosView Question on Stackoverflow
Solution 1 - C++Jeremiah WillcockView Answer on Stackoverflow
Solution 2 - C++Paweł BylicaView Answer on Stackoverflow
Solution 3 - C++Kerrek SBView Answer on Stackoverflow
Solution 4 - C++Jason HaslamView Answer on Stackoverflow
Solution 5 - C++Martin YorkView Answer on Stackoverflow
Solution 6 - C++TomView Answer on Stackoverflow
Solution 7 - C++UnicornView Answer on Stackoverflow