By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,015 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,015 IT Pros & Developers. It's quick & easy.

Function lookup problem

P: n/a
Could someone tell me why this function lookup doesn't work (gcc 4.1.2 on
64bit linux)?

struct test
{
const int& func( void ) const {
static const int i = 0;
return i;
}

private:
int& func( void ) {
return const_cast<int&>( static_cast<const test&>(*this).func() );
}
};

int main() {
test t;
int i = t.func(); // error: 'int& test::func()' is private
return i;
}

I'm sure that there is a good reason for it not being able to see the
public const version, but the thing is that I want to give read access to
some boost tuples publicly an write access privately and I would prefer
to keep the "tuple getting" code in one place. I know I could change the
read function's name, or static cast t to <const test&>, but seems a bit
awkward.

What do you think a good solution is?

Cheers,
Brian
Apr 8 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Brian Tyler wrote:
Could someone tell me why this function lookup doesn't work (gcc 4.1.2 on
64bit linux)?

struct test
{
const int& func( void ) const {
static const int i = 0;
return i;
}

private:
int& func( void ) {
return const_cast<int&>( static_cast<const test&>(*this).func() );
}
};

int main() {
test t;
int i = t.func(); // error: 'int& test::func()' is private
return i;
}

I'm sure that there is a good reason for it not being able to see the
public const version, but the thing is that I want to give read access to
some boost tuples publicly an write access privately and I would prefer
to keep the "tuple getting" code in one place. I know I could change the
read function's name, or static cast t to <const test&>, but seems a bit
awkward.

What do you think a good solution is?
Because overload resolution and visibility are orthogonal. It finds the
non-const version of func(), and complains because it's private.

Your solution? Rename the non-const version of func().

Apr 8 '08 #2

P: n/a
Brian Tyler wrote:
Could someone tell me why this function lookup doesn't work (gcc
4.1.2 on 64bit linux)?
Why do you say it doesn't work? It works just fine giving you the
right function as you wrote it. Perhaps you don't know the rules
of the lookup, but that's not a gcc's problem, is it?
>
struct test
{
const int& func( void ) const {
static const int i = 0;
return i;
}

private:
int& func( void ) {
return const_cast<int&>( static_cast<const test&>(*this).func()
); }
};

int main() {
test t;
int i = t.func(); // error: 'int& test::func()' is private
It *is* private, and accessing private members from a non-friend, non-
member *is* prohibited. Why are you surprised?
>

return i;
}

I'm sure that there is a good reason for it not being able to see the
public const version,
It sees it fine. It also sees the private non-const "version",
and the rules of overload resolution tell the compiler to pick
the non-const version (the private one) because 't' isn't const.
but the thing is that I want to give read
access to some boost tuples publicly an write access privately and I
would prefer to keep the "tuple getting" code in one place. I know I
could change the read function's name, or static cast t to <const
test&>, but seems a bit awkward.

What do you think a good solution is?
As you point out, "read" and "write" are different actions, so
you should probably name your functions differently.

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

P: n/a
I had guessed as much, but thought that some sort of automatic conversion
might take place... reminds me of my introduction operators and
namespaces.

I will rename the read function.

Thanks for the help.

Brian
Apr 8 '08 #4

P: n/a
"Brian Tyler" <br*********@gmail.comwrote in message
news:Aa******************@newsfe1-gui.ntli.net...
Could someone tell me why this function lookup doesn't work (gcc 4.1.2 on
64bit linux)?
[...]
>
Your calling func on a non-const test object. Try this to force it work:
__________________________________________________ ____
struct test
{
const int& func( void ) const {
static const int i = 0;
return i;
}

private:
int& func( void ) {
return const_cast<int&>( static_cast<const test&>(*this).func() );
}
};

int main() {
test t;
test const& tx = t;
int i = tx.func();

return i;
}
__________________________________________________ ____

What do you think a good solution is?
Rename your private function. I personally like to add either a 'sys_' or a
'prv_' prefix to my private function names. In other words use
'test::sys/prv_func()' instead...

Apr 8 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.