954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

NoDuplicates of “modern c++ design"

I don't my deduction of NoDuplicates are correct or not
please correct my error if I did

template<typename TList> struct NoDuplicates;

template<>
struct NoDuplicates<NullType>
{
  typedef NullType Result;
};

template<typename Head, typename Tail>
struct NoDuplicates<Typelist<Head, Tail> >
{
  //private :
    typedef typename NoDuplicates<Tail>::Result L1;
    typedef typename Erase<L1, Head>::Result L2;

  public :
    typedef Typelist<Head, L2> Result;
};

Example : Typelist > >
The idea of Noduplicates is very simple

NoDuplicates will extract the Tail of Typelist(L1)
and compare with the Head of the Typelist
erase the Type same as Head one time if there were and get a new type(L2)
Then we will get a new

Typelist = Typelist<Head, L2>

In this example it should be

Typelist<T1, Typelist<T2, NullType> >

Then NoDuplicates will run again until the end of the types

The idea is straight forward, but we have to accomplish it by the way of recursive
for simplicity, I would reduce the template parameters to two
Example : Typelist >

(1)

typedef typename NoDuplicates<Typelist<T1, NullType> >::Result L1;
typedef typename Erase<L1, T1>::Result L2;
typedef typename Typelist<T1, L2> Result;


(2)

typedef typename NoDuplicates<NullType>::Result L1 == NullType
typedef typename Erase<NullType, T1>::Result L2 == NullType
typedef typename Typelist<T1, NullType> Result

back to (1)

L1 == Typelist<T1, NullType>
typedef typename Erase<Typelist<T1, NullType>, T1>::Result L2 == NullType
typedef typename Typelist<T1, NullType> Result;


Do I make any mistake?
Could I find some good books talk about variadic template
vs old times TMP?

Thanks

stereomatching
Posting Whiz in Training
260 posts since Sep 2010
Reputation Points: 28
Solved Threads: 10
 

Looks good to me.

Test it out to make sure it's working.

With variadic templates (using the earlier 'indexof'):

template< typename T > struct unique ;

template<> struct unique< typelist<> > { typedef typelist<> type ; } ;

template< typename FIRST, typename ...REST > struct unique< typelist<FIRST,REST...> >
{
    typedef typename unique< typelist<REST...> >::type urtype ;
    typedef typename std::conditional<
                        indexof< FIRST, typelist<REST...> >::value == -1,
                        typename push_front< FIRST, urtype >::type, urtype >::type type ;
};
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

Thanks a lot, I learn a lot of things from you guys

stereomatching
Posting Whiz in Training
260 posts since Sep 2010
Reputation Points: 28
Solved Threads: 10
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You