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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:44:20+00:00 2026-06-13T00:44:20+00:00

I decided to try to write a functional map implementation in C++ using templates,

  • 0

I decided to try to write a functional map implementation in C++ using templates, and this is what I’ve come up with:

template <
    class U, 
    class V, 
    template <class> class T 
>

class T<V> WugMap(
    class T<U>::const_iterator first, 
    class T<U>::const_iterator second, 
    V (U::*method)() const)

{
    class T<V> collection;
    while (first != second)
    {
        collection.insert(collection.end(), ((*(first++)).*method)());
    }
    return collection;
}

Now this is all fine and dandy, and even compiles. Problem is, I have no idea how to actually call it.

Trying the naive way yields the following error:

prog.cpp:42: error: no matching function for call to 
‘WugMap(__gnu_cxx::__normal_iterator<Container*, std::vector<Container, 
std::allocator<Container> > >, __gnu_cxx::__normal_iterator<Container*, 
std::vector<Container, std::allocator<Container> > >, int (Container::*)()const)’

As far as I can tell, all of the arguments are correct. gcc isn’t suggesting any alternatives at all, which leads me to believe that my definition of WugMap is suspect, but it compiles fine, so I’m rather lost. Can anyone guide me through this silliness?

If anyone can suggest a better way to write a function like this that will support consuming any type of collection containing any type of object, I’ll look into changing it.

Here’s my ideone so far.

I’m currently using Ideone, which is using C++03, gcc 4.3.4.

Addendum 1

Is this possible in C++11? It’s been hinted that it is. I know templates in C++11 support varying numbers of arguments so I’ll modify my requirements to suit that as well. I’ll put a bit of effort into writing something up, but in the meantime, here are the requirements that I’m looking for:

  • Should have a signature something like the following:

    C2<V, ...> map(const C1<U, ...>&, V (U::*)(...), ...)
    

    That’s taking some collection C1, containing elements of type U, constructed with some number of default parameters, by reference, and also taking some member function (returning V and taking some number of arguments of unknown types) of U, and then taking, in order, arguments to be passed to the member function. The function will finally return a collection of type C2 containing elements of type V and being initialized with an unknown number of default parameters.

  • Should be chainable:

    vector<int> herp = map(
                       map(
                            set<Class1, myComparator>(),
                       &Class1::getClass2, 2, 3),
                       &Class2::getFoo);
    
  • Bonus points if I don’t have to have template arguments or any other extra verbosity when using it.

std::transform is great, but not chainable.

  • 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-06-13T00:44:21+00:00Added an answer on June 13, 2026 at 12:44 am

    Template arguments can never be deduced from nested types. Even if U and V can be deduced from the member function pointer, you won’t be able to deduce the template type T.

    Explicitly specifying the template arguments as in the link to ideone (I didn’t the the link before writing the statement above) doesn’t work either, mainly because the template arguments for std::vector are not just a single type T! std::vector takes a value type and an allocator type. Fixing things up is getting fairly ugly:

    #include <vector>
    #include <iostream>
    
    using namespace std;
    
    class Container
    {
    public:
        Container() {}
        Container(int _i) : i(_i) {}
    
        int get_i() const {return i;}
    
        int i;
    };
    
        template <
            class U, 
            class V, 
            template <typename...> class T 
        >
    
        T<V> WugMap(
            typename T<U>::const_iterator first, 
            typename T<U>::const_iterator second, 
            V (U::*method)() const)
        {
            T<V> collection;
            while (first != second)
            {
                collection.insert(collection.end(), ((*(first++)).*method)());
            }
            return collection;
        }
    
    int main()
    {
        vector<Container> containers;
        for (int i = 0; i < 10; ++i) containers.push_back((Container(i)));
    
        WugMap<Container, int, std::vector>(
            containers.begin(), containers.end(), &Container::get_i);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I decided to try to my hand at this, and have had a somewhat
After failing to achieve this with link_to remote, I decided to try jQuery way.
I came accross PetaPoco and decided to give it a try Did anyone come
I've decided to try and write a simulation program in Clojure (as a proof
I was bored and decided to try my hand at using Linq to solve
After being unable to solve this issue , I decided to try to implement
I got home yesterday and decided to try and write a scheme program that
I'm a beginner in Objective-C, and I decided to try to write some simple
I've been using QtCreator lately and decided to try out Visual Studio 2010 (Ultimate).
Having decided to try AForge for video and imaging stuff, I tried to implement

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.