Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > point of definition or instantiation of a template

Reply
Thread Tools

point of definition or instantiation of a template

 
 
ES Kim
Guest
Posts: n/a
 
      04-14-2005
Please consider this code:

void foo(int);

template<typename T>
struct S
{
void f()
{
foo(1); // (1)
T t;
foo(t); // (2)
}
};

void foo(int*);

// <--- point of instantiation
int main()
{
S<int*> a;
a.f();
}

In (1), foo is not a dependent name, so point of definition binding applies
and void foo(int) is called. But what about (2)? I think foo IS a dependent
name there and point of instantiation binding applies. At that point,
void foo(int*) is in scope, which means the code has no problem.
But Comeau rejects the code. Is it correct or not?

--
ES Kim


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-14-2005
ES Kim wrote:
> Please consider this code:
>
> void foo(int);
>
> template<typename T>
> struct S
> {
> void f()
> {
> foo(1); // (1)
> T t;
> foo(t); // (2)
> }
> };
>
> void foo(int*);
>
> // <--- point of instantiation
> int main()
> {
> S<int*> a;
> a.f();
> }
>
> In (1), foo is not a dependent name, so point of definition binding applies
> and void foo(int) is called. But what about (2)? I think foo IS a dependent
> name there


You made me look up the rules for dependent names, and, yes, you're
correct here, 'foo' is a dependent name.

> and point of instantiation binding applies. At that point,
> void foo(int*) is in scope, which means the code has no problem.
> But Comeau rejects the code. Is it correct or not?


Seems to be a bug. Have you tried contacting Comeau?

FWIW, Visual C++ 2003 gets it right. g++ v2.95.2 gets it right. g++
v3.2.2 gets it right. MIPSpro v7.4 gets it right. HP's aCC v3.50 gets
it right... I decided to stop at that point.

V
 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      04-14-2005
ES Kim wrote in news:d3kocl$nfm$(E-Mail Removed) in comp.lang.c++:

> Please consider this code:
>
> void foo(int);
>
> template<typename T>
> struct S
> {
> void f()
> {
> foo(1); // (1)
> T t;
> foo(t); // (2)
> }
> };
>
> void foo(int*);
>
> // <--- point of instantiation
> int main()
> {
> S<int*> a;
> a.f();
> }
>
> In (1), foo is not a dependent name, so point of definition binding
> applies and void foo(int) is called. But what about (2)?


(2) is a dependant name, it depends on T a template paramiter, it can be
nothing else.

> I think foo
> IS a dependent name there and point of instantiation binding applies.


foo( t ) (2) can only be bound to function's that were declared prior
to the defenition (body) of S<>:f(), only function's found by ADL
(Argument dependent Lookup) are allowed to bypass this rule.

In this case its foo( int ) as that is the *only* foo available when
S<>::f() is parsed.

> At that point, void foo(int*) is in scope,


foo(int *) is an overload of foo(int) however it isn't found by ADL
as ADL isn't used when all paramiters are pure inbult types (int,
int * etc).

For ADL to be used T would need to be declared in a different namespace
to S<>, in which case the compiler would use ADL to look for overloads
foo( convertible_from_T ) in T's namespace, whether or not they were
declared before the defenition of s<>::f().

> which means the code has no
> problem. But Comeau rejects the code. Is it correct or not?
>


Its correct an int * can't be converted to an int without a cast.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
ES Kim
Guest
Posts: n/a
 
      04-15-2005
"Victor Bazarov" <(E-Mail Removed)> wrote in message
newsmu7e.63200$(E-Mail Removed)01.us.t o.verio.net...
> ES Kim wrote:
> > and point of instantiation binding applies. At that point,
> > void foo(int*) is in scope, which means the code has no problem.
> > But Comeau rejects the code. Is it correct or not?

>
> Seems to be a bug. Have you tried contacting Comeau?


Here you and Rob go separate ways.
Rob's answer looks more convincing, though.

>
> FWIW, Visual C++ 2003 gets it right. g++ v2.95.2 gets it right. g++
> v3.2.2 gets it right. MIPSpro v7.4 gets it right. HP's aCC v3.50 gets
> it right... I decided to stop at that point.


Whoa.. You have quite many machines in your reach.
Thank you for the information.

--
ES Kim


 
Reply With Quote
 
ES Kim
Guest
Posts: n/a
 
      04-15-2005
"Rob Williscroft" <(E-Mail Removed)> wrote in message
news:Xns9638DAD2CA827rtwfreenetREMOVEcouk@216.196. 109.145...
> ES Kim wrote in news:d3kocl$nfm$(E-Mail Removed) in comp.lang.c++:
>>
> > I think foo
> > IS a dependent name there and point of instantiation binding applies.

>
> foo( t ) (2) can only be bound to function's that were declared prior
> to the defenition (body) of S<>:f(), only function's found by ADL
> (Argument dependent Lookup) are allowed to bypass this rule.
>
> In this case its foo( int ) as that is the *only* foo available when
> S<>::f() is parsed.
>
> > At that point, void foo(int*) is in scope,

>
> foo(int *) is an overload of foo(int) however it isn't found by ADL
> as ADL isn't used when all paramiters are pure inbult types (int,
> int * etc).
>
> For ADL to be used T would need to be declared in a different namespace
> to S<>, in which case the compiler would use ADL to look for overloads
> foo( convertible_from_T ) in T's namespace, whether or not they were
> declared before the defenition of s<>::f().
>
> > which means the code has no
> > problem. But Comeau rejects the code. Is it correct or not?
> >

>
> Its correct an int * can't be converted to an int without a cast.


Yes, now it makes sense. Some guy tried a slightly tweaked version.

void foo(int);

template<typename T>
struct S
{
void f()
{
foo(1); // (1)
T t;
foo(t); // (2)
}
};

class C {}; // modified
void foo(C); // modified

// <--- point of instantiation
int main()
{
S<C> a; // modified
a.f();
}

Now Comeau compiles clean, since 'C' is a user-defined name and ADL mechanism
works according to your explanation.
I remember I was helped with ADL from you months ago. Thank you again.

--
ES Kim


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
Template instantiation conflict with function definition Alessandro [AkiRoss] Re C++ 3 05-13-2009 10:08 AM
context in template point of instantiation (g++) carlos.urena.almagro@gmail.com C++ 4 01-16-2007 04:49 PM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
Can a successfully compileable template definition fail on instantiation? David C++ 2 05-13-2004 11:59 AM



Advertisments