Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

int (*&)()

1 view
Skip to first unread message

Asif Zaidi

unread,
Feb 7, 2010, 10:45:23 PM2/7/10
to
What does the syntax in subject line mean - how should I read it.


Thanks

Asif

Syron

unread,
Feb 8, 2010, 1:28:30 AM2/8/10
to

if this is a typedef (like 'int(*&func)()') it is a reference to a
pointer to a function that returns an int.

Kind regards, Syron

Asif Zaidi

unread,
Feb 8, 2010, 6:37:03 AM2/8/10
to

Thanks

Asif

Asif Zaidi

unread,
Feb 8, 2010, 7:58:35 AM2/8/10
to
On Feb 8, 12:28 am, Syron <mr.sy...@googlemail.com> wrote:


I am trying to implement the following and getting compile failure.
Any suggestions ?

typedef int (*& func_ptr1)();

int goo()
{
cout << "un goo" << endl;
return 0;
}


int main()
{

func_ptr1 p1 = goo;
}

The error is below

1>d:\profiles\waz003\my documents\visual studio 2008\projects
\hw5_3\hw5_3\hw5_3.cpp(174) : error C2440: 'initializing' : cannot
convert from 'int (__cdecl *)(void)' to 'func_ptr1'


Victor Bazarov

unread,
Feb 8, 2010, 8:12:43 AM2/8/10
to

I believe the reference to non-const requires a modifiable l-value for
initialisation. Your 'goo' is not an lvalue. You can either do

typedef int (* const & func_ptr_ref)();

...

func_ptr_ref pr = goo;

or define a variable of the type 'pointer to function' and initialise
your reference with it:

...
int main()
{
int (*some_fptr)();
func_ptr1 p1 = some_fptr;
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Asif Zaidi

unread,
Feb 8, 2010, 8:40:11 AM2/8/10
to

Thanks Victor - a few questions though

The first option you state: why does it work. I had the same thing you
had except I did NOT have the const in the typedef definition. Why
would the const help ?

The second option you state: that is just a function ptr is it not. It
is not a reference to a ptr to a function which is what I want ?

Thanks

Asif


Syron

unread,
Feb 8, 2010, 9:34:48 AM2/8/10
to
Am 08.02.2010 14:40, schrieb Asif Zaidi:
> On Feb 8, 7:12 am, Victor Bazarov<v.Abaza...@comAcast.net> wrote:
> [...]

>
> Thanks Victor - a few questions though
>
> The first option you state: why does it work. I had the same thing you
> had except I did NOT have the const in the typedef definition. Why
> would the const help ?
>
> The second option you state: that is just a function ptr is it not. It
> is not a reference to a ptr to a function which is what I want ?
>
>
>
> Thanks
>
> Asif
>
>

if a function pointer would not be const, you could do some REALLY
stupid things like "funcPtr++".

and as I now think about that, a reference to a const pointer does not
really make sense, does it?

Victor Bazarov

unread,
Feb 8, 2010, 9:42:32 AM2/8/10
to
>> [..]

Please do not quote signatures.

>
> Thanks Victor - a few questions though
>
> The first option you state: why does it work. I had the same thing you
> had except I did NOT have the const in the typedef definition. Why
> would the const help ?

A reference to const can be initialised with a non-lvalue. That's just
one of the language rules.

> The second option you state: that is just a function ptr is it not. It
> is not a reference to a ptr to a function which is what I want ?

Not sure I understand your question. 'p1' is your reference to a
pointer to function. Assign something to 'p1' and you're going to be
changing 'some_ptr'.

Now, take the "function" out of all this, perhaps it's what confuses
you, and imagine that you have just ints. Now, 12345 is an int, isn't
it? Can you define a reference to an int and initialize it with 12345?
No, you're not allowed to, because 12345 is not an lvalue.

int &ri = 12345;

However, you can define a reference to a const int and initialise it
with that number:

int const & rci = 12345;

(the compiler will create a temporary with that value and make the
temporary live as long as the reference to it).

Or you could have a real int (a variable) and initialise a reference
with it:

int real_int;

int& ri = real_int;

I am not sure this is any clearer, but the principles that govern the
initialisation of references to objects do apply when those objects are
pointers to functions.

SG

unread,
Feb 8, 2010, 12:01:23 PM2/8/10
to
Hallo Syron,

On 8 Feb., 15:34, Syron wrote:
>
> if a function pointer would not be const, you could do some REALLY
> stupid things like "funcPtr++".

Even for a non-const pointer to a function this is illegal.
sizeof(some_funtion) also won't work.

> and as I now think about that, a reference to a const pointer does not
> really make sense, does it?

Agreed. I don't think the OP really wants a reference here.

Cheers,
SG

James Kanze

unread,
Feb 8, 2010, 5:59:51 PM2/8/10
to
On Feb 8, 3:45 am, Asif Zaidi <asifnza...@gmail.com> wrote:
> What does the syntax in subject line mean - how should I read
> it.

According to the rules of type expressions. A reference to a
pointer to a function taking no arguments and returning int.

--
James Kanze

James Kanze

unread,
Feb 8, 2010, 6:05:55 PM2/8/10
to
On Feb 8, 12:58 pm, Asif Zaidi <asifnza...@gmail.com> wrote:
> On Feb 8, 12:28 am, Syron <mr.sy...@googlemail.com> wrote:

> > Am 08.02.2010 04:45, schrieb Asif Zaidi:

> > > What does the syntax in subject line mean - how should I
> > > read it.

> > if this is a typedef (like 'int(*&func)()') it is a


> > reference to a pointer to a function that returns an int.

> I am trying to implement the following and getting compile
> failure. Any suggestions ?

> typedef int (*& func_ptr1)();

> int goo()
> {
> cout << "un goo" << endl;
> return 0;
> }

> int main()
> {
>
> func_ptr1 p1 = goo;
> }

Why? The formal reason why it doesn't work is that you're
trying to initialize a reference to a non-const with an rvalue.
But the real question is "what do you think you're trying to
do?" To make the above work, it's sufficient to change the
typedef to:

typedef int (*const & func_ptr1)();

But while the results may be legal C++, they still don't make
any logical sense.

> The error is below

> 1>d:\profiles\waz003\my documents\visual studio 2008\projects
> \hw5_3\hw5_3\hw5_3.cpp(174) : error C2440: 'initializing' : cannot
> convert from 'int (__cdecl *)(void)' to 'func_ptr1'

The error message could be better, the fact remains: you can't
initialize a reference to a non-const with an rvalue. Given,
however, that at least in the above case, it doesn't make any
sense to even want to, the question remains: what are you really
trying to do?

--
James Kanze


James Kanze

unread,
Feb 8, 2010, 6:09:59 PM2/8/10
to
On Feb 8, 1:40 pm, Asif Zaidi <asifnza...@gmail.com> wrote:
> On Feb 8, 7:12 am, Victor Bazarov <v.Abaza...@comAcast.net> wrote:

[...]


> The first option you state: why does it work. I had the same
> thing you had except I did NOT have the const in the typedef
> definition. Why would the const help ?

Because the standard says so. Basically, when initializing a
reference, you need an lvalue, unless it is a reference to
const. (Strictly speaking, Victor was wrong when he said you
needed a modifiable lvalue: you need an lvalue whose type is not
const, which isn't quite the same thing. Morally, however, if
it's not the same thing, then you're cheating somewhere, and
you're going to have troubles down the road.)

--
James Kanze

James Kanze

unread,
Feb 8, 2010, 6:13:33 PM2/8/10
to
On Feb 8, 2:34 pm, Syron <mr.sy...@googlemail.com> wrote:
> Am 08.02.2010 14:40, schrieb Asif Zaidi:
> > On Feb 8, 7:12 am, Victor Bazarov<v.Abaza...@comAcast.net> wrote:
> > [...]

> > Thanks Victor - a few questions though

> > The first option you state: why does it work. I had the same
> > thing you had except I did NOT have the const in the typedef
> > definition. Why would the const help ?

> > The second option you state: that is just a function ptr is
> > it not. It is not a reference to a ptr to a function which
> > is what I want ?

> if a function pointer would not be const, you could do some


> REALLY stupid things like "funcPtr++".

The ++ operator isn't supported on function pointers. But
non-const function pointers do make sense---in fact, why use a
pointer to a function, instead of calling the function directly,
if the pointer is const?

> and as I now think about that, a reference to a const pointer
> does not really make sense, does it?

Yes and no. It's generally simpler to use the pointer directly,
but such references are likely to occur in templates and such.
If you instantiate:
std::vector< int (*)() >
, some of it's functions, like push_back, will take references
to const pointers.

--
James Kanze

Asif Zaidi

unread,
Feb 9, 2010, 1:59:28 AM2/9/10
to

Thanks for all your replies

0 new messages