C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Which scopes are searched for default template args?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Louis Lavery
Guest





PostPosted: Mon Sep 20, 2004 6:24 pm    Post subject: Which scopes are searched for default template args? Reply with quote



I'm not sure where the compiler looks for default template args.

namespace a
{
template<typename T,typename U = XXX struct A {/*...*/};
}

namespace b
{
struct Y {/*...*/};

a::A<Y> AY; // [1]
}

At [1], where does the compiler look for XXX<Y>?

Louis.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
JarlOstensen
Guest





PostPosted: Tue Sep 21, 2004 11:43 am    Post subject: Re: Which scopes are searched for default template args? Reply with quote



"Louis Lavery" <louis (AT) laver (DOT) demon.co.uk> wrote

Quote:
I'm not sure where the compiler looks for default template args.

namespace a
{
template<typename T,typename U = XXX struct A {/*...*/};
}

namespace b
{
struct Y {/*...*/};

a::A<Y> AY; // [1]
}

At [1], where does the compiler look for XXX<Y>?


Hi Louis,
I don't have the standard at hand (dog ate my hard-drive etc.) but
this is an "ADL" situation for templates. If you change your example
to:

template<typename T> struct XXX { /*...*/ };
namespace a
{
template<typename T> struct XXX { /*...*/ };
template<typename T,typename U = XXX struct A { /*...*/ };
}

namespace b
{
struct Y { };
a::A<Y> AY; // [1]
}

Then (if argument dependent lookup rules apply) U would be equal to
a::XXX. I.e. the compiler will match the argument from the namespace
of A "outwards". The first match it finds is a::XXX, and so that is
what it picks.
If you add an XXX to b the compiler doesn't even try to match it, i.e.
if you _only_ had an XXX in b then it would fail to compile. (Unless
of course b::XXX was defined before A).

Someone else needs to explain the details of why the match isn't
continued across namespaces (like it would be if you were considering
functions for instance). Alas I can't get to my copy of Ye Olde
Standard at the moment.

cheers,
jarl

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Tue Sep 21, 2004 6:35 pm    Post subject: Re: Which scopes are searched for default template args? Reply with quote



Louis Lavery wrote:
Quote:
I'm not sure where the compiler looks for default template args.

namespace a
{
template<typename T,typename U = XXX struct A {/*...*/};
}

namespace b
{
struct Y {/*...*/};

a::A<Y> AY; // [1]
}

At [1], where does the compiler look for XXX<Y>?

It should look for XXX itself at the point of definition of a::A.
XXX<Y> may be explicitly specialised or instantiated anywhere before
the point where a::A<Y> is instantiated; otherwise it will be
implicitly instantiated.

--
Ben Hutchings
The generation of random numbers is too important to be left to chance.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Tue Sep 21, 2004 7:05 pm    Post subject: Re: Which scopes are searched for default template args? Reply with quote

JarlOstensen wrote:
Quote:
"Louis Lavery" <louis (AT) laver (DOT) demon.co.uk> wrote in message
news:<cilt4r$dkk$1$8302bc10 (AT) news (DOT) demon.co.uk>...
I'm not sure where the compiler looks for default template args.

namespace a
{
template<typename T,typename U = XXX struct A {/*...*/};
}

namespace b
{
struct Y {/*...*/};

a::A<Y> AY; // [1]
}

At [1], where does the compiler look for XXX<Y>?


Hi Louis,
I don't have the standard at hand (dog ate my hard-drive etc.) but
this is an "ADL" situation for templates.
snip


ADL only applies to function calls and their arguments, not to uses of
templates and their template arguments.

--
Ben Hutchings
The generation of random numbers is too important to be left to chance.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.