C ++: using the operator of two internal types as a function object

I have a vector class that contains an array of type objects "T", and I want to implement 4 arithmetic operators that will apply the operation to each element:

// Constructors and other functions are omitted for brevity.
template<class T, unsigned int D>
class Vector {

public:
    // Add a value to each item: naive implementation.
    void operator += (const T&) {
        for (int i = 0; i < D; ++i) {
            data[i] += value;
        }
    }
    void operator -= (const T&) { ... }
    void operator *= (const T&) { ... }
    void operator /= (const T&) { ... }

private:
    T items[D];
};

Since the operators will contain the same template code (a cycle for each element and the application of the corresponding operation), I thought I could generalize it:

template<class T, unsigned int D>
class Vector {

public:
    void operator += (const T& value) { do_for_each(???, value); }
    void operator -= (const T& value) { do_for_each(???, value); }
    void operator *= (const T& value) { do_for_each(???, value); }
    void operator /= (const T& value) { do_for_each(???, value); }

private:
    void
    do_for_each(std::binary_function<void, T, T>& op, T value) {
        std::for_each(data, data + D, std::bind2nd(op, value));
    }

    T data[D];
};

Now the problem is how to pass an operator that takes two internal types and returns voidin do_for_each, as shown in the example above? C ++ does not allow me to do this trick for built-in types ( "T::operator+="will not work if "T"there is one "int").

+5
3

-, + =, +, .. .

, do_for_each , , . std::transform:

template<class T, unsigned int D>
class Vector {

public:
    Vector& operator += (const T& value) { 
        do_for_each(std::plus<T>(), value); 
        return *this;
    }

    Vector& operator -= (const T& value) { 
        do_for_each(std::minus<T>(), value); 
        return *this;
    }

    Vector& operator *= (const T& value) { 
        do_for_each(std::multiplies<T>(), value);
        return *this; 
    }

    Vector& operator /= (const T& value) { 
        do_for_each(std::divides<T>(), value); 
        return *this;
    }

private:
    template<typename BinFun>
    void do_for_each(BinFun op, const T& value) {
        std::transform(data, data + D, data, std::bind2nd(op, value));
    }

    T data[D];
};

std:: transform , .

+8

Boost Operators - , .

: , boost::operators::integer_arithmatic<T> .

+2

I think the light bulb is on the right track and answers the exact question.
But I think this is the wrong decision.

I would rather not use do_for_each (), but rather use std :: transform ():

template<class T, unsigned int D>
class Vector 
{

  public:
    Vector& operator += (const T& value) { 
        std::transform(data, data + D, data, std::bind2nd(std::plus<T>(),value)); 
        return *this;
    }

    Vector& operator -= (const T& value) { 
        std::transform(data, data + D, data, std::bind2nd(std::minus<T>(),value)); 
        return *this;
    }

    Vector& operator *= (const T& value) { 
        std::transform(data, data + D, data, std::bind2nd(std::multiplies<T>(),value));
        return *this; 
    }

    Vector& operator /= (const T& value) { 
        std::transform(data, data + D, data, std::bind2nd(std::divides<T>(),value)); 
        return *this;
    }

  private:
    T data[D];
};
+1
source

All Articles