Domanda

Stavo sperimentando con C ++ 0x variadic modelli quando sono incappato in questo problema:

template < typename ...Args >
struct identities
{
    typedef Args type; //compile error: "parameter packs not expanded with '...'
};

//The following code just shows an example of potential use, but has no relation
//with what I am actually trying to achieve.
template < typename T >
struct convert_in_tuple
{
    typedef std::tuple< typename T::type... > type;
};

typedef convert_in_tuple< identities< int, float > >::type int_float_tuple;

GCC 4.5.0 mi dà un errore quando provo a typedef il modello parametri pacco.

In sostanza, vorrei "store" i parametri di confezione in un typedef senza disimballaggio. È possibile? In caso contrario, c'è qualche ragione per cui questo non è consentito?

È stato utile?

Soluzione

Un altro approccio, che è leggermente più generico di Ben, è la seguente:

#include <tuple>

template <typename... Args>
struct variadic_typedef
{
    // this single type represents a collection of types,
    // as the template arguments it took to define it
};

template <typename... Args>
struct convert_in_tuple
{
    // base case, nothing special,
    // just use the arguments directly
    // however they need to be used
    typedef std::tuple<Args...> type;
};

template <typename... Args>
struct convert_in_tuple<variadic_typedef<Args...>>
{
    // expand the variadic_typedef back into
    // its arguments, via specialization
    // (doesn't rely on functionality to be provided
    // by the variadic_typedef struct itself, generic)
    typedef typename convert_in_tuple<Args...>::type type;
};

typedef variadic_typedef<int, float> myTypes;
typedef convert_in_tuple<myTypes>::type int_float_tuple;

int main()
{}

Altri suggerimenti

Penso che la ragione non è consentito è che sarebbe disordinato, e si può lavorare intorno ad esso. È necessario utilizzare inversione di dipendenza e rendere lo struct memorizzare il parametro pacco in un modello di fabbrica in grado di applicare tale parametro pack per un altro modello.

Qualcosa sulla falsariga di:

template < typename ...Args >
struct identities
{
    template < template<typename ...> class T >
    struct apply
    {
        typedef T<Args...> type;
    };
};

template < template<template<typename ...> class> class T >
struct convert_in_tuple
{
    typedef typename T<std::tuple>::type type;
};

typedef convert_in_tuple< identities< int, float >::apply >::type int_float_tuple;

ho trovato l'idea di Ben Voigt molto utile nei miei sforzi. Ho modificato leggermente per renderlo generale non solo tuple. Per i lettori qui potrebbe essere una modifica ovvia, ma può valere la pena mostra:

template <template <class ... Args> class T, class ... Args>
struct TypeWithList
{
  typedef T<Args...> type;
};

template <template <class ... Args> class T, class ... Args>
struct TypeWithList<T, VariadicTypedef<Args...>>
{
  typedef typename TypeWithList<T, Args...>::type type;
};

Il nome TypeWithList deriva dal fatto che il tipo è ora un'istanza con una lista precedente.

Questa è una variante di pulito trucco specializzazione parziale di GManNickG. Nessuna delega, e si ottiene più sicurezza tipo richiedendo l'utilizzo del struct variadic_typedef.

#include <tuple>

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

template<typename... Args>
struct convert_in_tuple {
    //Leaving this empty will cause the compiler
    //to complain if you try to access a "type" member.
    //You may also be able to do something like:
    //static_assert(std::is_same<>::value, "blah")
    //if you know something about the types.
};

template<typename... Args>
struct convert_in_tuple< variadic_typedef<Args...> > {
    //use Args normally
    typedef std::tuple<Args...> type;
};

typedef variadic_typedef<int, float> myTypes;
typedef convert_in_tuple<myTypes>::type int_float_tuple; //compiles
//typedef convert_in_tuple<int, float>::type int_float_tuple; //doesn't compile

int main() {}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top