|
Boost : |
Subject: Re: [boost] An alternative approach to TypeErasure
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2012-06-24 18:42:55
AMDG
On 06/24/2012 03:34 AM, Pyry Jahkola wrote:
>
> Here's a somewhat shorter intro:
>
> 1) Create a callable F by simply deriving it from poly::callable<F>:
>
> struct draw_ : poly::callable<draw_> {};
> constexpr draw_ draw = {};
>
> 2) Or use the convenience macro POLY_CALLABLE(name) to do the same:
>
> POLY_CALLABLE(to_string); // type `to_string_` and object `to_string`
>
> 3) Overload your callables by overloading the function named `call`,
> with the callable as first argument:
>
> template <typename T>
> void call(draw_, T const & x, std::ostream & out, std::size_t
> position) {
> out << std::string(position, ' ') << x << std::endl;
> }
>
Some thoughts on this interface:
a) It's a bit more concise than my interface,
but the difference isn't major.
b) ADL is less robust than class template
specialization, since it can accidentally
pull in unrelated functions that happen
to have the same name.
c) Your function objects won't overload
properly with ordinary functions, if,
for example, someone has a function called
draw or to_string that operates on his own
type.
In Christ,
Steven Watanabe
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk