Title
bind1st/bind2nd type safety
Status
cd1
Section
[depr.lib.binders]
Submitter
Andrew Demkin

Created on 2002-04-26.00:00:00 last changed 164 months ago

Messages

Date: 2010-10-21.18:28:33

Rationale:

There is no point in fixing bind1st and bind2nd. tr1::bind is a superior solution. It solves this problem and others.

Date: 2010-10-21.18:28:33

Proposed resolution:

Add this sentence to the end of [depr.lib.binders]/1: "Binders bind1st and bind2nd are deprecated in favor of std::tr1::bind."

(Notes to editor: (1) when and if tr1::bind is incorporated into the standard, "std::tr1::bind" should be changed to "std::bind". (2) 20.5.6 should probably be moved to Annex D.

Date: 2002-04-26.00:00:00

The definition of bind1st() ([depr.lib.binders]) can result in the construction of an unsafe binding between incompatible pointer types. For example, given a function whose first parameter type is 'pointer to T', it's possible without error to bind an argument of type 'pointer to U' when U does not derive from T:

   foo(T*, int);

   struct T {};
   struct U {};

   U u;

   int* p;
   int* q;

   for_each(p, q, bind1st(ptr_fun(foo), &u));    // unsafe binding

The definition of bind1st() includes a functional-style conversion to map its argument to the expected argument type of the bound function (see below):

  typename Operation::first_argument_type(x)

A functional-style conversion ([depr.lib.binders]) is defined to be semantically equivalent to an explicit cast expression ([depr.lib.binders]), which may (according to 5.4, paragraph 5) be interpreted as a reinterpret_cast, thus masking the error.

The problem and proposed change also apply to [depr.lib.binders].

History
Date User Action Args
2010-10-21 18:28:33adminsetmessages: + msg2359
2010-10-21 18:28:33adminsetmessages: + msg2358
2002-04-26 00:00:00admincreate