简体   繁体   English

“函数”类型的优点是什么(不是“指向函数的指针”)

[英]What is the merit of the “function” type (not “pointer to function”)

Reading the C++ Standard, i see that there are "function" types and "pointer to function" types: 阅读C ++标准,我看到有“函数”类型和“函数指针”类型:

typedef int func(int);     // function
typedef int (*pfunc)(int); // pointer to function
typedef func* pfunc;       // same as above

I have never seen the function types used outside of examples (or maybe i didn't recognize their usage?). 我从未见过在示例之外使用的函数类型(或者我可能没有认识到它们的用法?)。 Some examples: 一些例子:

func increase, decrease;            // declares two functions
int increase(int), decrease(int);   // same as above

int increase(int x) {return x + 1;} // cannot use the typedef when defining functions
int decrease(int x) {return x - 1;} // cannot use the typedef when defining functions

struct mystruct
{
    func add, subtract, multiply;   // declares three member functions
    int member;
};

int mystruct::add(int x) {return x + member;} // cannot use the typedef
int mystruct::subtract(int x) {return x - member;}

int main()
{
    func k; // the syntax is correct but the variable k is useless!
    mystruct myobject;
    myobject.member = 4;

    cout << increase(5) << ' ' << decrease(5) << '\n'; // outputs 6 and 4
    cout << myobject.add(5) << ' ' << myobject.subtract(5) << '\n'; // 9 and 1
}

Seeing that the function types support syntax that doesn't appear in C (declaring member functions), i guess they are not just a part of C baggage that C++ has to support for backward compatibility. 看到函数类型支持C中没有出现的语法(声明成员函数),我猜它们不仅仅是C ++必须支持向后兼容性的C包的一部分。

So is there any use for function types, other than demonstrating some funky syntax? 除了展示一些时髦的语法之外,功能类型是否有用?

On the one hand, you seem to be talking about function types in general. 一方面,你似乎在谈论一般的函数类型。 On the other hand, it appears that your question is really about the usability of typedef names for function types. 另一方面,似乎您的问题实际上是关于函数类型的typedef名称的可用性。

The function types themselves is a fundamental concept built into both C and C++ languages. 函数类型本身是C和C ++语言中的基本概念。 They are used all the time. 它们一直在使用。 You just can't live without them. 没有他们你就活不下去。 Every time you declare a function you declare an entity of function type. 每次声明一个函数时,都会声明一个函数类型的实体。 Every time you use a pointer to a function, you use a pointer to function type. 每次使用指向函数的指针时,都使用指向函数类型的指针。 Etc. 等等。

As for the possibility of making typedef names for function types... I don't see much use for it. 至于为函数类型创建typedef名称的可能性......我没有看到太多用途。 It is, I think, a part of C baggage, that was extended a bit for C++ just because it was easy to do. 我认为,这是C行李的一部分,因为它很容易做到,因此对C ++进行了一些扩展。

As you correctly noted typedef names for function types can be used to declare member functions (and in C++ you can also include const specifier into typedef, as in typedef int MemberType() const; ), but, for example, you can't use this feature with function types passed as template parameters. 正确地指出函数类型的typedef名称可以用来声明成员函数(在C ++中你也可以包含const说明符到typedef中,就像在typedef int MemberType() const; ),但是,例如,你不能使用此功能将函数类型作为模板参数传递。

boost:function uses that syntax, called "preferred" syntax. boost:function使用该语法,称为“首选”语法。
Seems that kind of syntax is not widely used? 似乎那种语法没有被广泛使用? Comparing to function pointer. 与函数指针比较。

You see it in the new function objects in Boost/TR1/C++0x. 您可以在Boost / TR1 / C ++ 0x中的新函数对象中看到它。

std::function<void()> accepts any object with an operator() that returns void and takes no arguments. std::function<void()>接受任何带有operator()的对象,该对象返回void并且不带参数。

An example of why this is useful is boost , where you see this a lot. 一个为什么这个有用的例子是提升 ,你会看到很多。 For example in the signals2 library: 例如在signals2库中:

boost::signals2::signal<void (int, int)> MySignal;

where the above declares a signal that will accept any function that takes two ints and has a void return type. 上面声明了一个信号,它接受任何带有两个整数且具有void返回类型的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM