3

以下の構文の違いは何ですか?

template<typename T>
struct A { ... };

A<void (*)()> o1; // <--- ok
A<void()> o2;  // <----- ??

ライブラリ以外の2番目の構文の実際の使用法を知りたい(void()insideのオブジェクトを宣言できないことを確認したA)。私はこの質問を参照しましたが、それは役に立ちません。

4

1 に答える 1

1

void() is the type of a function taking no arguments, and returning nothing.

void(*)() is the type of a pointer to a function taking no arguments, and returning nothing.

As an example of where void() is used and is useful, look at std::function -- the syntax it uses is much nicer than if you had to pass in a function pointer signature. You can use the exact same syntax when you mean "I want to tell this template class the signature of a call".

Mainly, this is just syntactic sugar. But sugar is the spice of life.

于 2013-01-22T04:14:39.083 に答える