Function overloading for a specific type list

I am trying to write adapter code for a list of types. The following is an experimental code for this purpose:

struct null
{
};

template<typename first, typename second>
struct cell
{
    typedef first head;
    typedef second tail;
};

template <
    typename T1 = null, typename T2 = null, typename T3 = null,
    typename T4 = null, typename T5 = null, typename T6 = null,
    typename T7 = null, typename T8 = null, typename T9 = null,
    typename T10 = null, typename T11 = null, typename T12 = null,
    typename T13 = null, typename T14 = null, typename T15 = null,
    typename T16 = null, typename T17 = null, typename T18 = null
>
struct type_list
{
private:
    typedef typename type_list <
        T2, T3, T4, T5, T6, T7, T8, T9, T10,
        T11, T12, T13, T14, T15, T16, T17, T18
    >::type tail;

public:
    typedef cell<T1, tail> type;
};

template<>
struct type_list<>
{
    typedef null type;
};

template<typename T>
void test(T);

#include <cstdio>

template<typename T1, typename T2>
void test(typename type_list<T1, T2>::type)
{
    // won't be instantiated
    printf("type_list<T1, T2>::type\n");
}

template<typename T1, typename T2>
void test(cell<T1, cell<T2, null>>)
{
    printf("cell<T1, cell<T2, null>>\n");
}

int main()
{
    // Below causes compile error when 'void test(cell<T1, cell<T2, null>>)' is absence
    test(type_list<int, int>::type());
}

output:

cell<T1, cell<T2, null>>

I want to use void test(type_list<T1, T2>::type), not void test(cell<T1, cell<T2, null>>), because the first is a little more concise. My question is:

  • What is the specific reason why I cannot use the first?
  • Is there any workaround? (except for using 'cell' directly :()

Of course, type_list is just a wrapper for generating a list of types, so "just erasing :: type after type_list" cannot be an option.

Thank.

+3
source share
1 answer

Availability:

template<typename T>
void test(T);

makes it best match your function call (until you overload the cell). Try changing the function call:

test<int,int>(type_list<int, int>::type());
0
source

All Articles