如何存储类型以供以后比较

时间:2014-05-19 23:56:25

标签: c++ c++11 types lambda c++14

首先关闭我的用例,因为我可能认为方向错误:我想创建一个将值映射到类型的地图。例如:

Map<std::string> map;
map.insert<int, double, char>("Hey");
auto string = map.at<int, double, char>();

std::type_index来说,这很容易。但是,我希望添加匹配类型的可能性,这些类型与搜索的类型不匹配,当它们是可转换的时。因此,以下内容也应返回"Hey",因为float可以转换为double

auto string = map.at<int, float, char>();

我不能在这种情况下使用type_index,因为std::is_convertible仅适用于类型。 This would be the version without conversion,但就目前而言,如果不进行重大更改,就很难将转化处理添加到其中。


我目前的尝试类似于以下内容,请注意这不起作用,只是展示了我尝试实施的内容:

template<typename T> 
class Map {
    T value; 
    std::vector<Map<T>> children; // all the children of the current node.
                                  // in the above example, if this was
                                  // the int node, the only child 
                                  // would be the double node

    template<typename T1>
    constexpr bool is_convertible() const {
        return std::is_convertible<__T__, T1>::value; // this isn't applicable
                                                  // since __T__ can't be
                                                  // stored (this nodes
                                                  // type)
    }

public:
    template<typename T1, typename... Tn>
    void insert(T&& value) {
        // iterate through/create the child nodes until the last template param
    }
    template<typename T1, typename... Tn>
    T& at() {
        // iterate through thechild nodes until a matching child is found
        // either exact match or a convertible 

        for(auto &c: children) {
            // if the above function would work
            if(c.template is_convertible<T1>()) { 
                return c.template at<Tn...>();
            }
        }
    }

}

现在我在我的智慧结束如何实现这一目标。我想将lambdas实现为比较器函数,但是lambda可以存储当前节点的类型,但是它无法在调用时接受模板参数来进行比较。

是否有一些C + 1y通用lambda比较器魔法,甚至更简单的方法?

3 个答案:

答案 0 :(得分:0)

我希望这可以做你想要的,有足够的空间进行扩展和创建附加到你想要的任何类型组合的模板专业化。它不是非常漂亮,但它可能会被重构并美化。

#include <iostream>

template <typename... Args>
struct map {
};

template <>
struct map<int, float, char> {
    static constexpr char value[] = "int float char";
};
constexpr char map<int,float,char>::value[];

template <typename T>
struct map<int, T> {
    static constexpr typename std::enable_if<std::is_integral<T>::value, char>::type value[] = "int, T";
};
template <typename T>
constexpr typename std::enable_if<std::is_integral<T>::value, char>::type map<int,T>::value[];

int main() {
    std::string v  = map<int,float,char>::value;
    std::string w  = map<int,int>::value;
    std::string w2 = map<int,unsigned>::value;
    // std::string w3 = map<int,float>::value; Won't compile

    std::cout << v << "\n";
    std::cout << w << "\n";
    std::cout << w2 << "\n";
    return 0;
}

答案 1 :(得分:0)

我使用boost :: fusion编写了一些奇怪的代码,这些代码接近于你想做的事情:

#include <boost/fusion/container/map.hpp>
#include <boost/fusion/include/insert.hpp>
#include <boost/fusion/include/pair.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <string>
#include <iostream>
#include <tuple>
#include <type_traits>
#include <memory>
template <std::size_t Value1, std::size_t Value2>
struct MinSizeT {
    static const std::size_t value = (Value1 > Value2) ? Value2 : Value1;
};
template<typename T1, typename T2, std::size_t N>
struct TupleIsConvertibleHelper {
    static const bool value = std::is_convertible<typename std::tuple_element<N - 1, T1>::type, typename std::tuple_element<N - 1, T2>::type>::value && TupleIsConvertibleHelper<T1, T2, N - 1>::value;
};
template<typename T1, typename T2>
struct TupleIsConvertibleHelper<T1, T2, 0> {
    static const bool value = true;
};
template<typename T1, typename T2>
bool TupleIsConvertible() { // Return true if all types in T1 are convertible to their corresponding type in T2
    if (std::tuple_size<T1>::value != std::tuple_size<T2>::value)
        return false;
    constexpr std::size_t minSize = MinSizeT<std::tuple_size<T1>::value, std::tuple_size<T2>::value>::value;
    return TupleIsConvertibleHelper<T1, T2, minSize>::value;
}
template<typename MapInserter>
class Map {
    MapInserter mc;
    template<typename... Types>
    struct do_at {
        template <typename T>
        void operator()(T const& x) const { // Find an exact match or the last convertible match
            typedef std::tuple<Types...> t1;
            typedef typename T::first_type t2;
            if (exactMatch)
                return;
            if (std::is_same<t1, t2>::value) {
                exactMatch = true;
                value = x.second;
            }
            else if (TupleIsConvertible<t1, t2>())
                value = x.second;
        }
        mutable bool exactMatch;
        mutable typename MapInserter::value_type value;
        do_at() : exactMatch(false) {}
    };
public:
    Map(MapInserter _mc) : mc(_mc) { }
    template<typename... Types>
    typename MapInserter::value_type at() {
        do_at<Types...> res;
        boost::fusion::for_each(mc.data->map, res);
        return res.value;
    }
};
template<typename ValueType, typename MapType = boost::fusion::map<>, typename ParentType = void*>
struct MapInserter {
    typedef ValueType value_type;
    struct Helper {
        MapType map;
        std::shared_ptr<ParentType> parent; // Must keep parent alive because fusion is lazy.
        Helper() = default;
        Helper(MapType&& _map, std::shared_ptr<ParentType> _parent) : map(std::move(_map)), parent(_parent) {}
    };
    std::shared_ptr<Helper> data;
    template<typename... KeyTypes>
    auto Insert(ValueType value) -> MapInserter<ValueType, decltype(boost::fusion::insert(data->map, boost::fusion::end(data->map), boost::fusion::make_pair<std::tuple<KeyTypes...>>(value))), Helper> {
        auto newMap = boost::fusion::insert(data->map, boost::fusion::end(data->map), boost::fusion::make_pair<std::tuple<KeyTypes...>>(value));
        return MapInserter<ValueType, decltype(newMap), Helper>(std::move(newMap), data);
    }
    MapInserter() : data(std::make_shared<Helper>()) { }
    MapInserter(MapType&& _map, std::shared_ptr<ParentType> _parent) : data(std::make_shared<Helper>(std::move(_map), _parent)) {}
    MapInserter(MapInserter&&) = default;
    MapInserter(const MapInserter&) = default;
};
int main() {
    auto mc = MapInserter<std::string>().
        Insert<int, char, float>("***int, char, float***").
        Insert<float, double>("***float, double***").
        Insert<int>("***int***").
        Insert<unsigned, bool>("***unsigned, bool***");
    Map<decltype(mc)> map(mc);
    std::cout << map.at<int, char, float>() << std::endl; // "***int, char, float***"
    std::cout << map.at<int, char, double>() << std::endl; // "***int, char, float***"
    std::cout << map.at<char>() << std::endl; // "***int***"
    return 0;
}

答案 2 :(得分:0)

template<class...>struct types { typedef types type; };
template<class T, class types>struct type_index;
template<class T, class...Ts>
struct type_index<T,types<T, Ts...>>:
  std::integral_constant<unsigned,0>
{};
template<class T, class T0, class...Ts>
struct type_index<T,types<T0, Ts...>>:
  std::integral_constant<unsigned,type_index<T,types<Ts...>::value+1>
{};

template<template<class>class filter, class types_in, class types_out=types<>, class details=void>
struct filter;

template<template<class>class filter, class T0, class... Ts, class... Zs>
struct filter<filter, types<T0,types...>, types<Zs...>,
  typename std::enable_if< filter<T0>::value >::type
>: filter<filter, types<types...>, types<Zs...,T0>>
{};
template<template<class>class filter, class T0, class... Ts, class... Zs>
struct filter<filter, types<T0,types...>, types<Zs...>,
  typename std::enable_if< !filter<T0>::value >::type
>: filter<filter, types<types...>, types<Zs...>>
{};
template<template<class>class filter, class... Zs>
struct filter<filter, types<>, types<Zs...>,
  void
>: types<Zs...>
{};

template<typename T>
struct convertable_to_test {
  template<typename U>
  using test = std::is_convertible<U, T>;
};

template<class T, class types>
struct get_convertable_to_types:filter< convertable_to_test<T>::template test, types> {};

这是一个开始。

创建系统支持的所有类型的主types<Ts...>。请拨打此SupportedTypes

将上述列表中每种类型偏移的types<Ts...>映射到std::vector<unsigned>。现在,您可以在运行时存储类型集合。将其称为运行时类型向量。

在地图中添加条目types<Args...>时,请在get_convertable_to_types中的每种类型上运行types<Args...>,并在types< types<...>... >中构建一个交叉产品。将生成的指数运行时类型向量存储在实现详细信息映射中。

使用types<Ts...>进行查询时,转换为运行时类型向量,并在实现详细信息映射中查找。做完了!

另一种方法是编写get_convertable_from_types,并在查询点映射到指数个数types<Ts...>,将每个转换为运行时类型向量。将内容添加到地图时,只存储一个运行时类型向量。这具有较慢的查找性能,但设置性能更快,并且使用的内存更少。

我打算完成这件事,但很忙。