 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Maciej Sobczak Guest
|
Posted: Thu May 19, 2005 8:34 pm Post subject: n1785 - Object Templates concerns |
|
|
Hello,
After reading the n1785 (Toward a Proposal for Object Templates in
C++0x) I have some feedback that I would like to share.
The authors of this proposal argue that it would be good to have the
possibility to use objects with static storage duration at namespace
scope that would be "templates" in the sense that we know from classes
and functions.
The example is:
template< class T = double>
T const pi = 3.1415926L;
with possible specializations (for different types), etc.
The idea is to use it in generic code (for example, from the template
function that computes the area of circle), so that it always resolves
to the type that is needed:
template <typename T>
T area_of_circle(T radius)
{
return static_cast<T>(pi) * radius * radius;
}
My feedback is:
I think that the above has no advantage over what we already can do with
the same effect on the "client" side:
struct PiType
{
template <typename T>
operator T() const { return 3.1415926536; }
} pi;
The object "pi" above can be used in generic code (like the
area_of_circle function). Specializations for different types or even
distinguishing reads from writes are possible as well.
Regards,
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
---
[ 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 |
|
 |
martinfrompi@yahoo.co.uk Guest
|
Posted: Tue May 24, 2005 9:28 pm Post subject: Re: n1785 - Object Templates concerns |
|
|
Maciej Sobczak wrote:
| Quote: | struct PiType
{
template
operator T() const { return 3.1415926536; }
} pi;
The object "pi" above can be used in generic code (like the
area_of_circle function). Specializations for different types or even
distinguishing reads from writes are possible as well.
|
The problem is that if *you* (or boost or the standard committee) write
PiType, and *I* write MartinsSuperHighPrecisionType, there is no way
for *me* to add operator MartinsSuperHighPrecisionType() const to
PiType.
---
[ 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 |
|
 |
Nicola Musatti Guest
|
Posted: Wed May 25, 2005 3:50 pm Post subject: Re: n1785 - Object Templates concerns |
|
|
[email]martinfrompi (AT) yahoo (DOT) co.uk[/email] wrote:
| Quote: | Maciej Sobczak wrote:
struct PiType
{
template <typename T
operator T() const { return 3.1415926536; }
} pi;
The object "pi" above can be used in generic code (like the
area_of_circle function). Specializations for different types or even
distinguishing reads from writes are possible as well.
The problem is that if *you* (or boost or the standard committee) write
PiType, and *I* write MartinsSuperHighPrecisionType, there is no way
for *me* to add operator MartinsSuperHighPrecisionType() const to
PiType.
|
No problem. Just write
template <> PiType::operator MartinsSuperHighPrecisionType() const {
return MartinsSuperHighPrecisionType(3.2);
}
Cheers,
Nicola Musatti
---
[ 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
|
|