 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Lucas Galfaso Guest
|
Posted: Mon Apr 25, 2005 1:25 am Post subject: Specialization of a template cast. |
|
|
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 ]
|
|
| Back to top |
|
 |
Llewelly Guest
|
Posted: Mon Apr 25, 2005 3:19 pm Post subject: Re: Specialization of a template cast. |
|
|
[email]lgalfaso (AT) gmail (DOT) com[/email] (Lucas Galfaso) writes:
| Quote: | 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
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 ]
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Tue Apr 26, 2005 5:21 am Post subject: Re: Specialization of a template cast. |
|
|
Lucas Galfaso wrote:
| Quote: | 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".
| Quote: |
(A)
#include <iostream
using namespace std;
class Foo {
int m_foo;
public:
Foo () : m_foo(0) {}
template
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;}?
};
|
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 = 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. ]
| Quote: | (B)
#include
using namespace std;
class Foo {
int m_foo;
public:
Foo () : m_foo(0) {}
template
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;
}
|
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 ]
|
|
| Back to top |
|
 |
Llewelly Guest
|
Posted: Wed Apr 27, 2005 6:37 am Post subject: Re: Specialization of a template cast. |
|
|
[email]AlbertoBarbati (AT) libero (DOT) it[/email] (Alberto Barbati) writes:
| Quote: | 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 ]
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Wed Apr 27, 2005 6:31 pm Post subject: Re: Specialization of a template cast. |
|
|
Llewelly wrote:
| Quote: | AlbertoBarbati (AT) libero (DOT) 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 ]
|
|
| Back to top |
|
 |
Llewelly Guest
|
Posted: Wed Apr 27, 2005 11:22 pm Post subject: Re: Specialization of a template cast. |
|
|
[email]AlbertoBarbati (AT) libero (DOT) it[/email] (Alberto Barbati) writes:
| Quote: | Llewelly wrote:
[email]AlbertoBarbati (AT) libero (DOT) it[/email] (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 ]
|
|
| Back to top |
|
 |
Felipe Magno de Almeida Guest
|
Posted: Wed May 04, 2005 11:46 pm Post subject: Re: Specialization of a template cast. |
|
|
Lucas Galfaso escreveu:
<snip>
Dont use an underscore before a capital letter, it is reserved for the
compiler.
| Quote: | 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 ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|