Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 88997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:38:51+00:00 2026-05-10T22:38:51+00:00

I have a vector-like class that contains an array of objects of type T

  • 0

I have a vector-like class that contains an array of objects of type 'T', and I want to implement 4 arithmetic operators, which will apply the operation on each item:

// 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]; }; 

Because operators will contain the same boilerplate code (looping over every element and applying appropriate 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 do I pass an operator that takes two intrinsic types and returns void to do_for_each, as depicted in the example above? C++ does not let me do this trick for intrinsic types ('T::operator+=' will not work if 'T' is 'int').

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:38:51+00:00Added an answer on May 10, 2026 at 10:38 pm

    First, you should really return a reference from your operator+=, since you can later use them to implement operator+, operator- and so on. I will change that accordingly.

    Also, your do_for_each has to be a template, since it has to know the precise type of the function object, as binary function objects are not polymorph classes. For the actual operation, you want to use 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 will just pass each element to the function object, and assigns the result back to the iterator given as the third argument.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.