Topic: Member Template Function?


Author: Jaakko =?iso-8859-1?Q?J=E4rvi?= <jaakko.jarvi@cs.utu.fi>
Date: 1999/06/07
Raw View
wmm@fastdial.net wrote:
>=20
> No.  You only need the "template" keyword before member template
> specializations inside a template definition and only when the
> postfix-expression is dependent on a template parameter (14.2p4).
> Here neither of those conditions applies.

Thanks, for the correction. I was fooled by egcs,
which seems to require the "template" keyword even if there is no=20
dependency on a template parameter:

class A {
public:
    template<class T> T f() { return T(); }
};

int main()
{
  int i =3D A().template f<int>(); // ok
   int j =3D A().f<int>(); // Egcs:.1.2 : parse error before >=20
}

/Jaakko

--=20
--- Jaakko J=E4rvi, jaakko.jarvi@cs.utu.fi
--- Turku Centre for Computer Science (www.tucs.fi)
--- Lemmink=E4isenkatu 14 A, FIN-20520 Turku, Finland
--- Phone: +358-2-333 8656, Fax: +358-2-333 8600
---
[ 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: Gabriel Dos_Reis <gdosreis@korrigan.inria.fr>
Date: 1999/06/07
Raw View
Jaakko J=E4rvi <jaakko.jarvi@cs.utu.fi> writes:

| wmm@fastdial.net wrote:
| >=3D20
| > No.  You only need the "template" keyword before member template
| > specializations inside a template definition and only when the
| > postfix-expression is dependent on a template parameter (14.2p4).
| > Here neither of those conditions applies.
|=20
| Thanks, for the correction. I was fooled by egcs,
| which seems to require the "template" keyword even if there is no=3D20
| dependency on a template parameter:

That is a known, frequently reported, and documented problem in the
parser. See EGCS' web pages for more information.

--=20
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr
---
[ 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: wmm@fastdial.net
Date: 1999/06/03
Raw View
In article <37560C87.20B535C1@cs.utu.fi>,
  Jaakko =?iso-8859-1?Q?J=E4rvi?= <jaakko.jarvi@cs.utu.fi> wrote:
> > In article <3755223E.5C831F95@nwg.nectec.or.th>,
> >   theppitk@nwg.nectec.or.th wrote:
> > > I wonder if it is possible to declare a member template function
in
> > > standard C++.
>
> Also:
> > >     int i = m.get<int>();
> should be
>         int i = m.template get<int>();
> because get is a member template with an explicitly specialised
> argument.

No.  You only need the "template" keyword before member template
specializations inside a template definition and only when the
postfix-expression is dependent on a template parameter (14.2p4).
Here neither of those conditions applies.

The reason for using the "template" keyword is that the parsing
is affected by whether a name is a template-name or not -- after
a template-name, "<" is the start of the template argument list,
otherwise it's a "less-than" operator.  In a dependent qualified-id
or postfix-expression, you can't do the semantic analysis to
determine which way to parse "<", so the "template" keyword is
needed for the parser to decide.  In non-dependent contexts you
always know whether a name is a template-name or not.
--
William M. Miller, wmm@fastdial.net
Software Emancipation Technology (www.setech.com)


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: comeau@panix.com (Greg Comeau)
Date: 1999/06/04
Raw View
In article <3755223E.5C831F95@nwg.nectec.or.th> theppitk@nwg.nectec.or.th writes:
>I wonder if it is possible to declare a member template function in
>standard C++....

Other messages have addressed that this is possible.  I would just like
to add onto this that folks should consider getting some (new) _quality_
texts, as well as the Standard itself, so that the uses and subtleties of
member templates (and other features) can be put in the right light.

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
     Producers of Comeau C/C++ 4.2.38 -- New Release!  We now do Windows too.
    Email: comeau@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
                *** WEB: http://www.comeaucomputing.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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Theppitak Karoonboonyanan <theppitk@nwg.nectec.or.th>
Date: 1999/06/02
Raw View
I wonder if it is possible to declare a member template function in
standard C++.
For example:

class MultiInt {
public:
    MultiInt(unsigned long l) : l(l) {}
    template<class T> T get() const { return reinterpret_cast<T&>(l); }

private:
    unsigned long l;
};

int main()
{
    MultiInt  m(12345);
    int i = m.get<int>();
}

Thank you,
Theppitak.
---
[ 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: wmm@fastdial.net
Date: 1999/06/02
Raw View
In article <3755223E.5C831F95@nwg.nectec.or.th>,
  theppitk@nwg.nectec.or.th wrote:
> I wonder if it is possible to declare a member template function in
> standard C++.
> For example:
>
> class MultiInt {
> public:
>     MultiInt(unsigned long l) : l(l) {}
>     template<class T> T get() const { return reinterpret_cast<T&>(l);
}
>
> private:
>     unsigned long l;
> };
>
> int main()
> {
>     MultiInt  m(12345);
>     int i = m.get<int>();
> }

Yes, that's fine (except for the dropping of the "const" qualifier
in the reinterpret_cast -- it ought to be

 reinterpret_cast<const T&>(l)

--
William M. Miller, wmm@fastdial.net
Software Emancipation Technology (www.setech.com)


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: Jaakko =?iso-8859-1?Q?J=E4rvi?= <jaakko.jarvi@cs.utu.fi>
Date: 1999/06/03
Raw View
> In article <3755223E.5C831F95@nwg.nectec.or.th>,
>   theppitk@nwg.nectec.or.th wrote:
> > I wonder if it is possible to declare a member template function in
> > standard C++.

Also:
> >     int i =3D m.get<int>();
should be
        int i =3D m.template get<int>();
because get is a member template with an explicitly specialised
argument.

/Jaakko

--=20
--- Jaakko J=E4rvi, jaakko.jarvi@cs.utu.fi
--- Turku Centre for Computer Science (www.tucs.fi)
--- Lemmink=E4isenkatu 14 A, FIN-20520 Turku, Finland
--- Phone: +358-2-333 8656, Fax: +358-2-333 8600
---
[ 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              ]