473,511 Members | 15,581 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::Backend because of Koenig lookup. But it doesn't happen. Is
it me or the compiler?

namespace Tangram {
namespace Backend {

struct RELATIONAL;

template<typename B>
struct Expression_Handle {
Expression_Handle();
Expression_Handle(int);
};

template<typename B>
struct Predicate_Handle {
};

template<typename B>
Predicate_Handle<B> operator ==(const Expression_Handle<B>&, const Expression_Handle<B>&);
}

namespace Relational {
using Backend::RELATIONAL;
typedef Backend::Expression_Handle<RELATIONAL> Expression;
}
}

void foo() {
using namespace Tangram::Relational;
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=release --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 1909
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::Backend because of Koenig lookup. But it doesn't happen. Is
it me or the compiler?

namespace Tangram {
namespace Backend {

struct RELATIONAL;

template<typename B>
struct Expression_Handle {
Expression_Handle();
Expression_Handle(int);
};

template<typename B>
struct Predicate_Handle {
};

template<typename B>
Predicate_Handle<B> operator ==(const Expression_Handle<B>&, const Expression_Handle<B>&);
}

namespace Relational {
using Backend::RELATIONAL;
typedef Backend::Expression_Handle<RELATIONAL> Expression;
}
}

void foo() {
using namespace Tangram::Relational;
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_Handle
{
Expression_Handle() {};
Expression_Handle(int) {};
};

struct Predicate_Handle { };

Predicate_Handle
operator ==(const Expression_Handle&, const Expression_Handle&)
{
return Predicate_Handle();
}
}

namespace Relational
{
using Backend::RELATIONAL;
typedef Backend::Expression_Handle Expression;
}
}

int main()
{
using namespace Tangram::Relational;
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::Expression_Handle<RELATIONAL> 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<typename B>
Predicate_Handle<B>
operator ==(const Expression_Handle<B>& lhs, int rhs)
{
return lhs == Predicate_Handle<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::Expression_Handle<RELATIONAL> 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<typename B>
Predicate_Handle<B>
operator ==(const Expression_Handle<B>& lhs, int rhs)
{
return lhs == Predicate_Handle<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
1597
by: Dave | last post by:
#include <iostream> using std::cout; using std::endl; // #define CAUSE_ERROR namespace N { struct foo_t {};
9
1403
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
1675
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...
3
1208
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...
5
2335
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...
2
2022
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...
14
5160
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...
16
1629
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
2110
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...
0
7245
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
7144
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
7427
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...
1
7085
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...
0
7512
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...
1
5069
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...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
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...

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.