Template argument output

Currently, I am facing a problem that I could not solve myself. Basically what I'm trying to do is implement some linq type behavior in C ++.

I will start with the code in my header:

template<typename T, template<class = T> class A,
         template<class = T, template<class=T> class = A> class C>
class queryable
{
public:
    typedef T value_type;
    typedef A<value_type> allocator_type;
    typedef C<value_type, allocator_type> container_type;    // (1)
    typedef queryable<T, A, C> type;
    queryable(container_type const &) { }
    template<typename _Out> queryable<_Out, A, C> select(/* some delegate */);
    // more methods etc
}

And this is how I would like it to be created:

std::vector<int> my_vec;
queryable<std::vector<int> > q(my_vec);

Needless to say, this did not work (there would be no other here :))

Now the weirder part is that even this doesn't work:

std::vector<int> my_vec;
queryable<int, std::allocator, std::vector> q(my_vec);

As you can see (looking at the select function), it’s important for me not just to use something like this:

template<typename T> class queryable;

Any suggestions on how to solve this? And is it possible?

Any help would be appreciated!

EDIT: the errors I get:

../entry.cpp:19:58: error: type/value mismatch at argument 3 in template parameter list for ‘template<class T, template<class> class A, template<class, template<class> class<template-parameter-2-2> > class C> class failproof::collections::queryable
../entry.cpp:19:58: error:   expected a template of type ‘template<class, template<class> class<template-parameter-2-2> > class C’, gottemplate<class _Tp, class _Alloc> class std::vector
../entry.cpp:19:61: error: invalid type in declaration before ‘;’ token

EDIT 2:

, , C 2 , 1 1 (1), C . ?

+5
3

"" , , . , .

vector - . , - vector<int>, . - , , .

, T, , , , , , , . , - -arg -arg-, .

(, vector . , vector<int> vector<int, allocator<int> >, - , -arg .)

- , ideone. Exploder .

typedef list<int> list_of_ints;

Exploder . , Exploder<list_of_ints> :: type_1 - , , int. ( ) - allocator<int> Exploder<list_of_ints> :: type_2.

typedef Exploder<list_of_ints> :: type_2  should_be_an_allocator_int;

, , , , int Exploder< should_be_an_allocator_int > :: type_1, allocator . allocator<double>.

typedef Exploder< should_be_an_allocator_int >
           :: rebind<double> :: type should_be_an_allocator_double;

, list<...,...> , , , .

, , list<int> int double:

Exploder<list_of_ints> :: rebind<double, should_be_an_allocator_double> :: type

, , typeid(...).name() , . , .

( , , - , . , .)

( . , allocator, -, rebind, . , , rebind)

Exploder

. , ideone .

template <class>
struct Exploder;

template<class T, template<class> class Template>
struct Exploder< Template<T> > {
        static const char * description() { return " One-arg template. Arg 1 is a type "; }
        typedef T type_1;
        template <class V>
        struct rebind {
                typedef Template<V> type;
        };
};
template<class T, class U, template<class,class> class Template>
struct Exploder< Template<T,U> > {
        static const char * description() { return " Two-arg template. All args are types, as opposed to being (unapplied) templates. "; }
        typedef T type_1;
        typedef U type_2;
        template <class V,class W>
        struct rebind {
                typedef Template<V,W> type;
        };
};
template<class S, class T, class U, template<class,class,class> class Template>
struct Exploder< Template<S,T,U> > {
        static const char * description() { return " Three-arg template. All args are types, as opposed to being (unapplied) templates. "; }
        typedef S type_1;
        typedef T type_2;
        typedef U type_3;
};
+7

() - , , -

template<typename, typename> class C

( , - , .)

queryable<int, std::allocator, std::vector>

, value_type allocator_type:

template <typename C> class queryable
{
public:
    typedef typename C::value_type value_type;
    typedef typename C::allocator_type allocator_type;
    typedef C container_type;
};

( , , rebind , .)

, typedef iterator const const_iterator; ; , , const_iterator , .

+6

( . vector - , - . vector<int> - , , , , , .)

, , queryable :

queryable< vector<int> > q;

, querable :

template <typename T>
struct queryable;

:

template <typename ValueType, template<class T,class = allocator<T> > class ContainerTemplate>
struct queryable< ContainerTemplate<Contained> > {
        typedef ValueType value_type;
        typedef ContainerTemplate<ValueType> container_type;
        typedef typename container_type :: allocator_type A;
        // typedef ContainerTemplate <WhateverOtherValueTypeYouWish> ...
        // typedef A :: rebind <SomeOtherType> ...
};

, , , ContainerTemplate , . ContainerTemplate - vector list set - .

@MikeSeymour, rebind .

ideone

+1

All Articles