vendredi 12 mars 2021

Template member function with stl

I have the following existing code snippet in MyQueue.h

class MyQueue {
 struct A {
  explicit A(ValType init){
 }
 ValType memberA;
 };
 struct B {
  explicit B {...}
  std::list<A> listofA; 
 };
 std::unordered_map<std::string, B> myMap;
};

ValType is a know class type defined in a different file.

GOAL

I need to templatize Valtype, meaning struct A constructor (or anywhere Valtype is used) can be Valtype or Valtype2.

My Questions are as follows

  1. Does the whole class MyQueue need to be a template class or can the relevant member variables and member function be templatized like I have below. What are the general rules to decide this?

  2. Can stl container be of template type? ex std::unordered_map<std::string, B<T>> myMap; std::list<A<T>> listofA;

This class has a cpp file with definitions of class member functions that use A, B and myMap.

class MyQueue {
 template <class T>
 struct A {
  explicit A(T init){
 }
 T memberA;
 };

 struct B {
  explicit B {...}
  template <class T>
  std::list<A<T>> listofA; 
 };
 template <class T>
 std::unordered_map<std::string, B<T>> myMap;
};

Aucun commentaire:

Enregistrer un commentaire