首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >作为泛型方法参数的模板类的嵌套结构

作为泛型方法参数的模板类的嵌套结构
EN

Stack Overflow用户
提问于 2016-07-30 16:09:03
回答 1查看 130关注 0票数 0

我得到了如此安心的代码:

代码语言:javascript
复制
#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 (就像现在一样,但它不起作用)。

有什么想法吗?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-07-30 16:23:35

第二个参数storage_template<inner_type>::state_typeinner_type是一个non-deduced context

如果参数类型为T,编译器将无法从T中推导出inner_type,除非尝试所有可能的类型,并查看其中是否有导致storage_template<any_type>::state_typeT的结果。

该标准说,这将是太多的工作,所以编译器不应该尝试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38671807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档