C ++ 11: How is a function alias?

If I have a class Foo in a namespace string:

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

Then I can:

 using Baz = bar::Foo; 

and now this is exactly the same as I defined a class in my namespace called 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 must also be implemented for the template functions.

Definition: If any object B is an alias for A, then if any or all of the customs (not declarations or course definitions) of A are replaced by B in the source code, different from the code with a separator, remains unchanged. For example, typedef AB is an alias. #define BA is an alias (at least). T& B = A not an alias, B can be effectively implemented as an indirect pointer, if "unaliased" A can use "immediate semantics".

+55
c ++ gcc linux c ++ 11
Mar 25 2018-12-21T00:
source share
7 answers

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 applies even if f overloaded and / or a function template.

+57
Mar 25 2018-12-12T00:
source share

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

Functions are much more like objects, so there is no mechanism for their aliases. At best, you can use function pointers or function references:

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

In the same vein, there is no mechanism for smoothing variables (with the exception of pointers or references).

+19
Mar 25 '12 at 21:19
source share

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 link is an alias of an existing object.
I just created a link to a function. A link can be used in the same way as a source object.

+13
Mar 26 '12 at 0:30
source share

This is not standard C ++, but most compilers provide a way to do this. With GCC, you can do this:

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

This creates the character f as an alias for __f . With VC ++, you do the same:

 #pragma comment(linker, "/export:f=__f") 
+7
Jul 16 '14 at 0:28
source share

You can enter a function in another area without changing its name. Thus, you can use the function with another qualified name:

 namespace bar { void f(); } namespace baz { using bar::f; } void foo() { baz::f(); } 
+6
Feb 17 '16 at 21:06
source share

constexpr function constexpr can be used as an alias of a function.

 namespace bar { int f(); } constexpr auto g = bar::f; 

In the place where the alias is used, the compiler will call the function with the alias even when compiling without any optimizations.

With GCC7, the following usage

 int main() { return g(); } 

becomes

 main: push rbp mov rbp, rsp call bar::f() # bar::f() called directly. pop rbp ret 

The assembly was created in Compiler Explorer .

+6
Apr 13 '18 at 14:39
source share

You can use good old macros

 namespace bar { void f(); } #define f bar::f int main() { f(); } 
0
Nov 02 '17 at 17:07 on
source share



All Articles