[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Koenig's lookup for template argument.

Peng Yu

10/20/2008 8:00:00 PM

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<T1> result_type;
};

template <typename T1, typename T2>
typename multiply_traits<X<T1>, T2>::result_type 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_traits<T1, T2>::result_type>
#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<int> x(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;
}
3 Answers

Hendrik Schober

10/21/2008 3:34:00 PM

0

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

Naked Gonad

10/22/2008 8:26:00 AM

0

Sir John Howard wrote:
> Eli Grubman wrote:
>
>> On Tue, 21 Oct 2008 04:18:07 -0700 (PDT), Sir John Howard
>> <sirjwhoward@gmail.com> wrote:
>>
>>> Panta Rhei is a Grik wog and completely inept stalker with no penis!
>>> LOL wrote:
>>>
>>>> On Tue, 21 Oct 2008 09:00:37 +0100, Naked Gonad
>>>> <bodron57@tiscali.co.uk> wrote:
>>>>
>>>>> Eli Grubman wrote:
>>>>>> On Tue, 21 Oct 2008 08:41:19 +0100, Naked Gonad
>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>
>>>>>>> Eli Grubman wrote:
>>>>>>>> On Mon, 20 Oct 2008 13:04:58 +0100, Naked Gonad
>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>
>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>> On Mon, 20 Oct 2008 10:11:55 +0100, Naked Gonad
>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>
>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>> On Mon, 20 Oct 2008 09:50:17 +0100, Naked Gonad
>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>
>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>> On Sun, 19 Oct 2008 14:51:22 +0100, Naked Gonad
>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>> On Sun, 19 Oct 2008 11:00:50 +0100, Naked Gonad
>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>> On Sat, 18 Oct 2008 13:13:54 +0100, Naked Gonad
>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>> On Sat, 18 Oct 2008 12:42:24 +0100, Naked Gonad
>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>> On Sat, 18 Oct 2008 12:13:58 +0100, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>> On Sat, 18 Oct 2008 09:00:15 +0100, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>> On Fri, 17 Oct 2008 20:59:08 +0100, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Fri, 17 Oct 2008 19:53:22 +0100, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Fri, 17 Oct 2008 18:36:10 +0100, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Fri, 17 Oct 2008 17:36:32 +0100, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Fri, 17 Oct 2008 17:23:41 +0100, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Fri, 17 Oct 2008 16:39:43 +0100, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Fri, 17 Oct 2008 16:12:19 +0100, "Peter Hucker" <none@spam.com>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Tue, 14 Oct 2008 17:49:14 +0100, Eli Grubman <eli.grubman@googlemail.com> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Tue, 14 Oct 2008 17:13:41 +0100, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Tue, 14 Oct 2008 15:54:48 +0100, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Tue, 14 Oct 2008 14:51:44 +0100, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Tue, 14 Oct 2008 14:11:29 +0100, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> You should have gone to Spec...
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Couldn't find it without my glasses.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Ah, they don't tell you that in the commercial.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Yeh, it's especially difficult for me because my
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> guide dog is blind as well.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> He should have gone to Spec...
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> But he's stupid.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> You don't need a lot of smarts to be a guide dog.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Smarties indeed, didn't you know they rear them on maltesers nowadays?
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Isn't rearing dogs bestiality?
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Only if you're caught.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Rearing them with Maltesers is even worse.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Do they melt in your mouth and not up your arse?
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Not if used for rearing.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> More filth.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Yes please.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Greedy bugger.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Greed is good.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Yum yum.
>>>>>>>>>>>>>>>>>>>>>>>>>>>> Sloth is better.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>> Greed + Sloth = Yum Yum.
>>>>>>>>>>>>>>>>>>>>>>>>>> What about Greed + Sloth + Envy + Pride?
>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>> Equals a person with serious issues and they should kill theirself
>>>>>>>>>>>>>>>>>>>>>>>>> immediately.......if not sooner.
>>>>>>>>>>>>>>>>>>>>>>>> What's a Deadly Sin or two between friends?
>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>> Murder?
>>>>>>>>>>>>>>>>>>>>>> Mortal, usually fatal, but not Deadly.
>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>> Killed with love?
>>>>>>>>>>>>>>>>>>>> Smothered.
>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> Sat on face?
>>>>>>>>>>>>>>>>>> Tom Tom!
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>> I don't know who it was.
>>>>>>>>>>>>>>>> Was it a Navvy?
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> It was a hairy arse.
>>>>>>>>>>>>>> Cleavage?
>>>>>>>>>>>>>>
>>>>>>>>>>>>> No, I've had my tits removed.
>>>>>>>>>>>> Colon cancer?
>>>>>>>>>>>>
>>>>>>>>>>> Colon Powell.
>>>>>>>>>> The Black Death?
>>>>>>>>>>
>>>>>>>>> Is that his nickname?
>>>>>>>> No, that was "Colon Bowel".
>>>>>>>>
>>>>>>> Oh yes, the one who talks crap.
>>>>>> And is full of shit.
>>>>>>
>>>>> Sounds like he needs a collonic quick.
>>>> Raise the Thames Barrier!
>>> Are you Poms looking forward to the great flood?
>> Of course. My ark is ready.
>
> Fark, ask the crows say.

"Ark"!! is that a ship I hear?

dascandy@gmail.com

10/29/2008 8:03:00 PM

0

On Oct 20, 9:00 pm, Peng Yu <PengYu...@gmail.com> wrote:
>   template <typename T1, typename T2>
>     Y<typename A::multiply_traits<T1, T2>::result_type>

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.