C++ 中的“自由函数”一词是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4861914/
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-28 16:48:01  来源:igfitidea点击:

What is the meaning of the term "free function" in C++?

c++

提问by Jame

While reading the documentation for boost::test, I came across the term "free function". What I understand is that a free function is any function that doesn't return anything (Its return type is void). But after reading further it seems that free functions also don't take any arguments. But I am not sure. These all are my assumptions. So could anybody define free function?

在阅读 boost::test 的文档时,我遇到了“自由函数”这个词。我的理解是,自由函数是任何不返回任何内容的函数(其返回类型为 void)。但是在进一步阅读之后,似乎自由函数也不接受任何参数。但我不确定。这些都是我的假设。那么有人可以定义自由函数吗?

回答by Georg Fritzsche

The term free functionin C++ simply refers to non-member functions. Every function that is not a member function is a free function.

C++ 中的术语自由函数只是指非成员函数。每个不是成员函数的函数都是自由函数。

struct X {
    void f() {}               // not a free function
};
void g() {}                   // free function
int h(int, int) { return 1; } // also a free function