660. Missing Bitwise Operations

Section: 23.14 [function.objects] Status: CD1 Submitter: Beman Dawes Opened: 2007-04-02 Last modified: 2016-02-10

Priority: Not Prioritized

View all other issues in [function.objects].

View all issues with CD1 status.

Discussion:

Section 23.14 [function.objects] provides function objects for some unary and binary operations, but others are missing. In a LWG reflector discussion, beginning with c++std-lib-18078, pros and cons of adding some of the missing operations were discussed. Bjarne Stroustrup commented "Why standardize what isn't used? Yes, I see the chicken and egg problems here, but it would be nice to see a couple of genuine uses before making additions."

A number of libraries, including Rogue Wave, GNU, Adobe ASL, and Boost, have already added these functions, either publicly or for internal use. For example, Doug Gregor commented: "Boost will also add ... (|, &, ^) in 1.35.0, because we need those function objects to represent various parallel collective operations (reductions, prefix reductions, etc.) in the new Message Passing Interface (MPI) library."

Because the bitwise operators have the strongest use cases, the proposed resolution is limited to them.

Proposed resolution:

To 23.14 [function.objects], Function objects, paragraph 2, add to the header <functional> synopsis:

template <class T> struct bit_and;
template <class T> struct bit_or;
template <class T> struct bit_xor;

At a location in clause 20 to be determined by the Project Editor, add:

The library provides basic function object classes for all of the bitwise operators in the language ([expr.bit.and], [expr.or], [exp.xor]).

template <class T> struct bit_and : binary_function<T,T,T> {
  T operator()(const T& x , const T& y ) const;
};

operator() returns x & y .

template <class T> struct bit_or : binary_function<T,T,T> {
  T operator()(const T& x , const T& y ) const;
};

operator() returns x | y .

template <class T> struct bit_xor : binary_function<T,T,T> {
  T operator()(const T& x , const T& y ) const;
};

operator() returns x ^ y .