Writing a Function Template With Template Template Arguments?

I have a function:
1
2
template<class Iterator, class T>
void a(Iterator, Iterator, const T&);

and I want to be able to simplify calls to 'a' with calls like
 
a(someIteratableContainer);

instead of having to call:
 
a(someIteratableContainer.begin(), someIteratableContainer.end(), valueOfTheContainersElementType);

I also want to be able to generalize the function to handle any of the standard iteratable contains: array, vector, deque, whatever.

I was under the impression I could write:
1
2
3
template<template<class T> class U> a(U<T>& container) {
    a(container.begin(), container.end(), g(T()));
}

where 'g()' returns an object of the element type. However, the compiler is claiming, no matter how I write a call to the overload, the original template is selected and/or the overload is invalid, depending on the various ways I attempt to write said overload. Any suggestions on the direction of where to find the correct way to write the overload would be appreciated. Thanks.
template<class T> class U is a template with one type template parameter. All of the containers you listed have more than one .

you could use template<class...> (except that still wouldn't work with std::array) or go the boost.range way and make it
1
2
template<class ForwardRange, class T>
void a(const ForwardRange& rng, const T&);

see pretty much any of http://www.boost.org/doc/libs/release/libs/range/doc/html/range/reference/algorithms.html
Last edited on
Thanks, Cubbi. Good point!

So, I've changed the code to try 'std::array' for now:
1
2
3
4
template<class T, size_t N>
void a(std::array<T, N>& theArray) {
    a(theArray.begin(), theArray.end(), g(T()));
}

Still no luck when I call:
 
a(someStdArray);

Same compiler complaints.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <vector>
#include <list>
#include <iostream>
#include <array>

template <class Container, class Value>
void a(Container C, Value V)
{
    auto it = C.begin();
    auto itend = C.end();
    while (it != itend) {
        if (V == *it) {
            std::cout << "found\n";
        }
        ++it;
    }
}


int main()
{
    std::vector<int> x {1, 2, 3, 4};
    std::array<int, 4> y {1, 5, 4, 1};
    std::list<int> z {0, 3, 1, 2};
    a(x, 1);
    a(y, 1);
    a(z, 1);
}
1
2
3
4
5
6
7
8
template <class Container>
void foo(Container &container){
   foo(
      container.begin(), 
      container.end(), 
      g( typename Container::value_type() ) 
   );
}
ne555, YES!!! Thank You!
Cubbi, I must admit I think I had a typo when I tested Your method. Thanks just as much.
Topic archived. No new replies allowed.