Boost logo

Boost :

Subject: Re: [boost] A design for geometric objects
From: Stjepan Rajko (stjepan.rajko_at_[hidden])
Date: 2008-11-14 17:10:21


On Fri, Nov 14, 2008 at 2:18 PM, Bruno Lalande <bruno.lalande_at_[hidden]> wrote:
>
> The "specialized" typedef is a bit weird, as it forces the user to
> sort of say "yes, I've really specialized it". But the goal is reached
> I think: I have never explicitly specialized ellipse_traits<circle>. I
> have only said that if circle_traits<X> exists then ellipse_traits<X>
> exists and ellipse_traits<X>::width() is circle_traits<X>::radius(),
> except if a better specialization of ellipse_traits<X> exists. And the
> compiler correctly understood what I meant when I asked for
> ellipse_traits<circle>::width().
>
> Does anybody know a way to know if a specialization exists without
> using this kind of trick?
>

I've done something similar, where the default template implementation
has a member typedef of a particular type `not_specialized`. You can
see this at:

http://tinyurl.com/5dba48

(look at port_binary_operation_impl and are_port_binary_operable)

In this case, the member typedef used is result_type but it can be
anything. The mechanism basically checks whether a particular
template instantiation has a result_type that is_same as
not_specialized. If it has no result_type, or the result_type is
something different, it knows that the template has been specialized.

The method is not perfect as you can fool it into thinking that the
template has not been specialized for certain arguments by setting the
typedef used to not_specialized. I don't know whether there is a way
to prevent that (perhaps make `not_specialized` a private nested
struct of something?)

Best,

Stjepan


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk