473,387 Members | 1,561 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,387 software developers and data experts.

Function lookup problem

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
4 1442
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Jacek Generowicz | last post by:
I have a program in which I make very good use of a memoizer: def memoize(callable): cache = {} def proxy(*args): try: return cache except KeyError: return cache.setdefault(args,...
11
by: John Collyer | last post by:
Hi, In assembly language you can use a lookup table to call functions. 1. Lookup function address in table 2. Call the function Like: CALL FUNCTION
8
by: Lucas Lemmens | last post by:
Dear pythonians, I've been reading/thinking about the famous function call speedup trick where you use a function in the local context to represent a "remoter" function to speed up the 'function...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
3
by: Janross | last post by:
I'm having trouble with a query that's prohibitively slow. On my free-standing office computer it's fine (well, 2-4 seconds), but on the client's network, it takes at least 5 minutes to run. ...
45
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
9
by: cmk128 | last post by:
Hi Why put * in front of the function name in the declaration? int (*write)(struct inode *, struct file *, const char *, int); thanks from Peter (cmk128@hotmail.com)
4
by: Prasad | last post by:
HI. I have written the following code..in VC++(win32 console with MFC support) CMapStringToPtr chat; vector<UserMessage *> v; /* UserMessage is a class.. */
4
by: StephQ | last post by:
I need to know if it is possible to solve the following problem (I am really stuck at it). Consider the CRTP pattern: struct Derived { ....// May contain void operate( double x ) , may not....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.