Topic: Inheriting a templated class & name lookups
Author: vutov.yavor@bulgaria.com
Date: Thu, 27 Jan 2005 12:28:36 CST Raw View
Hello, I had such code:
template<class T>
class Base{
protected:
T n;
};
template<class T>
class Derived:public Base<T>{
using Base<T>::n // line #1
public:
Derived(){
n = 0; // line #2
}
};
Without the using directive at line #1 I get error "unknown identifier
n at line #2 (using gcc 3.4.2 & Comeau)
It used to work without using nn earlier versions of gcc.
Could somebody please explain me, why using directive is necessery?
In fact using using makes no difference if n is public, privet or
protected... I find this annoing.
Thank you.
---
[ 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: sumit.rajan@gmail.com ("Sumit Rajan")
Date: Fri, 28 Jan 2005 02:41:56 GMT Raw View
<vutov.yavor@bulgaria.com> wrote in message
news:1106813783.896310.151620@z14g2000cwz.googlegroups.com...
> Hello, I had such code:
>
> template<class T>
> class Base{
> protected:
> T n;
> };
>
> template<class T>
> class Derived:public Base<T>{
> using Base<T>::n // line #1
> public:
> Derived(){
> n = 0; // line #2
> }
> };
>
> Without the using directive at line #1 I get error "unknown identifier
> n at line #2 (using gcc 3.4.2 & Comeau)
>
> It used to work without using nn earlier versions of gcc.
> Could somebody please explain me, why using directive is necessery?
> In fact using using makes no difference if n is public, privet or
> protected... I find this annoing.
This link may help:
http://parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.17
You will also find a detailed explanation of the issue on Chapter 9 of "C++
Templates" (Vandevoorde/Josuttis).
--
Sumit Rajan <sumit.rajan@gmail.com>
---
[ 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: v.Abazarov@comAcast.net ("Victor Bazarov")
Date: Fri, 28 Jan 2005 04:37:42 GMT Raw View
<vutov.yavor@bulgaria.com> wrote...
> Hello, I had such code:
>
> template<class T>
> class Base{
> protected:
> T n;
> };
>
> template<class T>
> class Derived:public Base<T>{
> using Base<T>::n // line #1
> public:
> Derived(){
> n = 0; // line #2
> }
> };
>
> Without the using directive at line #1 I get error "unknown identifier
> n at line #2 (using gcc 3.4.2 & Comeau)
>
> It used to work without using nn earlier versions of gcc.
> Could somebody please explain me, why using directive is necessery?
'n' is a type-dependent name. It depends on what 'T' is. When trying
to resolve names in templates, base class scopes are not searched.
By declaring "using" of that name, you (a) explicitly specify where it
should be found and (b) instruct the compiler to bring it into the
Derived class scope.
To learn more, search Google Groups for "dependent name lookup" or
something like that.
> In fact using using makes no difference if n is public, privet or
> protected... I find this annoing.
We can't do anything about that...
V
---
[ 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: hyrosen@mail.com (Hyman Rosen)
Date: Fri, 28 Jan 2005 04:37:28 GMT Raw View
vutov.yavor@bulgaria.com wrote:
> Could somebody please explain me, why using directive is necessery?
14.6.2/3
At the point of instantiation of a Derived<T>, it may be that there is
a specialization of Base<T> which does not contain an 'n'. Therefore it
is inappropriate to do name lookup in the unspecialized Base template.
For example,
int n;
template <typename T> struct Base { T n; };
template <typename T> struct Derived : Base<T> { void f() { ++n; } };
template <> struct Base<int> { };
int main() { n = 0; Derived<int> di; di.f(); return n; }
---
[ 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: "Yavor Vutov" <vutov.yavor@bulgaria.com>
Date: Fri, 28 Jan 2005 21:26:59 CST Raw View
Thank you for thre replays
"Victor Bazarov" wrote:
> <vutov.yavor@bulgaria.com> wrote...
> > Hello, I had such code:
> >
> > template<class T>
> > class Base{
> > protected:
> > T n;
> > };
> >
> > template<class T>
> > class Derived:public Base<T>{
> > using Base<T>::n // line #1
> > public:
> > Derived(){
> > n = 0; // line #2
> > }
> > };
> >
> > Without the using directive at line #1 I get error "unknown
identifier
> > n at line #2 (using gcc 3.4.2 & Comeau)
> >
> > It used to work without using nn earlier versions of gcc.
> > Could somebody please explain me, why using directive is necessery?
>
> 'n' is a type-dependent name. It depends on what 'T' is. When
trying
> to resolve names in templates, base class scopes are not searched.
> By declaring "using" of that name, you (a) explicitly specify where
it
> should be found and (b) instruct the compiler to bring it into the
> Derived class scope.
>
> To learn more, search Google Groups for "dependent name lookup" or
> something like that.
>
> > In fact using using makes no difference if n is public, privet or
> > protected... I find this annoing.
>
> We can't do anything about that...
Yes, but I think somebody should. In these days C++ language becomes
less and less expressive. Just to code somthing beautifuly & easyly in
one line one should write several lines with typedefs, usings,
typenames.... C++ becomes more powerful but at what price?
>
> V
>
>
> ---
> [ 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: "msalters" <Michiel.Salters@logicacmg.com>
Date: Wed, 2 Feb 2005 13:06:53 CST Raw View
vutov.yavor@bulgaria.com wrote:
> Hello, I had such code:
>
> template<class T>
> class Base{
> protected:
> T n;
> };
>
> template<class T>
> class Derived:public Base<T>{
> using Base<T>::n // line #1
> public:
> Derived(){
> n = 0; // line #2
> }
> };
>
> Without the using directive at line #1 I get error "unknown
identifier
> n at line #2 (using gcc 3.4.2 & Comeau)
You can also write
Derived(){
this->n = 0;
}
Since this-> is a dependent name (its type depends on T) anything
following it will be looked up once T is known. This will mean
the lookup of n is delayed until T is known, and then Base<T>::n
will be found.
HTH,
Michiel Salters
---
[ 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 ]