Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在一个类中保留具有不同值类型的多个映射,并支持基于类型的查找_C++_Tuples - Fatal编程技术网

C++ 在一个类中保留具有不同值类型的多个映射,并支持基于类型的查找

C++ 在一个类中保留具有不同值类型的多个映射,并支持基于类型的查找,c++,tuples,C++,Tuples,我想实现一个异构存储,使用具有不同值类型的多个映射。我正在根据值类型为地图编制索引。为此,我使用了typelist。这是一个简化的例子 我的类型列表类如下所示 namespace details { struct null{}; template<int N, typename E, typename T, typename ... Ts> struct index : index<N+1, E, Ts...> {}; template

我想实现一个异构存储,使用具有不同值类型的多个映射。我正在根据值类型为地图编制索引。为此,我使用了typelist。这是一个简化的例子

我的
类型列表
类如下所示

namespace details
{
    struct null{};

    template<int N, typename E, typename T, typename ... Ts>
    struct index : index<N+1, E, Ts...> {};

    template<int N, typename E, typename ... Ts>
    struct index<N, E, E, Ts...>
    {
        constexpr static int value = N;
    };

    template<int N, typename E>
    struct index<N, E, null>
    {
        constexpr static int value = -1;
    };
}

template<typename ... Ts>
struct typelist
{
    using type = typelist<Ts...>;
    template<typename T>
    struct index : details::index<0, T, Ts..., details::null> {};
};
最后,存储管理器看起来像是,它有几个存储impl实例,我想根据类型对它们进行索引

struct storage_manager{
    // tuple storing different storage_impls
    std::tuple< storage_impl<double> , storage_impl<std::string> > storages { storage_impl<double>{},storage_impl<std::string>{} };
    using item_types = typelist<double,std::string>;

    storage_manager(){}
    ~storage_manager(){}
    storage_manager(storage_manager const &) = delete;

    template<typename T>
    bool try_getting(int key, T &value)
    {
        return std::get<item_types::index<T>::value>(storages).try_getting(key,value);
    }
};

int main()
{ 
    storage_manager  mgr;
    double val1;
    std::cout<<mgr.try_getting(123,val1);
}
结构存储管理器{ //存储不同存储的元组 std::tuplestorages{storage\u impl{},storage\u impl{}; 使用项目类型=类型列表; 存储管理器(){} ~storage_manager(){} 存储管理器(存储管理器常数&)=删除; 样板 bool try_获取(int键、T和值) { 返回std::get(存储)。尝试获取(键、值); } }; int main() { 仓库经理;; 双价1;
std::cout以下是我的工作代码,我已声明cache\u impls为成员变量,并为每种情况指定了专用函数
storage\u ref
。 我只实现了
try\u get
,其他功能可以使用
storage\u ref
实现

#include <iostream>
#include <tuple>
#include <map>
#include <boost/noncopyable.hpp>

template<typename T>
struct storage_impl : public boost::noncopyable
{
    storage_impl(){}
    ~storage_impl(){}

    bool try_getting(int key, T &value)
    {
        auto search = _storage.find(key);
        if(search != _storage.end() )
        {
            value = search->second;
            return true;
        }
        return false;   
    }
    bool try_setting(int key, T const &value)
    {
        auto search = _storage.insert(std::make_pair(key,value));
        if(search.second == true )
        {
            return true;
        }
        return false;   
    }

    bool exists(int key)
    {
        auto search = _storage.find(key);
        if(search != _storage.end() )
        {
            return true;
        }
        return false; 
    }

    std::map<int,T> _storage;
};

struct storage_manager  : public boost::noncopyable
{
    storage_impl<double> double_store;
    storage_impl<std::string> string_store;

    template<typename T>
    struct item_return{ using type = storage_impl<T>; };

    template<typename T>
    typename item_return<T>::type storage_ref();

    template<typename T> 
    bool try_getting(int key, T &value)
    {
        return storage_ref<T>().try_getting(key,value);
    }
    template<typename T> 
    bool try_setting(int key, T const &value)
    {
        return storage_ref<T>().try_setting(key,value);
    }

    template<typename T> 
    bool exists(int key)
    {
        return storage_ref<T>().exists(key);
    }

    storage_manager(){}
    ~storage_manager(){}
};


//double specialization
template<>
struct storage_manager::item_return<double>{ using type = storage_impl<double>&; };

template<>
inline storage_impl<double>& storage_manager::storage_ref<double>()
{
    return double_store;
}

//std::string specialization
template<>
struct storage_manager::item_return<std::string>{ using type = storage_impl<std::string>&; };

template<>
inline storage_impl<std::string>& storage_manager::storage_ref<std::string>()
{
    return string_store;
}

int main()
{
    storage_manager mgr;
    double val1 = 90;
    std::cout<<mgr.try_getting(123,val1)<<'\n';

    std::cout<<mgr.try_setting(123,val1)<<'\n';

    std::cout<<mgr.exists<double>(123)<<'\n';

    std::string val2;
    std::cout<<mgr.try_getting(123,val2)<<'\n';  
}
#包括
#包括
#包括
#包括
样板
结构存储\u impl:public boost::不可复制
{
存储\u impl(){}
~storage_impl(){}
bool try_获取(int键、T和值)
{
自动搜索=_storage.find(键);
如果(搜索!=\u storage.end())
{
值=搜索->秒;
返回true;
}
返回false;
}
布尔try_设置(整数键、T常量和值)
{
自动搜索=_storage.insert(std::make_pair(key,value));
如果(search.second==true)
{
返回true;
}
返回false;
}
布尔存在(整数键)
{
自动搜索=_storage.find(键);
如果(搜索!=\u storage.end())
{
返回true;
}
返回false;
}
标准::地图存储;
};
结构存储\u管理器:公共boost::不可复制
{
仓储式双仓储;
存储\u impl字符串\u存储;
样板
结构项_返回{using type=storage_impl;};
样板
typename项_return::type storage_ref();
样板
bool try_获取(int键、T和值)
{
返回存储\u ref()。尝试获取(键、值);
}
样板
布尔try_设置(整数键、T常量和值)
{
返回存储\u ref()。尝试设置(键、值);
}
样板
布尔存在(整数键)
{
返回存储器_ref()。存在(键);
}
存储管理器(){}
~storage_manager(){}
};
//双重专业化
样板
结构存储管理器::项返回{using type=storage_impl&;};
样板
内联存储\u impl&存储\u管理器::存储\u ref()
{
返回双_店;
}
//字符串专门化
样板
结构存储管理器::项返回{using type=storage_impl&;};
样板
内联存储\u impl&存储\u管理器::存储\u ref()
{
返回字符串存储;
}
int main()
{
仓库经理;;
双价1=90;
标准::cout
#include <iostream>
#include <tuple>
#include <map>
#include <boost/noncopyable.hpp>

template<typename T>
struct storage_impl : public boost::noncopyable
{
    storage_impl(){}
    ~storage_impl(){}

    bool try_getting(int key, T &value)
    {
        auto search = _storage.find(key);
        if(search != _storage.end() )
        {
            value = search->second;
            return true;
        }
        return false;   
    }
    bool try_setting(int key, T const &value)
    {
        auto search = _storage.insert(std::make_pair(key,value));
        if(search.second == true )
        {
            return true;
        }
        return false;   
    }

    bool exists(int key)
    {
        auto search = _storage.find(key);
        if(search != _storage.end() )
        {
            return true;
        }
        return false; 
    }

    std::map<int,T> _storage;
};

struct storage_manager  : public boost::noncopyable
{
    storage_impl<double> double_store;
    storage_impl<std::string> string_store;

    template<typename T>
    struct item_return{ using type = storage_impl<T>; };

    template<typename T>
    typename item_return<T>::type storage_ref();

    template<typename T> 
    bool try_getting(int key, T &value)
    {
        return storage_ref<T>().try_getting(key,value);
    }
    template<typename T> 
    bool try_setting(int key, T const &value)
    {
        return storage_ref<T>().try_setting(key,value);
    }

    template<typename T> 
    bool exists(int key)
    {
        return storage_ref<T>().exists(key);
    }

    storage_manager(){}
    ~storage_manager(){}
};


//double specialization
template<>
struct storage_manager::item_return<double>{ using type = storage_impl<double>&; };

template<>
inline storage_impl<double>& storage_manager::storage_ref<double>()
{
    return double_store;
}

//std::string specialization
template<>
struct storage_manager::item_return<std::string>{ using type = storage_impl<std::string>&; };

template<>
inline storage_impl<std::string>& storage_manager::storage_ref<std::string>()
{
    return string_store;
}

int main()
{
    storage_manager mgr;
    double val1 = 90;
    std::cout<<mgr.try_getting(123,val1)<<'\n';

    std::cout<<mgr.try_setting(123,val1)<<'\n';

    std::cout<<mgr.exists<double>(123)<<'\n';

    std::string val2;
    std::cout<<mgr.try_getting(123,val2)<<'\n';  
}