Topic: n1785 - Object Templates concerns


Author: martinfrompi@yahoo.co.uk
Date: Tue, 24 May 2005 15:28:52 CST
Raw View
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.

---
[ 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: "Nicola Musatti" <nicola.musatti@gmail.com>
Date: 25 May 2005 15:50:09 GMT
Raw View

martinfrompi@yahoo.co.uk wrote:
> 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                       ]





Author: no.spam@no.spam.com (Maciej Sobczak)
Date: Thu, 19 May 2005 20:34:21 GMT
Raw View
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                       ]