473,651 Members | 2,512 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Koenig's lookup for template argument.

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.

Thanks,
Peng
#include <iostream>

namespace A {

template <typename T>
class X {
public:
X() { }
X(T t) : _t(t) { }
const T &the_t() const { return _t; }
private:
T _t;
};

template <typename T1, typename T2>
struct multiply_traits ;

template <typename T1, typename T2>
struct multiply_traits <X<T1>, T2{
typedef X<T1result_type ;
};

template <typename T1, typename T2>
typename multiply_traits <X<T1>, T2>::result_typ e operator*(const
X<T1&x, const T2 &t) {
return X<T1>(x.the_t( ) * t);
}

}

namespace B {

template <typename T>
class Y {
public:
Y(T t) : _t(t) { }
const T &the_t() const { return _t; }
private:
T _t;
};

template <typename T1, typename T2>
Y<typename A::multiply_tra its<T1, T2>::result_typ e>
#if 0
I want to specify the template argument without explicitly say the
namespace A.
If this could be possible, Y can be used for types that are defined
in any namespace as long as a corresponding multiply_traits are
defined in such namespace.
But I just don't find such a way.
Is there anybody know if there is any walkaround?
#end if
operator*(const Y<T1&y, const T2 &t) {
return Y<T1>(y.the_t( ) * t);
}
}

int main () {
A::X<intx(2);
B::Y<A::X<int y(x);

std::cout << (x * 3).the_t() << std::endl;
std::cout << (y * 5).the_t().the_ t() << std::endl;
}
Oct 20 '08 #1
2 2120
Peng Yu wrote:
Hi,

I'm wondering if Koenig lookup can be applied somehow to derive which
template to use based on the template arguments.
What you do is compile-time stuff. Koenig lookup,
OTOH, only applies to function parameters. So I
see no way you can use this.
[...]
HTH,

Schobi
Oct 21 '08 #2
On Oct 20, 9:00*pm, Peng Yu <PengYu...@gmai l.comwrote:
* template <typename T1, typename T2>
* * Y<typename A::multiply_tra its<T1, T2>::result_typ e>
IIRC, there was a proposal for "auto" return types somewhere in c++0x.
Also, boost has a template iirc that does just this - determine what
the return value of some operation is. For these concrete problems
those solutions might help you.

As for the abstract problem of template importing from an unknown
namespace, Koenig lookup doesn't apply. You need to specify the
namespace one way or another.

Oct 29 '08 #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 {};
11
1699
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) {
2
1924
by: Jean-Louis Leroy | last post by:
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;
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
29
2100
by: Stefan Slapeta | last post by:
There have been many announciations that VC 8.0 will finally support two phase template lookup; now, after I installing VC 2005 Express I had to realize that it still doesn't! What has happend to these plans??? Stefan
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
13
5583
by: paul.joseph.davis | last post by:
Hi, I've just had my first encounter with two-phase lookup and I'm scratching my head a bit. The idea behind two phase look up is pretty easy to understand, but I have a case that fails to compile although it appears to me that it should. template<typename TYPE> void foo( std::map< int, TYPE pmap )
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); }
0
8352
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8802
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...
0
8697
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
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
8579
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...
1
6158
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
5612
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
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1587
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.