0

I've got such peace of code:

#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;
}

I just wonder how to make static method loader::load work. I'd like to have two overloads of this method, one with template parameter storage, second with template parameter state_type (as it is now, but it doesn't work).

Have any ideas?

Thank you.

1 Answers1

0

The inner_typeof the second parameter storage_template<inner_type>::state_type is a non-deduced context.

If you have a parameter type T, the compiler cannot deduce inner_type from T without trying out all possible types and see if any of those results in storage_template<any_type>::state_type being T.

The standard says that this would be way too much works, so the compilers should not try that.

Community
  • 1
  • 1
Bo Persson
  • 90,663
  • 31
  • 146
  • 203