1

I have a template class that has two template arguments. One of argument logically connected with another.
I want to reduce two arguments of the template into one.
This is what I have (simplified):

#include <iostream>
#include <vector>

template <typename T, typename dataT>
struct SomeManager {
    std::vector<T> v;
    void setData(dataT data) {
        v.push_back(T());
        v.back().setData(data);
    }
    void showLast() { 
        if (!v.empty())
            v.back().showData();
        else
            std::cout << "Nothing to show";
    }
};

struct Concrete1 {
    struct data {
        bool boolData;
    } currentData;
    void setData(data d) { currentData.boolData = d.boolData; }
    void showData() { std::cout << "Current bool is: " << currentData.boolData; }
};

struct Concrete2 {
    struct data {
        std::string stringData;
    } currentData;
    void setData(data d) { currentData.stringData = d.stringData; }
    void showData() { std::cout << "Current string data: " << currentData.stringData; }
};

To use it correctly dataT must be always T::data
So it is an example of correct declaration:

SomeManager<Concrete1, Concrete1::data> c1;
SomeManager<Concrete2, Concrete2::data> c2;

And this is incorrect:

SomeManager<Concrete1, Concrete2::data> c3;
c3.showLast(); // This even works

I am tried to rewrite SomeManager with one template argument but no success

struct vectorManager {
    std::vector<T> v;
    void addData(T::data) {/*...*/  } // syntax error : identifier 'data
    void showLast() { /*..*/}
};

Sure I can make #define for old version like this #define DECL_SOME_MANAGER(T) someManager<T, T::data> but I wonder s it possible to make it without #define?

Stepan Loginov
  • 1,667
  • 4
  • 22
  • 49

1 Answers1

2

This should be as simple as:

template <typename T>
struct SomeManager {

    typedef typename T::data dataT;

... with the rest of the template unchanged.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148