Home > Software design >  C Instantiate template template class
C Instantiate template template class

Time:12-08

I am trying to implement my own container (in my demonstration called Manager), which stores another templated container (e.g Node<T>). Therefore I am trying to instantiate an object like Mgr<Node<T>>. After reading through all of the template template-articles on here, I came to the following demonstration code.

#include <iostream>

template<typename T>
struct Node {
    T data;
    int id;
};

template<template<typename> class Container, typename T>
struct Mgr {
    Container<T> nodes;
    int id;
    void print()
    {
        std::cout << this->id;
    }
};

int main() {
    Mgr<Node<char>, char> mgr;
    mgr.print();
    return 0;
}

Compiling this still gives me the error message: type/value mismatch at argument 1 in template parameter list for 'template<template<class> class Container, class T> struct Mgr...Expected a class template, got Node<char> Is my function definition wrong, or do my instantiation parameters incorrect?

CodePudding user response:

template<typename> class Container expect that you pass a template as Container, Node<char> is not a template, it's a concrete type.

You want Mgr<Node, char> mgr;

  • Related