Topic: Template Instantiation Rule


Author: idanan@ibm.net
Date: 1997/11/13
Raw View
In <01bcee66$f2e80ca0$cb335295@sowmy_W95.attachmate.com>, "Sowmy"
<Sowmy_S@hotmail.com> writes:
>template<class _Ty >
>class T{
>public:
> void Do(const _Ty& _X = _Ty()){}
>};
>
>class A{
>public:
>  class B{int b;};
>  T<B> t;
>};
>
>
>The above code gives a compilation error. Undeclared identifier in "B" in
>Line "void Do(const _Ty& _X = _Ty()){}"
>
>Apparently it looks like that the compiler (I am using the Microsoft's
>MSDEV) is instantiating the template class outside the class A even though
>it is used inside the class. Is it the correct behaviour? (Actually I had
>this problem in using std::vector of a nested class)

A conforming compiler should accept this, however it will not produce what
you're expecting. The class T<A::B> will be a global scoped class. And will
no have any access privilages to class A. To force MSDEV to accept this
try explicitly specifying the class, as in:

class A{
public:
  class B{int b;};
  T<A::B> t;
};

I hope this helps,

Itai
idanan@ibm.net
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Sowmy" <Sowmy_S@hotmail.com>
Date: 1997/11/11
Raw View
template<class _Ty >
class T{
public:
 void Do(const _Ty& _X = _Ty()){}
};

class A{
public:
  class B{int b;};
  T<B> t;
};


The above code gives a compilation error. Undeclared identifier in "B" in
Line "void Do(const _Ty& _X = _Ty()){}"

Apparently it looks like that the compiler (I am using the Microsoft's
MSDEV) is instantiating the template class outside the class A even though
it is used inside the class. Is it the correct behaviour? (Actually I had
this problem in using std::vector of a nested class)

Any kind of help would be greatly appreciated.

Thanks
Sowmy
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: bparker@mailbox.uq.edu.au (Brian Parker)
Date: 1997/11/12
Raw View
On 11 Nov 97 11:36:25 GMT, "Sowmy" <Sowmy_S@hotmail.com> wrote:

>template<class _Ty >
>class T{
>public:
> void Do(const _Ty& _X = _Ty()){}
>};
>
>class A{
>public:
>  class B{int b;};
>  T<B> t;
>};
>
>
>The above code gives a compilation error. Undeclared identifier in "B" in
>Line "void Do(const _Ty& _X = _Ty()){}"
>
>Apparently it looks like that the compiler (I am using the Microsoft's
>MSDEV) is instantiating the template class outside the class A even though
>it is used inside the class. Is it the correct behaviour? (Actually I had
>this problem in using std::vector of a nested class)

I tried this on VC++ 5.0 with SP2 installed with the same result.
It is a bug, but I don't think it has to do with the location of
instantiation- the problem lies with the default argument. If you add
a constructor that takes an int to class B and change the default
argument to _Ty(1) then it compiles OK... weird.

,Brian Parker.
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]