Linux C++11:如何给函数取别名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9864125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:23:44  来源:igfitidea点击:

C++11: How to alias a function?

c++linuxgccc++11

提问by Andrew Tomazos

If I have a class Foo in namespace bar:

如果我在命名空间栏中有一个 Foo 类:

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.

现在就像我在我的命名空间中定义了名为 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 aliasof 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 Bis an alias. #define B Ais an alias (at least). T& B = Ais not an alias, B can effectively implemented as an indirect pointer, wheres an "unaliased" A can use "immediate semantics".

定义:如果某个实体 B 是A的别名,那么如果 A 的任何或所有用法(当然不是声明​​或定义)在源代码中被 B 替换,那么(剥离)生成的代码保持不变。例如typedef A B是别名。 #define B A是别名(至少)。 T& B = A不是别名,B 可以有效地实现为间接指针,其中“无别名”A 可以使用“立即语义”。

采纳答案by Jeremiah Willcock

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 fis overloaded and/or a function template.

即使f重载和/或函数模板,此解决方案也适用。

回答by Kerrek SB

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

类是types,因此它们可以使用typedefand别名using(在 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).

同样,没有别名变量的机制(缺少直通指针或引用)。

回答by Martin York

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.

引用是现有对象的别名。
我刚刚创建了一个函数的引用。可以以与原始对象完全相同的方式使用引用。

回答by Tom

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

它不是标准的 C++,但大多数编译器都提供了一种方法。使用 GCC,您可以这样做:

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

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

这将创建符号f作为 的别名__f。使用 VC++,您可以通过以下方式做同样的事情:

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

回答by Jason Haslam

It is possible to introduce the function into a different scope without changing its name. So you can alias a function with a different qualified name:

可以在不更改其名称的情况下将函数引入不同的作用域。因此,您可以使用不同的限定名称为函数设置别名:

namespace bar {
  void f();
}

namespace baz {
  using bar::f;
}

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

回答by Romeno

You can use good old macros

你可以使用好的旧宏

namespace bar
{
    void f();
}

#define f bar::f

int main()
{
    f();
}

回答by Pawe? Bylica

The constexprfunction pointer can be used as a function alias.

constexpr函数指针可以用作函数别名。

namespace bar
{
    int f();
}

constexpr auto g = bar::f;

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

很可能(但语言不保证) using直接g使用bar::f。具体来说,这取决于编译器版本和优化级别。

In particular, this is the case for:

尤其是以下情况:

  • GCC 4.7.1+, without optimization,
  • Clang 3.1+, without optimization,
  • MSVC 19.14+, with optimization.
  • GCC 4.7.1+,无优化,
  • Clang 3.1+,没有优化,
  • MSVC 19.14+,优化。

See assembly generated by these compilers.

请参阅这些编译器生成的程序集