What means: template <class F, class Tuple> struct result<F(Tuple)>{}; ?

S

Stuart

Hello newsgroup,

I've stumbled over this posting (e-mail address removed) from
comp.lang.c++.moderated. The code that gets me confused is:


template <class Signature>
struct result;

template <class F, class Tuple>
struct result<F(Tuple)>
: fusion::result_of::at_c<N,Tuple>
{};

I don't know what kind of thing F(Tuple) in the partial (?)
specialization of result is. Is this a function type? I have never seen
this before, even though I've had my share of Vandevoorde and Josuttis's
Guide to C++ Templates.

Thanks in advance,
Stuart
 
S

Seungbeom Kim

I might remember completely wrong, but IIRC "type(type)" is a weird
alternative for declaring a function pointer type (which would usually
be written as "type(*)(type)". I'm not exactly sure why the (*) part
can be left out.)

That is true only in declarations of function parameters, where just as
"array of T" is adjusted to "pointer to T", "function returning T" is
adjusted to "pointer to function returning T".

In other cases, they are different:

typedef int T(); T e; // e is a function
typedef int (*T)(); T e; // e is an object (a pointer to function)
 
S

Stefan Ram

Juha Nieminen said:
Given that functions are not first-class citizens in C/C++, I really
don't understand what "e is a function" could even mean (other than it
meaning "e is a pointer to a function".)

You understand »e is a pointer to a function«. Then, by renaming,
you understand »f is a pointer to a function«, Now, let's call the
pointee »e«. Then, e is a function.

#include <iostream>
#include <ostream>

int main()
{ typedef int T(); T e;
::std::cout << sizeof( e )<< '\n'; }

main.cpp [Error] ISO C++ forbids applying 'sizeof' to an expression of function type [-fpermissive]
 
S

Stefan Ram

Juha Nieminen said:
don't understand what "e is a function" could even mean

ISO/IEC 14882 says under the heading

»4.3 Function-to-pointer conversion [conv.func]«

»An lvalue of function type T can be converted to a
prvalue of type "pointer to T."«

The correspondence between the heading and the following
sentence suggest that a »function« (in the context of
run-time values) is »an (l)value of function type«.
 
B

Bart van Ingen Schenau

Given that functions are not first-class citizens in C/C++, I really
don't understand what "e is a function" could even mean (other than it
meaning "e is a pointer to a function".)

Given
typedef int func();
then
func foo;
is an odd but perfectly legal way of declaring a function called foo that
takes no arguments and returns an int.

Bart v Ingen Schenau
 
S

Seungbeom Kim

Given
typedef int func();
then
func foo;
is an odd but perfectly legal way of declaring a function called foo that
takes no arguments and returns an int.

That's exactly what I meant.

typedef int func();
func foo; // declare first

// use foo()

int foo() { ... } // define later
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top