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 6961081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:29:06+00:00 2026-05-27T15:29:06+00:00

Lets say I have vectors of different objects, say D is of type vector<

  • 0

Lets say I have vectors of different objects, say D is of type vector< Dog > and R is of type vector< Rock >. Is it possible to use templates so I can automatically have all member functions be callable on the vectors? I.e. if Dog has a function Bark(), I want D.bark() to cause all the dogs to bark.

Now, clearly I could code this behavior by hand with a new function and a simple loop, but is
there an easy way to template it? I.e. so that R.smash() smashes all the rocks without me
ever explicitly coding that loop. I’m trying to imagine what the syntax would be and it is blowing my mind.

  • 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. Editorial Team
    Editorial Team
    2026-05-27T15:29:07+00:00Added an answer on May 27, 2026 at 3:29 pm
    std::for_each(dogs.begin(), dogs.end(), std::bind(&Dog::bark, _1));
    

    In C++03, you can use boost::bind instead. You can try to wrap it in a separate function template, but I have a feeling it won’t buy you much. Using for_each where you need this behaviour is clear enough.

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

Sidebar

Related Questions

Is it possible to use the glDrawElements method when you have lets say 2
Lets say for example I have the following 2 vectors: *B *A The vector
Lets say i have vector of vectors vector< vector<int> > bigTable; vector<int> data; data.resize(2);
Lets say I have 2 points (x1,y1) and (x2,y2). And I can draw vector
Lets say i have vector of vectors vector< vector<int> > bigTable; bigTable.reserve(5); To clarify
I have two vectors with different dimensions. To make it simple,lets say A =
Lets say if I have a vector V, which has 10 elements. If I
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say I have an arbitrary vector A. What is the most efficient way
Lets say I have some domain objects that will need to be serialized/packed using

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.