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
?