Topic: Specialization of a template cast.
Author: llewelly.at@xmission.dot.com (Llewelly)
Date: Wed, 27 Apr 2005 23:22:33 GMT Raw View
AlbertoBarbati@libero.it (Alberto Barbati) writes:
> Llewelly wrote:
> > AlbertoBarbati@libero.it (Alberto Barbati) writes:
> >
> >
> >>Lucas Galfaso wrote:
> >>
> >>>Hi,
> >>> From the standard, it is not clear how you should (a) create a
> >>>specialization from a template cast and (b) use a specific
> >>>specialization on a cast, ie
> >>>
> >>
> >>There's no such thing as a template cast in C++. Please always use the
> >>correct terms if you want to be understood. In this case the correct
> >>term is "template conversion function".
> >
> > [snip]
> >
> > Nit upon nit: The standard uses 'conversion function
> > template'. e.g. 14.8.2.3 .
> >
>
> Yes, that would be a more correct expression. I was deceived by 14.5.2/5
> where the wording "template conversion function" is actually used,
> probably a mistake that escaped the "template function" vs. "function
> template" war.
hm. On second look I see your term ('template conversion function')
all over the place. It's even in the first paragraph of 14.8.2.3 !
13.3.3.1.2/3
14.5.2/[5678]
14.5.5.2/3
14.8.2.3/1
however my term ('conversion function template') is used *only* in the
title of 14.8.2.3
I guess that's what I deserve for nit picking.
It would be nice if the same phrase could be used throughout.
---
[ 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: "Felipe Magno de Almeida" <felipe.m.almeida@gmail.com>
Date: Wed, 4 May 2005 17:46:48 CST Raw View
Lucas Galfaso escreveu:
<snip>
> template <typename _Ty>
Dont use an underscore before a capital letter, it is reserved for the
compiler.
> operator _Ty() const { cout << "This is a specialization on the
type
> " << typeid(_Ty).name() << endl; return (_Ty) m_foo; }
>
> Thanks,
> Lucas Galfaso
---
[ 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: lgalfaso@gmail.com (Lucas Galfaso)
Date: Mon, 25 Apr 2005 01:25:20 GMT Raw View
Hi,
From the standard, it is not clear how you should (a) create a
specialization from a template cast and (b) use a specific
specialization on a cast, ie
(A)
#include <iostream>
using namespace std;
class Foo {
int m_foo;
public:
Foo () : m_foo(0) {}
template <typename _Ty>
operator _Ty() const { cout << "This is a specialization on the type
" << typeid(_Ty).name() << endl; return (_Ty) m_foo; }
// How you write a specialization of the template cast above?
// template <> operator<int> int() const {return m_foo;} ?
// template <> operator int<int>() const {return m_foo;}?
// template <> operator int()<int> const {return m_foo;}?
};
(B)
#include <iostream>
using namespace std;
class Foo {
int m_foo;
public:
Foo () : m_foo(0) {}
template <typename _Ty>
operator _Ty() const { cout << "This is a specialization on the type
" << typeid(_Ty).name() << endl; return (_Ty) m_foo; }
operator int() const { cout << "This is a standard cast to int" <<
endl; return m_foo; }
};
int main() {
Foo foo;
cout << (int)foo; // this will call the non-template version of the
cast, how you call the template version for the type int?
return 0;
}
Thanks,
Lucas Galfaso
---
[ 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: llewelly.at@xmission.dot.com (Llewelly)
Date: Mon, 25 Apr 2005 15:19:15 GMT Raw View
lgalfaso@gmail.com (Lucas Galfaso) writes:
> Hi,
> From the standard, it is not clear how you should (a) create a
> specialization from a template cast and (b) use a specific
> specialization on a cast, ie
>
>
> (A)
>
> #include <iostream>
> using namespace std;
>
> class Foo {
> int m_foo;
> public:
> Foo () : m_foo(0) {}
>
> template <typename _Ty>
> operator _Ty() const { cout << "This is a specialization on the type
> " << typeid(_Ty).name() << endl; return (_Ty) m_foo; }
>
> // How you write a specialization of the template cast above?
> // template <> operator<int> int() const {return m_foo;} ?
> // template <> operator int<int>() const {return m_foo;}?
> // template <> operator int()<int> const {return m_foo;}?
> };
[snip]
You need:
template <> Foo::operator int() const {return m_foo;}
outside of the class decl.
Maybe 14.5.2/5 is helpful:
# A specialization of a template conversion function is referenced
# in the same way as a non-template conversion function that
# converts to the same type. [Example:
#
# struct A {
# template <class T> operator T*();
# };
# template <class T> A::operator T*(){ return 0; }
# template <> A::operator char*(){ return 0; } // specialization
# template A::operator void*(); // explicit instantiation
#
# int main()
# {
# A a;
# int* ip;
# ip = a.operator int*(); // explicit call to template operator
# // A::operator int*()
# }
#
# --end example] [Note: because the explicit template argument
# list follows the function template name, and because conversion
# member function templates and constructor member function
# templates are called without using a function name, there is no
# way to provide an explicit template argument list for these
# function templates. ]
---
[ 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: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Tue, 26 Apr 2005 05:21:14 GMT Raw View
Lucas Galfaso wrote:
> Hi,
> From the standard, it is not clear how you should (a) create a
> specialization from a template cast and (b) use a specific
> specialization on a cast, ie
>=20
There's no such thing as a template cast in C++. Please always use the
correct terms if you want to be understood. In this case the correct
term is "template conversion function".
>=20
> (A)
>=20
> #include <iostream>
> using namespace std;
>=20
> class Foo {
> int m_foo;
> public:
> Foo () : m_foo(0) {}
>=20
> template <typename _Ty>
> operator _Ty() const { cout << "This is a specialization on the type
> " << typeid(_Ty).name() << endl; return (_Ty) m_foo; }
>=20
> // How you write a specialization of the template cast above?
> // template <> operator<int> int() const {return m_foo;} ?
> // template <> operator int<int>() const {return m_foo;}?
> // template <> operator int()<int> const {return m_foo;}?
> };
>=20
You must have missed 14.5.2/5: "A specialization of a template
conversion function is referenced in the same way as a non-template
conversion function that converts to the same type. [Example:
struct A {
template <class T> operator T*();
};
template <class T> A::operator T*(){ return 0; }
template <> A::operator char*(){ return 0; } // specialization
template A::operator void*(); // explicit instantiation
int main()
{
A a;
int* ip;
ip =3D a.operator int*(); // explicit call to template operator
// A::operator int*()
}
=97end example] [Note: because the explicit template argument list follow=
s
the function template name, and because conversion member function
templates and constructor member function templates are called without
using a function name, there is no way to provide an explicit template
argument list for these function templates. ]
> (B)
> #include <iostream>
> using namespace std;
>=20
> class Foo {
> int m_foo;
> public:
> Foo () : m_foo(0) {}
>=20
> template <typename _Ty>
> operator _Ty() const { cout << "This is a specialization on the type
> " << typeid(_Ty).name() << endl; return (_Ty) m_foo; }
> operator int() const { cout << "This is a standard cast to int" <<
> endl; return m_foo; }
> };
>=20
>=20
> int main() {
> Foo foo;
> cout << (int)foo; // this will call the non-template version of the
> cast, how you call the template version for the type int?
>=20
> return 0;
> }
>=20
According to 14.5.2/5, there's no way to call of the template version.
The non-template version is always called as a result of the general
rule for which a non-template function that provides a perfect match is
always preferred to a matching template instantiation.
Alberto
---
[ 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: llewelly.at@xmission.dot.com (Llewelly)
Date: Wed, 27 Apr 2005 06:37:49 GMT Raw View
AlbertoBarbati@libero.it (Alberto Barbati) writes:
> Lucas Galfaso wrote:
> > Hi,
> > From the standard, it is not clear how you should (a) create a
> > specialization from a template cast and (b) use a specific
> > specialization on a cast, ie
> >
>
> There's no such thing as a template cast in C++. Please always use the
> correct terms if you want to be understood. In this case the correct
> term is "template conversion function".
[snip]
Nit upon nit: The standard uses 'conversion function
template'. e.g. 14.8.2.3 .
---
[ 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: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Wed, 27 Apr 2005 18:31:15 GMT Raw View
Llewelly wrote:
> AlbertoBarbati@libero.it (Alberto Barbati) writes:
>
>
>>Lucas Galfaso wrote:
>>
>>>Hi,
>>> From the standard, it is not clear how you should (a) create a
>>>specialization from a template cast and (b) use a specific
>>>specialization on a cast, ie
>>>
>>
>>There's no such thing as a template cast in C++. Please always use the
>>correct terms if you want to be understood. In this case the correct
>>term is "template conversion function".
>
> [snip]
>
> Nit upon nit: The standard uses 'conversion function
> template'. e.g. 14.8.2.3 .
>
Yes, that would be a more correct expression. I was deceived by 14.5.2/5
where the wording "template conversion function" is actually used,
probably a mistake that escaped the "template function" vs. "function
template" war.
Alberto
---
[ 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 ]