What’s the difference between
typedef void (&FunctionTypeR)();
vs
typedef void (FunctionType)();
Is the second also a reference to function? Is FunctionTypeR equivalent to FunctionType& when used as the type of an argument?
For
void foo(FunctionType bar)
Does the runtime makes a copy of the argument bar (a function) when foo is invoked?
The difference is that you cannot create objects of function type, but you can create of objects of function pointer type, and function reference type.
That means if you’ve a function, say
f()as:then here is what you can do, and what you cannot do:
Test code:
Now see the compilation error (and warnings):
Online demo : http://ideone.com/hpTEv
However, if you use
FunctionType(which is a function type) in a function parameter list as:then it’s equivalent to
That means, no matter what you write, you can call the function using
baras:That is, you can write this:
Demo : http://ideone.com/kwUE9
This is due to function type to function pointer type adjustment; that is, the function type is adjusted to become a pointer to function type:
The C++03 Standard says in §13.1/3,
And if you use
`FunctionTypeR(which is a function reference type) as:then it’s equivalent to:
And,
Demo : http://ideone.com/SmtQv
Interesting part…
You can use FunctionType to declare a function (but not to define it).
For example,
Demo : http://ideone.com/W4ED2