Topic: Dependent names an dtwo-pased lookup


Author: kanze@gabi-soft.de (James Kanze)
Date: Mon, 27 Jan 2003 22:56:22 +0000 (UTC)
Raw View
zei2003a@liwest.at (Helmut Zeisel) wrote in message
news:<3E3104BC.7040404@liwest.at>...
> James Kanze wrote:
> > dave@boost-consulting.com (David Abrahams) wrote in message

> >>It's either a "feature" or an "unimplemented feature", depending on
> >>how you look at it (some people don't like 2-phase lookup).

> > You mean I'm not the only one:-).
>  ...
> > Calling base class member functions in a templated derived class is
> > pretty frequent, at least in what little template code I've seen.
> > And practically speaking, none of the existing code base uses this->
> > to do it.

> OTOH, for a compiler without 2-phase lookup, in a template class with
> a template parameter as its base you have to prefix every non-member
> with the global scope operator "::" to avoid surprising results for
> template parameters with a member of the same name.  Is "::" really
> used in the existing code base?

A little.  More than this->, perhaps, but certainly less than it should
be necessary.

> What happens more frequently in the existing code base, call to
> members or call to globals?

I think it depends on what the template is doing.  In a template class
with a base class, I suspect that calls to base class members are more
frequent.  In a template class without a base class, or in a template
function, of course, there will be no calls to base class members.

--
James Kanze                           mailto:jkanze@caicheuvreux.com
Conseils en informatique orient   e objet/
                    Beratung in objektorientierter Datenverarbeitung

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: anthony.williamsNOSPAM@anthonyw.cjb.net (Anthony Williams)
Date: Fri, 24 Jan 2003 19:24:26 +0000 (UTC)
Raw View
kanze@gabi-soft.de (James Kanze) writes:

> dave@boost-consulting.com (David Abrahams) wrote in message
> news:<u8yxcyjug.fsf@boost-consulting.com>...
> > zei2003a@liwest.at (Helmut Zeisel) writes:
>
> > > Anthony Williams wrote:
> > >> zei2003a@liwest.at (Helmut Zeisel) writes:
>
> > >>>What should be the correct output of the program below:
>
> > >>>"12" or "22"?
> > >> 22
>
> > >>>GCC 3.2 produces "12".
>
> > > So this is a bug in GCC and I (or someone else) should make an entry
> > > in the GCC bug data base.
>
> > It's either a "feature" or an "unimplemented feature", depending on
> > how you look at it (some people don't like 2-phase lookup).

> Any serious implementation of it will have an option to turn it off, in
> order to avoid breaking existing code.  Calling base class member
> functions in a templated derived class is pretty frequent, at least in
> what little template code I've seen.  And practically speaking, none of
> the existing code base uses this-> to do it.

I have to admit that even though I know the rule, I rarely use this-> for
calling member functions of a dependent base, simply because I forget when
writing the code and none of the compilers I use warn me about it. If
compilers started implementing 2-phase lookup correctly, I would have to
revisit my code and verify that the correct function is being called in each
case. Not a trivial task, and I expect that in some cases people would find
this unworkable. It is my opinion, however, that this is a case of "more fool
me", since I've been too lazy to write my code correctly in the first place
--- just like any code written with the "old" for-loop scoping rules since it
became part of the Standard; it is trivial to write code that is correct in
both cases, so one really should.

However, just as for the for-loop scoping rules, I would expect compilers to
provide an option to turn it off, as you say.

Anthony
--
Anthony Williams
Senior Software Engineer, Beran Instruments Ltd.
Remove NOSPAM when replying, for timely response.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: zei2003a@liwest.at (Helmut Zeisel)
Date: Fri, 24 Jan 2003 20:18:26 +0000 (UTC)
Raw View
David Abrahams wrote:

> Except Daveed, of course <wink>.

OK, I will buy his book ;-)

Helmut




---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: zei2003a@liwest.at (Helmut Zeisel)
Date: Fri, 24 Jan 2003 20:20:02 +0000 (UTC)
Raw View
James Kanze wrote:
> dave@boost-consulting.com (David Abrahams) wrote in message

>>It's either a "feature" or an "unimplemented feature", depending on
>>how you look at it (some people don't like 2-phase lookup).
>
> You mean I'm not the only one:-).
...
> Calling base class member
> functions in a templated derived class is pretty frequent, at least in
> what little template code I've seen.  And practically speaking, none of
> the existing code base uses this-> to do it.

OTOH, for a compiler without 2-phase lookup,
in a template class with a template parameter as its base
you have to prefix every non-member with the global scope operator "::"
to avoid surprising results for template parameters with a member of the
same name.
Is "::" really used in the existing code base?
What happens more frequently in the existing code base, call to members
or call to globals?

Helmut

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: dave@boost-consulting.com (David Abrahams)
Date: Thu, 23 Jan 2003 00:32:45 +0000 (UTC)
Raw View
zei2003a@liwest.at (Helmut Zeisel) writes:

> Anthony Williams wrote:
>> zei2003a@liwest.at (Helmut Zeisel) writes:
>>
>>>What should be the correct output of the program below:
>>>
>>>"12" or "22"?
>> 22
>>
>>>GCC 3.2 produces "12".
>
> So this is a bug in GCC and I (or someone else) should
> make an entry in the GCC bug data base.

It's either a "feature" or an "unimplemented feature", depending on
how you look at it (some people don't like 2-phase lookup).  The GCC
developers know about it.  Adding a bug report will probably get it
closed as duplicate, but it might help to add pressure in favor of
fixing it.

>> bar on its own is not a dependent name, so it is looked up at the
>> point of the template definition. Members of dependent base classes
>> are not considered.
>
> Please can someone point me to the respective sections in the standard
> so that I can refer to them in the GCC bug data base entry.

IMO, this is really not something you should ask someone else to do.
Almost nobody knows the section numbers by heart, so whoever does this
for you will end up doing the same work you'd have done.

> PS: VC++ 6.0 and VC++ 7.0 also produce the result 12

Microsoft has said publicly that 7.1 will not have it either.  To my
knowledge only EDG and Metrowerks have implemented 2-phase lookup.

--
                       David Abrahams
   dave@boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: schnitker@sigma-c.com (Uwe Schnitker)
Date: Thu, 23 Jan 2003 08:14:28 +0000 (UTC)
Raw View
google@vandevoorde.com (Daveed Vandevoorde) wrote in message news:<52f2f9cd.0301221241.151fa4e8@posting.google.com>...
> schnitker@sigma-c.com (Uwe Schnitker) wrote:
> [...]
> > Anyone like to make the call? And give me some nice reference
> > on the subject? (I'm quite sure I read something about this issue
> > quite recently, but I didn't found it with googling - shame on me.)
>
> You're correct.  For references:
>   . Section 14.6 in the standard
>   . Chapter 9 ("Names in Templates") of
>       "C++ Templates: The Complete Guide"
>
>         Daveed

Thank you, Daveed. In fact, I was counting on you.

And in the German posting, I mentioned that even if I don't yet have
a copy of your book available, I might as well shoot in the dark and
reference chapter 9, knowing that the explanation just has to be there.

Regards,

Uwe

> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: zei2003a@liwest.at (Helmut Zeisel)
Date: Thu, 23 Jan 2003 17:46:44 +0000 (UTC)
Raw View
David Abrahams wrote:

> The GCC
> developers know about it.  Adding a bug report will probably get it
> closed as duplicate, but it might help to add pressure in favor of
> fixing it.

OK. I will not add a bug report.

>>Please can someone point me to the respective sections in the standard
>>so that I can refer to them in the GCC bug data base entry.
>
> IMO, this is really not something you should ask someone else to do.
> Almost nobody knows the section numbers by heart, so whoever does this
> for you will end up doing the same work you'd have done.

If no one did it up to now, you are right.
OTOH, if someone has done similar work already, then *I* would not end
up doing the same work again.
But anyway, since I will not add a bug report, this is no longer important.

Helmut







---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kanze@gabi-soft.de (James Kanze)
Date: Thu, 23 Jan 2003 19:12:01 +0000 (UTC)
Raw View
dave@boost-consulting.com (David Abrahams) wrote in message
news:<u8yxcyjug.fsf@boost-consulting.com>...
> zei2003a@liwest.at (Helmut Zeisel) writes:

> > Anthony Williams wrote:
> >> zei2003a@liwest.at (Helmut Zeisel) writes:

> >>>What should be the correct output of the program below:

> >>>"12" or "22"?
> >> 22

> >>>GCC 3.2 produces "12".

> > So this is a bug in GCC and I (or someone else) should make an entry
> > in the GCC bug data base.

> It's either a "feature" or an "unimplemented feature", depending on
> how you look at it (some people don't like 2-phase lookup).

You mean I'm not the only one:-).

Seriously, it's a damned if you do, damned if you don't issue.
Implement two phase lookup, and you break your users existing code base;
don't, and you aren't conform.

Any serious implementation of it will have an option to turn it off, in
order to avoid breaking existing code.  Calling base class member
functions in a templated derived class is pretty frequent, at least in
what little template code I've seen.  And practically speaking, none of
the existing code base uses this-> to do it.

--
James Kanze                           mailto:jkanze@caicheuvreux.com
Conseils en informatique orient   e objet/
                    Beratung in objektorientierter Datenverarbeitung

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: dave@boost-consulting.com (David Abrahams)
Date: Thu, 23 Jan 2003 20:28:56 +0000 (UTC)
Raw View
zei2003a@liwest.at (Helmut Zeisel) writes:

> David Abrahams wrote:

>> IMO, this is really not something you should ask
>> someone else to do.
>>
>> Almost nobody knows the section numbers by heart, so whoever does
>> this for you will end up doing the same work you'd have done.

Except Daveed, of course <wink>.

--
                       David Abrahams
   dave@boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: dave@boost-consulting.com (David Abrahams)
Date: Wed, 22 Jan 2003 07:06:39 +0000 (UTC)
Raw View
schnitker@sigma-c.com (Uwe Schnitker) writes:

> Hi all,
>
> in a german newsgroup I posted the following code example:
>
> class foo {
> protected:
>   void bar();
> };
>
> template <class T> Derived : public T {
> public:
>   void tofu() {
>      bar();  //1
>      this->bar(); //2
>   }
> };
>
> Derived<foo> test;
>
> test.tofu(); //3
>
> which should lead to an error in line //1 when
> instantiating Derived<foo>::tofu in line //3,
> since unqualified bar is not a dependent name
> and should be bound at Derived's point of definition,
> where bar cannot be found.
>
> This was to explain why this->bar() is needed here.
>
> However, GCC 3.2 compiles this code and makes it work
> as someone else - IMHO wrongly - expected.
>
> The Comeau online compiler rejects the code as I expected.
>
> Anyone like to make the call?

You and Comeau have it right.

> And give me some nice reference
> on the subject? (I'm quite sure I read something about this issue
> quite recently, but I didn't found it with googling - shame on me.)

There's always the standard ;-)

I've got a "plain English" description at:
http://lists.boost.org/MailArchives/boost/msg40032.php.

-Dave

--
                       David Abrahams
   dave@boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: zei2003a@liwest.at (Helmut Zeisel)
Date: Wed, 22 Jan 2003 13:29:45 +0000 (UTC)
Raw View
I would like to formulate the same question in a slightly different way:

What should be the correct output of the program below:

"12" or "22"?

GCC 3.2 produces "12".
There are, however, good reasons to expect the result "22".
What does the standard say?

Helmut

#include <iostream>
using namespace std;
struct foo {
   void bar(){ cout << 1; };
};
struct foo1 {};
void bar(){ cout << 2; }
template <class T> struct Derived : public T {
   Derived() { bar(); }
};

int main()
{
   Derived<foo> test;
   Derived<foo1> test1;
}

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: anthony.williamsNOSPAM@anthonyw.cjb.net (Anthony Williams)
Date: Wed, 22 Jan 2003 14:33:26 +0000 (UTC)
Raw View
zei2003a@liwest.at (Helmut Zeisel) writes:

> What should be the correct output of the program below:
>
> "12" or "22"?

22

> GCC 3.2 produces "12".
> There are, however, good reasons to expect the result "22".
> What does the standard say?

This is the same as your previous question, so the answer is the same --- bar
on its own is not a dependent name, so it is looked up at the point of the
template definition. Members of dependent base classes are not considered.

Therefore the global bar is (should be) found in both cases.

> #include <iostream>
> using namespace std;
> struct foo {
>    void bar(){ cout << 1; };
> };
> struct foo1 {};
> void bar(){ cout << 2; }
> template <class T> struct Derived : public T {
>    Derived() { bar(); }
> };
>
> int main()
> {
>    Derived<foo> test;
>    Derived<foo1> test1;
> }

Anthony
--
Anthony Williams
Senior Software Engineer, Beran Instruments Ltd.
Remove NOSPAM when replying, for timely response.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: zei2003a@liwest.at (Helmut Zeisel)
Date: Wed, 22 Jan 2003 18:06:17 +0000 (UTC)
Raw View
Anthony Williams wrote:
> zei2003a@liwest.at (Helmut Zeisel) writes:
>
>>What should be the correct output of the program below:
>>
>>"12" or "22"?
>
> 22
>
>>GCC 3.2 produces "12".

So this is a bug in GCC and I (or someone else) should
make an entry in the GCC bug data base.

> bar
> on its own is not a dependent name, so it is looked up at the point of the
> template definition. Members of dependent base classes are not considered.

Please can someone point me to the respective sections in the standard
so that I can refer to them in the GCC bug data base entry.

Thank you,

Helmut

PS: VC++ 6.0 and VC++ 7.0 also produce the result 12

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: google@vandevoorde.com (Daveed Vandevoorde)
Date: Wed, 22 Jan 2003 21:23:01 +0000 (UTC)
Raw View
schnitker@sigma-c.com (Uwe Schnitker) wrote:
[...]
> Anyone like to make the call? And give me some nice reference
> on the subject? (I'm quite sure I read something about this issue
> quite recently, but I didn't found it with googling - shame on me.)

You're correct.  For references:
  . Section 14.6 in the standard
  . Chapter 9 ("Names in Templates") of
      "C++ Templates: The Complete Guide"

        Daveed

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: schnitker@sigma-c.com (Uwe Schnitker)
Date: Tue, 21 Jan 2003 19:05:04 +0000 (UTC)
Raw View
Hi all,

in a german newsgroup I posted the following code example:

class foo {
protected:
  void bar();
};

template <class T> Derived : public T {
public:
  void tofu() {
     bar();  //1
     this->bar(); //2
  }
};

Derived<foo> test;

test.tofu(); //3

which should lead to an error in line //1 when
instantiating Derived<foo>::tofu in line //3,
since unqualified bar is not a dependent name
and should be bound at Derived's point of definition,
where bar cannot be found.

This was to explain why this->bar() is needed here.

However, GCC 3.2 compiles this code and makes it work
as someone else - IMHO wrongly - expected.

The Comeau online compiler rejects the code as I expected.

Anyone like to make the call? And give me some nice reference
on the subject? (I'm quite sure I read something about this issue
quite recently, but I didn't found it with googling - shame on me.)

Regards,

Uwe

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]