0

I have a function that receives a template parameter.

template<class Container>
void function(const Container& object)
{
     //here i want to iterate through object and print them
}

int main()
{
     function(std::vector<int>{1,3,6,7});
     function(std::vector<std::vector<int>>{{1,2,3},{2,5,7}});
}

Is it possible to do this in one function? Suppose the container argument will be integer.

Ionut Alexandru
  • 680
  • 5
  • 17

3 Answers3

7

One example:

template<class T>
void print(T const& object) {
    std::cout << object;
}

template<class... Args>
void print(std::vector<Args...> const& container) {
    for(auto const& element : container) {
        print(element);
        std::cout << ' ';
    }
    std::cout << '\n';
}

int main() {
     print(std::vector<int>{1,3,6,7});
     print(std::vector<std::vector<int>>{{1,2,3},{2,5,7}});
}
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • I'd name it `std::ostream & operator<<(std::ostream &, std::vector const&)` rather than `void print(std::vector const&)` – Caleth Feb 19 '20 at 11:10
  • @Caleth That may cause issues with ADL and ODR because `operator<<` cannot be put into `std::` but its arguments are from `std::`. So that `operator<<` must be in global namespace, which may clash with other smart users defining the same operator. – Maxim Egorushkin Feb 19 '20 at 11:12
  • *"in one function"*. nit picking ;-) – Jarod42 Feb 19 '20 at 12:51
1

This should work for your case. Note that I'm using a trait as implemented here in this amazing solution by @Jarod42 https://stackoverflow.com/a/29634934/8192043.

template<template<typename ...> typename C, typename D, typename ... Others>
void function(const C<D, Others...> &object)
{
    if constexpr(is_iterable<D>::value)
    {
       for(const auto& v : object)
       {
           for (const auto& w : v)
           {...}
       }
    }
    else
    {
       for (const auto& w : object)
       {...}
    }
}
theWiseBro
  • 1,439
  • 12
  • 11
0

With is_iterable traits, you might do:

template<typename Container>
void function(const Container& object)
{
    if constexpr(is_iterable<std::decay_t<*object.begin()>>::value)
    {
       for(const auto& v : object)
       {
           function(v); // recursive call
       }
    }
    else
    {
        for (const auto& w : object)
        {
            // ...
        }
    }
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302