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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:10:40+00:00 2026-05-26T08:10:40+00:00

Say you would like to make iterative modifications to the elements of a container

  • 0

Say you would like to make iterative modifications to the elements of a container object and do something with the elements at each iteration. The code I came up with to do something like this:

#include <vector>
#include <iostream>

template <class InputIterator, class Operation, class Callback>
void transformAndTraverse(InputIterator begin, InputIterator end,  Operation op,  Callback cb) {
  InputIterator start = begin;
  while (begin != end) {
    op(begin);
    cb(start, end);
    ++begin;
  }
}

template <class InputIterator>
struct plotFnObject {
  void operator()(InputIterator begin, InputIterator end) {
    while (begin < end) {
      std::cout << *begin << " ";
      ++begin;
    }
    std::cout << std::endl;
  }
};

template <class InputIterator>
struct addTwoFnObject {
  void operator()(InputIterator idata) { *idata += 2; }
};

int main(int argc, char **argv) {
  typedef std::vector<int>::iterator vec_iter;
  std::vector<int> vec;
  vec.push_back(0);
  vec.push_back(0);
  vec.push_back(0);

#ifndef DEMO_COMPILE_ERROR
  // the below works
  transformAndTraverse(vec.begin(), vec.end(), addTwoFnObject<vec_iter>(), plotFnObject<vec_iter>());
#else
  // but this generates compilation errors
  addTwoFnObject<vec_iter> addtwo();
  plotFnObject<vec_iter> plot();
  transformAndTraverse(vec.begin(), vec.end(), addtwo, plot);
#endif
}

The trouble is that I get some difficult to understand compiler errors with the second form:

$ g++ testtemplate.cpp -DDEMO_COMPILE_ERROR
testtemplate.cpp: In function 'void transformAndTraverse(InputIterator, InputIterator, Operation, Callback) [with InputIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >, Operation = addTwoFnObject<__gnu_cxx::__normal_iterator<int*, std::vector<int> > > (*)(), Callback = plotFnObject<__gnu_cxx::__normal_iterator<int*, std::vector<int> > > (*)()]':
testtemplate.cpp:44:60:   instantiated from here
testtemplate.cpp:8:5: error: too many arguments to function
testtemplate.cpp:9:5: error: too many arguments to function

$ g++ testtemplate.cpp
$

Why is the compiler OK with creating the function objects in-line, but not with creating them out of line and passing them?

$ g++ --version
g++ (GCC) 4.5.3
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  • 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-26T08:10:41+00:00Added an answer on May 26, 2026 at 8:10 am

    These are declarations of functions:

    addTwoFnObject<vec_iter> addtwo();
    plotFnObject<vec_iter> plot();
    

    These are local variable instantiations:

    addTwoFnObject<vec_iter> addtwo;
    plotFnObject<vec_iter> plot;
    

    You need to lose the extra parens at the end, otherwise the compiler complains that here you are trying to call the hypothetical function addtwo without providing an argument list:

    transformAndTraverse(vec.begin(), vec.end(), addtwo, plot);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought I'd never say this. I would like to make my machine run
Lets say that I would like to construct a list (L2) by appending elements
First I would like to say that I am new to this site, never
I would like to store an object FOO in a database. Lets say FOO
I would like to make a class that has different text to say to
I would like to make use of somehting like [parse_ini_file][1]. Lets say for instance
I would like to make a simple desktop application to learn this kind of
Let say I would like to set nested style for development and compressed for
I would like to be able to say things like cd [.fred] and have
I would like to apply successive animations (say ScaleAnimation) to an ImageView showing a

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.