473,583 Members | 3,155 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Name lookup doubt

I was reading the "Exceptiona l C++" of Herb Sutter. In an example, he
mentions the following.

// In some library header:
namespace N { class C{}; }
int operator+(int i, N::C) { return i+1; }
// A mainline to exercise it:
#include <numeric>
int main()
{
N::C a[10];
std::accumulate (a, a+10, 0);
}

He explains what the compiler does inside std::accumulate . There'd be a
call to an operator+ that takes an int and an N::C - "First, the
compiler looks in the immediate scope, in this case the scope of
namespace std, and makes a list of all functions it can find that are
named operator+() (regardless of whether they are accessible or even
take the right number of parameters). Only if it does not find any at
all does it then continue outward into the next enclosing namespace
outside std, which happens to be the global scope - until it eventually
runs out of scopes, without having found a function with the right name
or else finds a scope that contains at least one candidate function."
He then says that the code example above, may or may not compile
depending on whether the implementation of standard header numeric
a) declares an operator+() OR
b) includes any other standard header that does so

The solution to this, he says, is to put the operator+() inside
namespace N

namespace N {
class C{};
int operator+(int i, N::C) { return i+1; }
}

This would compile on any implementation because of Koenig lookup. My
doubt is whether Koenig lookup precedes the name look-up procedure he
explained before - that is, going from current scope outwards,
searching for names. What is the order in which namespaces are searched
for names taking even Koenig lookup into account.

Thanks in advance
Regards,
Srini

Aug 4 '05 #1
1 1986

Srini wrote:
I was reading the "Exceptiona l C++" of Herb Sutter. In an example, he
mentions the following.

// In some library header:
namespace N { class C{}; }
int operator+(int i, N::C) { return i+1; }
// A mainline to exercise it:
#include <numeric>
int main()
{
N::C a[10];
std::accumulate (a, a+10, 0);
}

He explains what the compiler does inside std::accumulate . There'd be a
call to an operator+ that takes an int and an N::C - "First, the
compiler looks in the immediate scope, in this case the scope of
namespace std, and makes a list of all functions it can find that are
named operator+() (regardless of whether they are accessible or even
take the right number of parameters). Only if it does not find any at
all does it then continue outward into the next enclosing namespace
outside std, which happens to be the global scope - until it eventually
runs out of scopes, without having found a function with the right name
or else finds a scope that contains at least one candidate function."
He then says that the code example above, may or may not compile
depending on whether the implementation of standard header numeric
a) declares an operator+() OR
b) includes any other standard header that does so

The solution to this, he says, is to put the operator+() inside
namespace N

namespace N {
class C{};
int operator+(int i, N::C) { return i+1; }
}

This would compile on any implementation because of Koenig lookup. My
doubt is whether Koenig lookup precedes the name look-up procedure he
explained before - that is, going from current scope outwards,
searching for names. What is the order in which namespaces are searched
for names taking even Koenig lookup into account.


It does not matter whether Koening lookup or a version of if that
applies also to non operators - ADL, happens before or after ordinary
lookup, because the set of viable functions is a union of those found
by ordinary lookup and those found by ADL.

§3.4.2/2a:

If the ordinary unqualified lookup of the name finds the declaration of
a class member function, the associated namespaces and classes are not
considered. Otherwise the set of declarations found by the lookup of
the function name is the union of the set of declarations found using
ordinary unqualified lookup and the set of declarations found in the
namespaces and classes associated with the argument types.

Aug 4 '05 #2

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

Similar topics

9
1534
by: John Harrison | last post by:
Both gcc 3.3.1 and VC++ 7.1 compile the following code. struct Outer { struct Inner { int f() { return c; } }; private: static const int c;
5
14791
by: David Rasmussen | last post by:
If I have a string that contains the name of a function, can I call it? As in: def someFunction(): print "Hello" s = "someFunction" s() # I know this is wrong, but you get the idea... /David
16
2293
by: Michael | last post by:
Does someone have a reliable routine to get the domain name of the current user of a web page? Something like NSLookup. I have no problem getting their IP address, but I also want the domaine name. Thanks, -Michael
1
3384
by: KK | last post by:
Hi, Is there any way to change the name of XML node? In the following xml, I want to change the node <FIELD_1> to <FIELD_ITEM> in all the places. Ex: <NewDataSet> <LOOKUP> <FIELD_1>First_Item113</FIELD_1>
0
1028
by: KK | last post by:
Hi, Is there any way to change the name of XML node? In the following xml, I want to change the node <FIELD_1> to <FIELD_ITEM> in all the places. Ex: <NewDataSet> <LOOKUP> <FIELD_1>First_Item113</FIELD_1>
9
5068
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)
5
1956
by: Subhransu Sahoo | last post by:
Hi All, Does the C++ standard tell function overloading can't be done with the return types of two functions ? If so, is it true that the name mangling scheme does not take the return type into account ?I know that C++ standard does not tell anything about name mangling and only talks bout function overloading. But, I have observed that the...
6
4082
by: pj | last post by:
Hi, I 'm currently writing a program that performs transliteration (i.e., converts greek text written using the english alphabet to "pure" greek text using the greek alphabet) as part of my thesis. I have decided to add the capability to convert words using some sort of lookup algorithm as a sidekick to the "normal" conversion algorithm,...
2
1538
by: murali.desikan | last post by:
Hi, ISO/IEC 14882:2003 Section 3.4.1/13 has the following Names declared in the outermost block of the function definition are not found when looked up in the scope of a handler for the function-try-block.
0
7896
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7827
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8184
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8328
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8195
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5701
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3845
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1158
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.