Topic: [Q]: Template parameters


Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/04/24
Raw View
fjh@mundook.cs.mu.OZ.AU (Fergus Henderson) writes:

>John Hancock <jhancock+@IUS5.IUS.cs.cmu.edu> writes:
>
>>A second question deals with member template function specialization.
>>What's the proper syntax for specializing a member template function:
>>
>>class Foo{
>>  template <class T> static char code(T );
>>};
>>
>>I tried a bunch of things, including:
>>char Foo::code<float>(float){return 'f';}
>
>I think that is the right syntax.

That was wrong; John Lilley's answer was correct.
Use either

 class Foo {
   template <class T> static char code(T );
   template <> static char code(float) { .... }
 };

or

 class Foo {
   template <class T> static char code(T );
 };
 template <> char Foo::code(float) { .... }

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: John Hancock <jhancock+@IUS5.IUS.cs.cmu.edu>
Date: 1997/04/18
Raw View
I've been trying out some things with member templates
with the newest(?) version of the SGI C++ compiler (v 7.1).
under IRIX 6.2.

I'm wondering whether the following member template
declaration is legal or not:

class Foo{
template <class T> static T bar(char *filename);
};

I get the following error:
template parameter "T" is not used in declaring the parameter
types of function template "Foo::bar"

I was under the impression that it was legal to parameterize
a template function on the return type. In the Draft standard, I
spotted an example function declaration that looked like:

template <class X, class Y, class Z> X f(Y, Z);

Although this isn't a member template, it is parameterizing on the
return type. So is my code above legal, or is it a compiler bug?

-----

A second question deals with member template function specialization.
What's the proper syntax for specializing a member template function:

class Foo{
  template <class T> static char code(T );
};

I tried a bunch of things, including:
char Foo::code<float>(float){return 'f';}

but the compiler didn't seem to like this.

Anyone know how this is supposed to be done?


Thanks much,

John
--
--------------------------------------------------------------------------
     John A. Hancock, Robotics Institute, Carnegie Mellon University
        jhancock@ri.cmu.edu, http://www.ius.cs.cmu.edu/~jhancock/
"Life is short, but long enough to get what's coming to you." - John Alton
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/04/19
Raw View
John Hancock <jhancock+@IUS5.IUS.cs.cmu.edu> writes:

>I've been trying out some things with member templates
>with the newest(?) version of the SGI C++ compiler (v 7.1).
>under IRIX 6.2.
>
>I'm wondering whether the following member template
>declaration is legal or not:
>
>class Foo{
>template <class T> static T bar(char *filename);
>};

Yes, unless I am severely mistaken, it's legal.

>I get the following error:
>template parameter "T" is not used in declaring the parameter
>types of function template "Foo::bar"

Looks like your compiler doesn't implement explicit template
qualifcations.  You should be able to invoke the above
function as

 Foo::bar<int>("whatever");

>I was under the impression that it was legal to parameterize
>a template function on the return type.

Yep.

>-----
>
>A second question deals with member template function specialization.
>What's the proper syntax for specializing a member template function:
>
>class Foo{
>  template <class T> static char code(T );
>};
>
>I tried a bunch of things, including:
>char Foo::code<float>(float){return 'f';}

I think that is the right syntax.

>but the compiler didn't seem to like this.

Sounds like a compiler bug (or a not-yet-implemented feature).

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Matt Austern <austern@isolde.mti.sgi.com>
Date: 1997/04/20
Raw View

John Hancock <jhancock+@IUS5.IUS.cs.cmu.edu> writes:

>
> I've been trying out some things with member templates
> with the newest(?) version of the SGI C++ compiler (v 7.1).
> under IRIX 6.2.
>
> I'm wondering whether the following member template
> declaration is legal or not:
>
> class Foo{
> template <class T> static T bar(char *filename);
> };

Yes, it is.  Unfortunately, it relies on a feature that the SGI
compiler hasn't yet implemented.

This actually has nothing to do with member templates; an example that
illustrates this point just as well is
    template <class T> void f() {}.

This is perfectly legal.  However, ordinary template argument
deduction can't be used with a function like f().  You call it
by writing something like f<int>().

This feature is called "explicit template argument specification".  It
is part of the language, but it is a relatively recent addition and
the 7.1 compiler does not support it.  (For that matter, I don't know
of any compilers that support this feature yet.)
---
[ 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: John Lilley <jlilley@empathy.com>
Date: 1997/04/20
Raw View

John Hancock wrote:

> A second question deals with member template function specialization.
> What's the proper syntax for specializing a member template function:
>
> class Foo{
>   template <class T> static char code(T );
> };
>
>I tried a bunch of things, including:
>char Foo::code<float>(float){return 'f';}

The CD2 draft says you use template<> and just specify the function with
all types filled in:

  template<> static char code(char) { ... }

john lilley
---
[ 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: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/04/20
Raw View
John Lilley <jlilley@empathy.com> writes:

>John Hancock wrote:
>
>> A second question deals with member template function specialization.
>> What's the proper syntax for specializing a member template function:
>>
>> class Foo{
>>   template <class T> static char code(T );
>> };
>>
>>I tried a bunch of things, including:
>>char Foo::code<float>(float){return 'f';}
>
>The CD2 draft says you use template<> and just specify the function with
>all types filled in:
>
>  template<> static char code(char) { ... }

I think you will need `Foo::' somewhere in there:

   template<> static char Foo::code(char) { ... }

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]