0
的參數我已經得到的代碼,和平:模板類的嵌套結構作爲通用的方法
#include <map>
class empty_class
{
};
template <typename base_class = empty_class>
class traits : public base_class
{
public:
typedef int id;
typedef std::map<id, id> ids;
};
class simple_traits
{
public:
typedef double id;
typedef std::map<id, id> ids;
};
template <typename base_class = traits<>>
class storage_template : public base_class
{
public:
typedef typename base_class::id id;
typedef typename base_class::ids ids;
struct state;
typedef state state_type;
public:
struct state
{
ids m_ids;
};
public:
inline state_type & get_state()
{
return m_state;
}
inline void set_state(state_type & state)
{
m_state = state;
}
protected:
state_type m_state;
};
typedef storage_template<> default_storage;
typedef storage_template<simple_traits> simple_storage;
class loader
{
public:
template <class inner_type>
static bool load(const char * filepath, typename storage_template<inner_type>::state_type & state)
{
/* further actions */
return true;
}
template <class inner_type>
static bool load(const char * filepath, typename storage_template<inner_type> & storage)
{
return load(filepath, storage.get_state());
}
};
int main(void)
{
default_storage storage_1;
simple_storage storage_2;
loader::load("filepath", storage_1.get_state());
loader::load("filepath", storage_2.get_state());
loader::load("filepath", storage_1);
loader::load("filepath", storage_2);
return 0;
}
我只是想知道如何使靜態方法loader::load
工作。我想這個方法有兩個重載,一個帶有模板參數storage
,其次帶有模板參數state_type
(就像現在這樣,但它不起作用)。
有什麼想法嗎?
謝謝。