Click here to Skip to main content
15,889,808 members
Articles / Programming Languages / VC++

Static Value Lists

Rate me:
Please Sign up or sign in to vote.
4.60/5 (7 votes)
25 Aug 2011CPOL18 min read 23.5K   246   17  
An introduction to advanced template metaprogramming using an explanatory project
// This file is part of the lobster library
// Copyright: Andreas Raczek
// This file is published under the The Code Project Open License (CPOL) 
// See the file "CPOL.html" for the full license governing this code. 
#pragma once

namespace lobster {

namespace static_list {

// Template declaration, necessary to inform gcc
// about variadic template
template<
    typename tail, 
    typename tail::value_type... args
> struct var_list;


// Partial specialization for 2+ values
// variadic template parameter is split into head "v" and tail "args..."
template<
    typename tail,  
    typename tail::value_type v,
    typename tail::value_type... args
> struct var_list<
    tail, 
    v, 
    args...
>
{
    typedef typename var_list<list_item<tail, v>, args...>::type type;  
    
    // This typedef would build a reversed list:
    // typedef typename list_item<var_list<tail, args...>, v>::type type;  
};


// Partial specialization for last value (args... is empty)
template<
    typename tail, 
    typename tail::value_type v> 
struct var_list<
    tail, 
    v
> {
    typedef list_item<tail, v> type;
};



}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions