references to functions

R

Rahul

Hi Everyone,

I was wondering about references to functions, so i tried this,

int (& p[10]) (); // doesn't work as array of references is
illegal as memory is not allocated for references,

next, i tried a single reference to the function,

int ( & p ) ();

int incomplete_function(void)
{
// #error *** Nigel - Function incomplete. Fix before using ***
printf("testing testing testing done...\n");
return (0);
}

int sample(void)
{
printf("sample\n");
return(0);
}

int main()
{
int ( & p) ();
p = incomplete_function;
p();
p = sample;
p();
return(0);
}

and it works fine and both the functions are called, but i expected
the reference to be a constant pointer which can't be declared, in
fact i expected a compilation error in the declaration of the
reference p as it is not initialized over here, is reference to
function legal? what does the standard say about this?

Thanks in advance!!!
 
B

Barry

Rahul said:
Hi Everyone,

I was wondering about references to functions, so i tried this,

int (& p[10]) (); // doesn't work as array of references is
illegal as memory is not allocated for references,

next, i tried a single reference to the function,

int ( & p ) ();

int incomplete_function(void)
{
// #error *** Nigel - Function incomplete. Fix before using ***
printf("testing testing testing done...\n");
return (0);
}

int sample(void)
{
printf("sample\n");
return(0);
}

int main()
{
int ( & p) ();
p = incomplete_function;
p();
p = sample;
p();
return(0);
}

and it works fine and both the functions are called, but i expected
the reference to be a constant pointer which can't be declared, in
fact i expected a compilation error in the declaration of the
reference p as it is not initialized over here, is reference to
function legal? what does the standard say about this?

<std>
8.3.5/6
.... There shall be no arrays of functions, although there can
be arrays of pointers to functions.
</std>

"Reference to function" is legal, but no "array of function", then of
course, no "reference to array of function".
 
R

Rahul

Rahul said:
Hi Everyone,
I was wondering about references to functions, so i tried this,
int (& p[10]) (); // doesn't work as array of references is
illegal as memory is not allocated for references,
next, i tried a single reference to the function,
int ( & p ) ();
int incomplete_function(void)
{
// #error *** Nigel - Function incomplete. Fix before using ***
printf("testing testing testing done...\n");
return (0);
}
int sample(void)
{
printf("sample\n");
return(0);
}
int main()
{
int ( & p) ();
p = incomplete_function;
p();
p = sample;
p();
return(0);
}
and it works fine and both the functions are called, but i expected
the reference to be a constant pointer which can't be declared, in
fact i expected a compilation error in the declaration of the
reference p as it is not initialized over here, is reference to
function legal? what does the standard say about this?

<std>
8.3.5/6
... There shall be no arrays of functions, although there can
be arrays of pointers to functions.
</std>

"Reference to function" is legal, but no "array of function", then of
course, no "reference to array of function".

Yes i understand that there can't be any array of references, i was
asking about a reference to the function and the fact that it can be
changed during the execution...
 
B

Barry

Rahul said:
Rahul said:
Hi Everyone,
I was wondering about references to functions, so i tried this,
int (& p[10]) (); // doesn't work as array of references is
illegal as memory is not allocated for references,
next, i tried a single reference to the function,
int ( & p ) ();
int incomplete_function(void)
{
// #error *** Nigel - Function incomplete. Fix before using ***
printf("testing testing testing done...\n");
return (0);
}
int sample(void)
{
printf("sample\n");
return(0);
}
int main()
{
int ( & p) ();
p = incomplete_function;
p();
p = sample;
p();
return(0);
}
and it works fine and both the functions are called, but i expected
the reference to be a constant pointer which can't be declared, in
fact i expected a compilation error in the declaration of the
reference p as it is not initialized over here, is reference to
function legal? what does the standard say about this?
<std>
8.3.5/6
... There shall be no arrays of functions, although there can
be arrays of pointers to functions.
</std>

"Reference to function" is legal, but no "array of function", then of
course, no "reference to array of function".

Yes i understand that there can't be any array of references, i was
asking about a reference to the function and the fact that it can be
changed during the execution...

In this case you couldn't use reference to function, as you can't change
the what the reference refers to after the reference is
bound(initialized) to some object. Use pointer to function instead.

void foo() {}
void bar() {}

typedef void FT();

int main()
{
FT* pf;

pf = foo;
pf();

pf = bar;
pf();
}
 
R

Rahul

In this case you couldn't use reference to function, as you can't change
the what the reference refers to after the reference is
bound(initialized) to some object. Use pointer to function instead.


But thats not happening, i expected an error when i tried to assign
the second function to the already assigned reference...
but it doesn't, which is what my query is all about...
 
B

Barry

Rahul said:
But thats not happening, i expected an error when i tried to assign
the second function to the already assigned reference...
but it doesn't, which is what my query is all about...

What compiler are you using?
VC6?

all the compilers that I can reach
gcc4.2.2, VC8, Comeau online, icc9.1
reject the code.

void foo(int) {}
void bar(int) {}

typedef void FT(int);

int main()
{
FT& rf = foo;
//rf = bar; // compile-error here
rf(10);

return 0;
}
 
B

Barry

Barry said:
What compiler are you using?
VC6?

all the compilers that I can reach
gcc4.2.2, VC8, Comeau online, icc9.1
reject the code.

void foo(int) {}
void bar(int) {}

typedef void FT(int);

int main()
{
FT& rf = foo;
//rf = bar; // compile-error here
rf(10);

return 0;
}

Well, something to add,

FT& rf
is actually means
FT const& rf, where const is ignored if you say it.
so /rf/ is actually not a modifiable l-value.
 
R

Rahul

Well, something to add,

FT& rf
is actually means
FT const& rf, where const is ignored if you say it.
so /rf/ is actually not a modifiable l-value.

I guess, you meant FT &const rf...
 
S

Salt_Peter

Rahul said:
Hi Everyone,
I was wondering about references to functions, so i tried this,
int (& p[10]) (); // doesn't work as array of references is
illegal as memory is not allocated for references,
next, i tried a single reference to the function,
int ( & p ) ();
int incomplete_function(void)
{
// #error *** Nigel - Function incomplete. Fix before using ***
printf("testing testing testing done...\n");
return (0);
}
int sample(void)
{
printf("sample\n");
return(0);
}
int main()
{
int ( & p) ();

error: 'p' declared as reference but not initialized

standard says: 8.3.2
"The declaration of a reference shall contain an initializer except
when the declaration contains an extern specifier, is a class member
declaration within a class declaration or is the declaration of a
parameter or return type"

error: assignment of read-only reference 'p'
Yes i understand that there can't be any array of references, i was
asking about a reference to the function and the fact that it can be
changed during the execution...

Your compiler is not compiling C++, the above will fail on a compliant
compiler.
Not to mention that a free-standing function has no state and
therefore is not an object.

One solution might be boost::function...

#include <iostream>
#include "boost/function.hpp"

struct foo
{
int operator()(int n) const
{
std::cout << "foo(int)\n";
return n;
};
};

struct bar
{
int operator()(int n) const
{
std::cout << "bar(int)\n";
return n;
};
};

int main()
{
boost::function< int (int x) > f;
f = foo();
std::cout << f(5) << std::endl;
f = bar();
std::cout << f(99) << std::endl;
}

/*
foo(int)
5
bar(int)
99
*/
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top