2016-06-24 3 views
2

Bezug zu this question ich mich gefragt, ob so etwas wie dies auf einfache Art und Weise boost erreichbar wäre :: hana:boost :: hana Tupel Auspacken für variadische Vorlage Instanziierung

#include <boost/hana.hpp> 
#include <boost/hana/unpack.hpp> 

namespace hana = boost::hana; 

template<typename ... T> 
struct A {}; 

int main() { 

    auto my_tuple = hana::tuple_t<int, double, float>; 

    // Is there any way to unpack my_tuple as template arguments for A? 
    // Something like 
    using MyStruct = A<hana::unpack_types(my_tuple)...>; 

    static_assert(std::is_same<MyStruct, A<int, double, float>>::value, "Ooops!"); 
} 
+0

Mögliche Duplikate von http://StackOverflow.com/a/7858971/5922757 – Jezor

Antwort

7

Verwenden template_A in eine metafunction zu heben, dann unpack nennen:

using MyStruct = decltype(hana::unpack(my_tuple, hana::template_<A>))::type; 

Example.

1

Sie es mit sich selbst tun können:

template <template <typename...> class C, typename Tuple> struct RebindImpl; 

template <template <typename...> class C, typename ... Ts> 
struct RebindImpl<C, hana::tuple_t<Ts...>>{ 
    using type = C<Ts...>; 
}; 

template <template <typename...> class C, typename Tuple> 
using Rebind = typename RebindImpl<C, Tuple>::type; 
+0

Dies wird nicht tun, was Sie erwarten! 'hana :: tuple_t ' wird als funktional äquivalent zu 'hana :: make_tuple (hana :: type {} ...)' angegeben, außer mit einem möglicherweise anderen Typ. Also zuerst, du weißt nicht, welchen Typ du bekommst, und zweitens, selbst wenn du annimmst, es sei genau dasselbe wie hana :: make_tuple (hana :: type {} ...) ', dann wird deine Lösung instanziieren 'C ...>', was nicht das ist, was Sie wollen. –

0

ein einfacher Weg, es zu tun, wenn alles, was Sie einen richtigen Typ zu bekommen ist egal:

template <typename... Args> 
constexpr A<Args...> make_A(hana::tuple_t<Args...>) { 
    return {}; 
} 
Verwandte Themen