nested templates function taking 2 containers with the same inner type

c++ template function implementation in cpp file
c++ template function specialization
c++ template function in class
c++ template class
c template member variable
c++ template specialization
template <typename vs class
c++ template class constructor

I am trying to write function which is roughly similar to a function that takes two instances of containers (let's say for now they are the same), containing same type and merges them. So here's my function for vectors:

template<typename T>
void test(std::vector<T> v1, std::vector<T> v2) {
  std::cout << "called nested for c\n";

and it works.

Now I want to have one that would work for sets or vectors. And I try this:

template<typename T, template <typename> typename C >
void test(C<T> v1, C<T> v2) {
  std::cout << "called nested for c<t>\n";

and get note: template argument deduction/substitution failed: error: wrong number of template arguments (3, should be 1) test(s, s); ^ note: provided for ‘template class C’ template typename C > ^

I try

template< template <typename T> typename C >
void test(C<T> v1, C<T> v2) {}

std::set<int> s = {1,2};
test(s, s);

and that doesn't work: note: template argument deduction/substitution failed: note: cannot convert ‘s’ (type ‘std::set’) to type ‘int’ test(s, s); ^

So I try

template<typename C, typename T >
void test(C<T> v1, C<T> v2) {}

and get note: candidate: template void test(C, C) void test(C v1, C v2) { ^ note: template argument deduction/substitution failed: note: couldn't deduce template parameter ‘T’ test(s, s); ^

I feel like I'm not even close to understanding how templates actually work in C++ and it is so sad. Also in reality I want the containers to be different. And ideally be able to somehow specify a limited set of allowed containers. And they are not really std:: containers.

Note that the final goal is to be able to take 2 different containers, so something like test(vector<int>, set<int>). And modifying the containers is kind of impossible.

Your first attempt was nearly right. You got the error because the standard containers like vector accept more than one template parameter. For example, this is std::vector:

    class T,
    class Allocator = std::allocator<T>
> class vector;

You never usually use the second parameter, and it has a default, but it's still present, so your template template parameter template <typename> typename C can't match. Instead, you can do this:

template<template <typename...> typename C, typename... Ts>
void test(C<Ts...> v1, C<Ts...> v2) {}

Templates, C++ FAQ, My template function does something special when the template type T is int or Why am I getting errors when my template-derived-class uses a nested type it inherits Class templates are often used to build type safe containers (although this specify the types is when the function takes two parameters of the same type,  Nested Azure ARM templates are very common for more advance scenarios like deployment of more than one simplistic VM. There may be a bunch of databases, VMs, queues and so on for a complex environment. You can break down a solution into targeted components using nested Azure ARM templates. Another advantage is the modularization and reusability

I believe what you are looking for is

template<template <typename...> typename Cont1, 
         template <typename...> typename Cont2,
         typename... T>
void test(Cont1<T...> a, Cont2<T...> b) { }

In the above template <typename...> typename Cont1 declares one template template type and template <typename...> typename Cont2 declares another one so you can have two different container types. The both share T... so each template container needs to have a matching template parameter list. This means

test(std::map<int, int>{}, std::map<int, int>{});
test(std::set<int>{}, std::set<int>{});
test(std::vector<int>{}, std::set<int>{});

all work but

test(std::map<int, int>{}, std::vector<int>{});
test(std::map<int, int>{}, std::set<int>{});


Perspectives of System Informatics: Third International Andrei , Because of their ability to change their internal type at any time, Walue class instances into a Value parameterized container, thus obtaining nested containers. With this information and Value's parameterized method template-​class T2 T& This is shown in the following sample code: // double sin (double); a function  I am trying to write function which is roughly similar to a function that takes two instances of containers (let's say for now they are the same), containing same type and merges them.

You need at least 2 template parameters:

  1. Container type
  2. Contained type

Since you're using an STL container and passing the contained type apart from the container, the compiler cannot infer the other parameters (e.g., allocator std::allocator<T>, compare std::less<T>). But you can forward all nested templated parameters using a variadic template. Example:

template <template <typename...> class C, typename... T>
struct wrapper {
    typedef C<T...> type;
wrapper<std::vector, int>::type foo;
wrapper<std::set   , int>::type bar;

You can see it in ideone.

Link templates for deployment, Using linked and nested templates when deploying Azure resources The scope determines how parameters, variables, and functions like and use that to get the URL for other templates in the same location. "kind": "StorageV2" // Copy works here when scope is inner // But, when 0 Open; 2 Closed. When scope is set to outer, you can't use the reference function in the outputs section of a nested template for a resource you have deployed in the nested template. To return the values for a deployed resource in a nested template, either use inner scope or convert your nested template to a linked template.

C++ Primer, 257 function template, 626 member function, 437 and header, 437 inner_product, 376 multiset, 376 return type fromset::insert, 373 sequential container, 319 set, 633 template argument conversion, 638 member template, 663 nested class 88 isalpha, 88 ISBN , 2 isbn_mismatch, 699 destructor explained, 709 iscntrl,  Another time when you must specify the types is when the function takes two parameters of the same type, but you give it two different types. template<typename T> void g(T x, T y); int m = 0; long n = 1; g(m, n); Since m and n have different types, the compiler can’t deduce what type to use for T, so you have to tell it what to use:

Member templates, Template declarations (class, function, and variables (since C++14)) can appear inside a member specification of any class, struct, or union that  Let's say I want to make a template class that take 2 types, one of which is also a template class that will depend on the first type:

Handbook of Object Technology, A pattern may then be used as a class, procedure, function, etc. patterns with other patterns, and it is an alternative to template classes and generic classes. 1996) and recently nested classes (called inner classes) were added to Java. 2​-D graphics, meta-programming, basic data structures (such as containers), and  The ATAN2 function takes two parameters of the same type. If you pass parameters of different types—for example, one PLS_INTEGER and one BINARY_FLOAT—PL/SQL tries to find a match where both parameters use the higher type. In this case, that is the version of ATAN2 that takes two BINARY_FLOAT parameters; the PLS_INTEGER parameter is converted upwards.