8

I want to declare a function that accepts different STL containers, but they must contain objects of a specific class (e.g. it should accept std::vector<double> and std::deque<double> but no std::vector<std::string>).

I have found answers for templating both the container and the contained types, but my attempts to adapt them so that the contained type is fixed have been unsuccessful.

3
  • 3
    What have you tried? What problems have your attempts given you? Commented Sep 29, 2017 at 8:48
  • 2
    ^That. Hint: value_type
    – Passer By
    Commented Sep 29, 2017 at 8:51
  • @PasserBy Are you talking about Henri Menke's second method? I had thought of static asserts, but thought there might be a more elegant solution. Commented Sep 29, 2017 at 9:17

1 Answer 1

13

You can do it with template template arguments (no typo). The first template argument of your template function is another template with a variadic number of template arguments. The second template argument are the variadic template arguments. In the signature you then fix the first template argument to the type you want (e.g. double) and let the compiler deduce the rest.

#include <deque>
#include <iostream>
#include <string>
#include <vector>

template < template < class ... > class Container, class ... Args >
void any_container(Container<double, Args...>)
{
    // print what was deduced
    std::cout << __PRETTY_FUNCTION__ << '\n';
}

int main()
{
    std::vector<double> vd;
    any_container(vd);

    std::deque<double> dd;
    any_container(dd);

    std::vector<std::string> vs;
    any_container(vs); // BOOM!
}

@PasserBy already hinted a different solution in this comment. Instead of having a substitution failure you could also just take the container as a template argument and the query its value_type in a static_assert. This has the advantage that you can put a custom error message.

#include <deque>
#include <iostream>
#include <string>
#include <type_traits>
#include <vector>

template <typename Container> void any_container(Container)
{
    static_assert(std::is_same<typename Container::value_type, double>::value,
                  "BOOM!");
    // print what was deduced
    std::cout << __PRETTY_FUNCTION__ << '\n';
}

int main()
{
    std::vector<double> vd;
    any_container(vd);

    std::deque<double> dd;
    any_container(dd);

    std::vector<std::string> vs;
    any_container(vs); // BOOM!
}
8
  • __PRETTY_FUNCTION__ is not standard. It is available in GCC and Clang and is not part of the preprocessor (in contrast to e.g. __LINE__). Commented Sep 29, 2017 at 9:07
  • So if I understood this correctly this is a bit of hack, right? I state that I will use a variadic number of template arguments which then turns out to be 0 every time, but that allows me to fix the first parameter, which wouldn't be possible otherwise. Is that it? Commented Sep 29, 2017 at 9:09
  • @user2891462 See the alternative solution below. Commented Sep 29, 2017 at 9:10
  • 1
    @user2891462 Also the variadic number of arguments is never zero in the above example. std::vector has a second template argument which is the allocator, which defaults to the standard allocator. Commented Sep 29, 2017 at 9:11
  • I see, thanks for the explanation. I prefer the first method, since the type is clearer in the declaration of the function :) Commented Sep 29, 2017 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.