Topic: [Q] function member template in a template class


Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1999/05/21
Raw View
On 10 May 99 16:05:31 GMT, Biju Thomas <b_thomas@ibm.net> wrote:
>Siemel Naran wrote:

>> But I also think the presence of the word 'typename' there is
>> harmless, so maybe the C++ grammar should be changed to allow it.  On,
>> the other hand, allowing a redundant 'typename' doesn't solve any big
>> problems and might accidentally cause other problems.
>
>I think that a redundant 'typename' keyword will be harmless always.
>What type of accidental problems can it cause?

I'm thinking of the BNF grammar in Stroustrup's Appendix C.  Allowing
the redundant 'typename' might complicate these already complicated
rules!

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: fvali@biotrack.com
Date: 1999/05/21
Raw View
Hi,
   just thought i'd point out a few things.
In article <B35C6DB3-625F1@192.168.1.8>,
  "Gabor Greif" <gabor@no.netopia.com> wrote:
<snip>
>
> Thanks to Howard and also Siemel, who responded privately. The latter
has
> pointed out, that my original code contained a non-conformant line:
>
> >        typename Res<J, T> foo(void);
>
> Here typename is not allowed, since it is inferrable that Res<J, T> is
a
> type. The typename would be necessary to disambiguate the access to
some
> member in Res<J, T>, such as a typedef or a nested class.

The reason typename is not allowed here is because according to clause
14, typename can only be applied to qualified names.

I believe this is standard compliant:

struct Res
{
   typedef int I;

};

template<int I, typename Ty>
struct Bar
{
   template<typename Ty> // yes Ty again, but is diff from enclosing Ty
       typename Res::I foo(Ty);
};

The restriction on typename is that it must be used with a
qualified name, and it can only be used in a template declaration
or template definition; it doesn't always have to be used with
a dependent name.


>
> He has also brought to my attention that calling foo as in
>
> >   b.foo<2>();
>
> will not work, since the "<" could be parsed as a smaller operator.
>
According to the standard, b.foo<2>() will be parsed as you want it
to even without 'template ' prefixing foo.
If b were a dependent name (and in this case it clearly isn't) then
the standard requires you to prefix foo with 'template ' in order
for 'foo' to be parsed as a member template name.

> The correct version would be:
>
> >  b.template foo<2>();
>
> It seems that Howard and me are using the same compiler ;-)
>
>  Gabor
> ---


--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Gabor Greif" <gabor@no.netopia.com>
Date: 1999/05/10
Raw View
On my question , how to externally define a member template function of a
templated class

On Fri, May 7, 1999 18:43 Uhr, Howard Hinnant
<mailto:hinnant@_anti-spam_metrowerks.com> wrote:
>This ought to do it:
>
>template <int I, typename T>
>struct Res { };
>
>
>template <int I, typename T>
>struct Bar
>{
>        template <int J>
>        typename Res<J, T> foo(void);
>};
>
>template <int I, typename T>
>template <int J>
>typename Res<J, T>
>Bar<I, T>::foo(void)
>{
>   return Res<J, T>();
>}
>
>int main()
>{
>   Bar<1, int> b;
>   b.foo<2>();
>}
>
>-Howard
>


Thanks to Howard and also Siemel, who responded privately. The latter has
pointed out, that my original code contained a non-conformant line:

>        typename Res<J, T> foo(void);

Here typename is not allowed, since it is inferrable that Res<J, T> is a
type. The typename would be necessary to disambiguate the access to some
member in Res<J, T>, such as a typedef or a nested class.

He has also brought to my attention that calling foo as in

>   b.foo<2>();

will not work, since the "<" could be parsed as a smaller operator.

The correct version would be:

>  b.template foo<2>();

It seems that Howard and me are using the same compiler ;-)

 Gabor
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1999/05/10
Raw View
On 10 May 99 12:39:59 GMT, Gabor Greif <gabor@no.netopia.com> wrote:

>>        typename Res<J, T> foo(void);
>
>Here typename is not allowed, since it is inferrable that Res<J, T> is a
>type. The typename would be necessary to disambiguate the access to some
>member in Res<J, T>, such as a typedef or a nested class.

Yes.  But I also think the presence of the word 'typename' there is
harmless, so maybe the C++ grammar should be changed to allow it.  On,
the other hand, allowing a redundant 'typename' doesn't solve any big
problems and might accidentally cause other problems.

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Biju Thomas <b_thomas@ibm.net>
Date: 1999/05/10
Raw View
Siemel Naran wrote:
>
> But I also think the presence of the word 'typename' there is
> harmless, so maybe the C++ grammar should be changed to allow it.  On,
> the other hand, allowing a redundant 'typename' doesn't solve any big
> problems and might accidentally cause other problems.
>

I think that a redundant 'typename' keyword will be harmless always.
What type of accidental problems can it cause?

--
Biju Thomas
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: hinnant@_anti-spam_metrowerks.com (Howard Hinnant)
Date: 1999/05/07
Raw View
In article <B3539C4C-1E8FDD@192.168.1.8>, "Gabor Greif"
<gabor@no.netopia.com> wrote:

> { to moderator: I have posted this days ago, did not seem to have made it.
> Nothing on dejanews. Did not got a rejection either... So I repost. }
>
> Here is a question I was not able to answer myself yesterday night...
>
> Suppose having a class template with two parameters and a function member
> template in it (returning a two-parameter dependent result):
>
> template <int I, typename T>
> struct Res { };
>
>
> template <int I, typename T>
> struct Bar
> {
>         template <int J>
>         typename Res<J, T> foo(void);
> };
>
> What is the syntax for defining the function foo outside of the Bar
> template?

This ought to do it:

template <int I, typename T>
struct Res { };


template <int I, typename T>
struct Bar
{
        template <int J>
        typename Res<J, T> foo(void);
};

template <int I, typename T>
template <int J>
typename Res<J, T>
Bar<I, T>::foo(void)
{
   return Res<J, T>();
}

int main()
{
   Bar<1, int> b;
   b.foo<2>();
}

-Howard
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Gabor Greif" <gabor@no.netopia.com>
Date: 1999/05/04
Raw View
{ to moderator: I have posted this days ago, did not seem to have made it.
Nothing on dejanews. Did not got a rejection either... So I repost. }

Here is a question I was not able to answer myself yesterday night...

Suppose having a class template with two parameters and a function member
template in it (returning a two-parameter dependent result):

template <int I, typename T>
struct Res { };


template <int I, typename T>
struct Bar
{
 template <int J>
 typename Res<J, T> foo(void);
};

What is the syntax for defining the function foo outside of the Bar
template?

My attempt was

template <int I, int J, typename T>
typename Res<J, T> Bar<I, T>::foo<J>(void)
{
 return Res<J, T>();
}


But my compiler rejected it.

Another tricky question:
Assuming I want to define foo for a special case that I == J. Is this
syntax correct?

template <int I, typename T>
typename Res<I, T> Bar<I, T>::foo<I>(void)
{
 return Res<I, T>();
}

But this also busts my compiler.

Can the standards gurus help me in this one?

Thanks in advance...

 Gabor
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]