473,654 Members | 3,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

gcc doesn't try Koenig lookup

I'm trying to compile the following code with gcc (version below). I
would expect the compile to try the operator ==() declared in
Tangram::Backen d because of Koenig lookup. But it doesn't happen. Is
it me or the compiler?

namespace Tangram {
namespace Backend {

struct RELATIONAL;

template<typena me B>
struct Expression_Hand le {
Expression_Hand le();
Expression_Hand le(int);
};

template<typena me B>
struct Predicate_Handl e {
};

template<typena me B>
Predicate_Handl e<B> operator ==(const Expression_Hand le<B>&, const Expression_Hand le<B>&);
}

namespace Relational {
using Backend::RELATI ONAL;
typedef Backend::Expres sion_Handle<REL ATIONAL> Expression;
}
}

void foo() {
using namespace Tangram::Relati onal;
Expression expr;
expr == Expression(666) ; // ok
expr == 666; // error: no match for 'operator==' in 'expr == 666'
}

Here's my gcc:
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=releas e --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++ ,objc,java,f95, ada --enable-java-awt=gtk --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --host=i386-redhat-linux
Thread model: posix
gcc version 4.0.1 20050727 (Red Hat 4.0.1-5)
--
Jean-Louis Leroy
Sound Object Logic
http://www.soundobjectlogic.com
Nov 1 '05 #1
2 1924
Jean-Louis Leroy wrote:
I'm trying to compile the following code with gcc (version below). I
would expect the compile to try the operator ==() declared in
Tangram::Backen d because of Koenig lookup. But it doesn't happen. Is
it me or the compiler?

namespace Tangram {
namespace Backend {

struct RELATIONAL;

template<typena me B>
struct Expression_Hand le {
Expression_Hand le();
Expression_Hand le(int);
};

template<typena me B>
struct Predicate_Handl e {
};

template<typena me B>
Predicate_Handl e<B> operator ==(const Expression_Hand le<B>&, const Expression_Hand le<B>&);
}

namespace Relational {
using Backend::RELATI ONAL;
typedef Backend::Expres sion_Handle<REL ATIONAL> Expression;
}
}

void foo() {
using namespace Tangram::Relati onal;
Expression expr;
expr == Expression(666) ; // ok
expr == 666; // error: no match for 'operator==' in 'expr == 666'
}


The operator== is found in the appropriate namespace as this variation
shows:

namespace Tangram {
namespace Backend
{

struct RELATIONAL;

struct Expression_Hand le
{
Expression_Hand le() {};
Expression_Hand le(int) {};
};

struct Predicate_Handl e { };

Predicate_Handl e
operator ==(const Expression_Hand le&, const Expression_Hand le&)
{
return Predicate_Handl e();
}
}

namespace Relational
{
using Backend::RELATI ONAL;
typedef Backend::Expres sion_Handle Expression;
}
}

int main()
{
using namespace Tangram::Relati onal;
Expression expr;
expr == Expression(666) ; // ok
expr == 666; // ok
}

The problem is not the name lookup, but the fact that expr == 666 is
not enough for the compiler instantiate the template class
Backend::Expres sion_Handle<REL ATIONAL> implicitly.

One solution would be add an int conversion method to Expression. But
since implicit conversions are best avoided, a more straightforward
solution would be to implement the == operator for integer values:

template<typena me B>
Predicate_Handl e<B>
operator ==(const Expression_Hand le<B>& lhs, int rhs)
{
return lhs == Predicate_Handl e<B>(rhs);
}

Greg

Nov 1 '05 #2
> The problem is not the name lookup, but the fact that expr == 666 is
not enough for the compiler instantiate the template class
Backend::Expres sion_Handle<REL ATIONAL> implicitly.
I wonder what the standard has got to say on this (unfortunately my
copy is at the other side of town and I haven't navigate the thing for
a while). But this flies in the face of intuition IMO. Mine says that
the template declares an infinite set of overloaded 'operator =()'
functions that should contribute to the set of candidate
functions. One of them happens to be viable (and the only one at that)
at the cost of one user-defined conversion.
One solution would be add an int conversion method to Expression.
Actually no, the code I posted is a simplification of a fragment of an
object-relational mapper. The goal is to produce an AST that will be
translated to SQL.
But since implicit conversions are best avoided, a more
straightforward solution would be to implement the == operator for
integer values:

template<typena me B>
Predicate_Handl e<B>
operator ==(const Expression_Hand le<B>& lhs, int rhs)
{
return lhs == Predicate_Handl e<B>(rhs);
}


Yeah, that's the workaround I use but I have to do it for all
operators :-(
--
Jean-Louis Leroy
Sound Object Logic
http://www.soundobjectlogic.com
Nov 1 '05 #3

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

Similar topics

1
1607
by: Dave | last post by:
#include <iostream> using std::cout; using std::endl; // #define CAUSE_ERROR namespace N { struct foo_t {};
9
1414
by: ES Kim | last post by:
#include <map> #include <iterator> #include <iostream> #include <algorithm> using namespace std; typedef map<int, int> Map; ostream& operator<<(ostream& os, const Map::value_type& v);
11
1700
by: REH | last post by:
I'm a little confused about argument dependent lookup. Exactly when does this apply? Specifically, I was hoping to use it to access enumeration constants. For example: namespace Flags { enum flag_type {F1, F2, F3}; } void foo(Flags::flag_type f) {
3
1220
by: REH | last post by:
I'm a little confused about argument dependent lookup. Exactly when does this apply? Specifically, I was hoping to use it to access enumeration constants. For example: namespace Flags { enum flag_type {F1, F2, F3}; } void foo(Flags::flag_type f) {
5
2341
by: Javier Estrada | last post by:
I'm using a user-defined diagnostics stream and custom manipulators that work with it template<class charT, class traits = std::char_traits<charT> class basic_diagsstream : public std::basic_ostream<charT, traits ... } and a manipulator
2
2042
by: cgv | last post by:
Hi, I want to distinguish between an int parameter to a template function depending on whether its value is known at compile time or not. For the compile time version I want to call the function with the syntax func<5>(); and for the dynamic version with
14
5195
by: thomas | last post by:
I allocated a piece of memory and use memset to set it to 0. ------------------------------------ int *graph = new int; memset(graph, 0, sizeof(graph)); for(int i=0;i<4;i++){ for(int j=0;j<4;j++) cout<<graph<<" "; cout<<endl; } --------------------------------
16
1647
by: Juha Nieminen | last post by:
The so-called koenig lookup allows doing odd things like this: #include <algorithm> #include <string> int main() { std::string table; sort(table, table+10); }
2
2121
by: Peng Yu | last post by:
Hi, I'm wondering if Koenig lookup can be applied somehow to derive which template to use based on the template arguments. The following code shows an example where multiply_traits's namespace A has to be specified in the definition of Y. This makes Y unusable for any multiply_traits defined in other namespaces, which corresponding T1 and T2 defined in those namespaces. See also comments in the code.
0
8815
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8482
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8593
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7306
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.