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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:53:46+00:00 2026-05-30T01:53:46+00:00

Suppose I have a function that does something on an arbitrary container type (C++11):

  • 0

Suppose I have a function that does something on an arbitrary container type (C++11):

template<class containerType>
void bar( containerType& vec ) {

        for (auto i: vec) {
                std::cout << i << ", ";
        }

        std::cout << '\n';
}

I can call this function from another function like this:

void foo() {
        std::vector<int> vec = { 1, 2, 3 };
        bar(vec);
}

Now suppose I have different functions just like bar, and I want to pass one of these functions to foo, then foo would look something like this:

template<class funcType>
void foo( funcType func ) {
    std::vector<int> vec = { 1, 2, 3 };
    func(vec);
}

However, calling foo like this:

foo(bar);

does not work (pretty clear, since bar is not a function but a function template). Is there any nice solution to this? How must I define foo to make this work?

EDIT: here is a minimal compileable example, as demanded in the comments…

#include <iostream>
#include <vector>
#include <list>

template<class containerType>
void bar( containerType& vec ) {

        for (auto i: vec) {
                std::cout << i << ", ";
        }

        std::cout << '\n';
}

template<typename funcType>
void foo(funcType func) {

        std::vector<int> vals = { 1, 2, 3 };
        func(vals);

}

int main() {
        // foo( bar );  - does not work.
}
  • 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-30T01:53:46+00:00Added an answer on May 30, 2026 at 1:53 am

    Something like this? (not fully awake, might miss the point)

    #include <iostream>
    #include <vector>
    #include <list>
    
    struct Test{
    template<class containerType>
    static void apply( containerType& vec ) {
    
            for (auto it = vec.begin(); it != vec.end(); ++it) {
                    std::cout << *it << ", ";
            }
    
            std::cout << '\n';
    }
    };
    
    template<class FuncStruct>
    void foo() {
    
            std::vector<int> vals;
            vals.push_back(1);
            FuncStruct::apply(vals);
    
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        foo<Test>();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's suppose I have a C extension function that does something that is completely
Suppose I have a function which looks like this: template <class In, class In2>
Suppose I have written a decorator that does something very generic. For example, it
Suppose I have virtual function foo() in class B, and I need slightly different
Suppose I have this function: void my_test() { A a1 = A_factory_func(); A a2(A_factory_func());
Suppose I have a C function: void myFunction(..., int nObs){ int myVec[nObs] ; ...
I have a class that looks like: class A { public: A(); void MethodThatWillNotBeInOtherStructure();
Suppose I have a function which takes a nullary functor as an argument: void
Suppose I have a function in a single threaded program that looks like this
I have a function I've written that was initially supposed to take a string

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.