473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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!!!
Jan 1 '08 #1
9 1902
Rahul wrote:
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".
Jan 1 '08 #2
On Jan 1, 2:02 pm, Barry <dhb2...@gmail.comwrote:
Rahul wrote:
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...
Jan 1 '08 #3
Rahul wrote:
On Jan 1, 2:02 pm, Barry <dhb2...@gmail.comwrote:
>Rahul wrote:
>>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();
}
Jan 1 '08 #4
>
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...
Jan 1 '08 #5
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;
}
Jan 1 '08 #6
Barry wrote:
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.
Jan 1 '08 #7
On Jan 1, 4:47 pm, Barry <dhb2...@gmail.comwrote:
Barry wrote:
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...
Jan 1 '08 #8
Rahul wrote:
On Jan 1, 4:47 pm, Barry <dhb2...@gmail.comwrote:
>Barry wrote:
>>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;
Jan 1 '08 #9
On Jan 1, 6:08 am, Rahul <sam_...@yahoo.co.inwrote:
On Jan 1, 2:02 pm, Barry <dhb2...@gmail.comwrote:
Rahul wrote:
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"
p = incomplete_function;
error: assignment of read-only reference 'p'
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...
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
*/
Jan 1 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: peter | last post by:
Hello all, Recently I've started to refactor my code ...(I'm using python 2.3.4) I tried to add extra functionality to old functions non-intrusively. When I used a construct, which involves...
17
by: Tom | last post by:
The motivation for references seems clear: stop people from using nasty pointers when all they really want is a reference to an object. But C++ references are so inadequate that I'm still using...
5
by: John | last post by:
Hi I have my app in access 2000 and the office version I have is 10.0 (xp). When I send my app to clients (who have office 9.0/2000) as mde, how does my application work around the reference...
3
by: SAM | last post by:
Hi everyone, I consider myself a very competent programmer when it comes to actual programming and analyzing the business that I'm modelling. I am now crossing into what I would consider Access...
14
by: el_sid | last post by:
Our developers have experienced a problem with updating Web References in Visual Studio.NET 2003. Normally, when a web service class (.asmx) is created, updating the Web Reference will...
28
by: Frederick Gotham | last post by:
When I was a beginner in C++, I struggled with the idea of references. Having learned how to use pointers first, I was hesitant to accept that references just "do their job" and that's it. Just...
9
by: igor.kulkin | last post by:
References is a relatively basic feature of C++ language. It might be a good thing to think of references as aliases to the variables. However it's good to think of references this way when you...
0
by: Irfy | last post by:
Could someone elaborate on the purpose and concrete usage scenarios of references to functions. What can references to functions do that pointers to functions cannot. Why are they in C++? A swap...
3
by: mk | last post by:
Hello everyone, I'm storing functions in a dictionary (this is basically for cooking up my own fancy schmancy callback scheme, mainly for learning purpose): .... return "f2 " + arg .......
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.