SORU
14 Ocak 2011, Cuma


&Genişletmeden quot;mağaza" bir şablon parametresi pack mümkün mü?

C 0x ile bu sorunu sendeledi zaman: variadic şablonları deneme yapıyordum

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 bana şablon parametreleri paketi typedef çalıştığımda bir hata veriyor.

Temelde, "" parametreleri açma olmadan bir typedef pack,. saklamak istiyorum Mümkün mü? Bu yasak için bir neden mi var?

CEVAP
14 Ocak 2011, Cuma


Ben biraz daha genel bir yaklaşım, aşağıdaki gibidir:

#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()
{}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Codecourse

    Codecourse

    3 ŞUBAT 2009
  • DavidParody

    DavidParody

    17 EKİM 2009
  • superflyy88

    superflyy88

    8 ŞUBAT 2009