Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2005-08-12 15:56:13


Christopher Kohlhoff wrote:
> Hi all,
>
> As part of the asio library I have been using named placeholders for
> the common arguments to callback functions. For example:
>
> asio::async_send_n(sock, buffer, length,
> boost::bind(mycallback, asio::arg::error,
> asio::arg::last_bytes_sent, asio::arg::total_bytes_sent));
>
> rather than:
>
> asio::async_send_n(sock, buffer, length,
> boost::bind(mycallback, _1, _2, _3));
>
> I find having named placeholders saves time and helps prevent errors
> to
> do with the ordering of the parameters (e.g. in the case of something
> like the last_bytes_sent/total_bytes_sent example where both are
> size_t, and so incorrect order will not be detected by the compiler).
>
> Unfortunately, from my reading of the TR1 bind doco at
>
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1455.htm
>
> it simply defines the placeholders as:
>
> namespace std {
> namespace placeholders {
> extern unspecified _1;
> extern unspecified _2;
> extern unspecified _3;
> // implementation defined number of additional placeholders
> }
> }
>
> and it doesn't say anything about the type of the placeholders. Since
> the placeholders are CopyConstructible, it seems like I ought to be
> able to do something using a typedef (if there was one), e.g:
>
> std::placeholders::_1_t my_placeholder(std::placeholders::_1);
>
> Is there some TR1-compatible (and therefore portable) way of working
> around it that's eluding me (other than #define :).

We decided to not expose the types of the default _N placeholders in TR1,
but you can add your own placeholder types by specializing
std::tr1::is_placeholder.


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