references to functions | | |
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!!! | | | | re: references to functions
Rahul wrote: Quote:
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". | | | | re: references to functions
On Jan 1, 2:02 pm, Barry <dhb2...@gmail.comwrote: Quote:
Rahul wrote: > Quote:
I was wondering about references to functions, so i tried this,
> Quote:
int (& p[10]) (); // doesn't work as array of references is
illegal as memory is not allocated for references,
> Quote:
next, i tried a single reference to the function,
> > Quote:
int incomplete_function(void)
{
// #error *** Nigel - Function incomplete. Fix before using ***
printf("testing testing testing done...\n");
return (0);
}
> Quote:
int sample(void)
{
printf("sample\n");
return(0);
}
> Quote:
int main()
{
int ( & p) ();
p = incomplete_function;
p();
p = sample;
p();
return(0);
}
> Quote:
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... | | | | re: references to functions
Rahul wrote: Quote:
On Jan 1, 2:02 pm, Barry <dhb2...@gmail.comwrote: Quote:
>Rahul wrote: Quote:
>>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();
} | | | | re: references to functions Quote:
>
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... | | | | re: references to functions
Rahul wrote: Quote: Quote:
>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...
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;
} | | | | re: references to functions
Barry wrote: Quote:
Rahul wrote: Quote: Quote:
>>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...
>
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. | | | | re: references to functions
On Jan 1, 4:47 pm, Barry <dhb2...@gmail.comwrote: Quote:
Barry wrote: Quote:
Rahul wrote: Quote:
>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.
> Quote: Quote:
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...
> Quote:
What compiler are you using?
VC6?
> Quote:
all the compilers that I can reach
gcc4.2.2, VC8, Comeau online, icc9.1
reject the code.
> Quote:
void foo(int) {}
void bar(int) {}
> Quote:
typedef void FT(int);
> Quote:
int main()
{
FT& rf = foo;
//rf = bar; // compile-error here
rf(10);
> >
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... | | | | re: references to functions
Rahul wrote: Quote:
On Jan 1, 4:47 pm, Barry <dhb2...@gmail.comwrote: Quote:
>Barry wrote: Quote:
>>Rahul wrote:
>>>>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...
>>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.
>
I guess, you meant FT &const rf...
There's no such thing.
FT const& rf == const FT& rf; | | | | re: references to functions
On Jan 1, 6:08 am, Rahul <sam_...@yahoo.co.inwrote: Quote:
On Jan 1, 2:02 pm, Barry <dhb2...@gmail.comwrote:
>
>
> > Quote: Quote:
I was wondering about references to functions, so i tried this,
> Quote: Quote:
int (& p[10]) (); // doesn't work as array of references is
illegal as memory is not allocated for references,
> Quote: Quote:
next, i tried a single reference to the function,
> > Quote: Quote:
int incomplete_function(void)
{
// #error *** Nigel - Function incomplete. Fix before using ***
printf("testing testing testing done...\n");
return (0);
}
> Quote: Quote:
int sample(void)
{
printf("sample\n");
return(0);
}
> Quote: Quote:
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" Quote: Quote: Quote:
p = incomplete_function;
error: assignment of read-only reference 'p' Quote: Quote: Quote:
p();
p = sample;
p();
return(0);
}
> Quote: Quote:
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?
> Quote:
<std>
8.3.5/6
... There shall be no arrays of functions, although there can
be arrays of pointers to functions.
</std>
> Quote:
"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...
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
*/ |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|