Click here to Skip to main content
15,896,557 members
Articles / Programming Languages / C++

Enumerator Lists and Enumerated Arrays

Rate me:
Please Sign up or sign in to vote.
4.60/5 (9 votes)
4 Oct 2011CPOL11 min read 59.2K   464   15  
Establishing a strong binding between enumerations and arrays
// 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 tmp {

#pragma region power implementation for unsigned exponents

template<
    long base, 
    unsigned long exp
> struct power_enum {
    enum {
        value = base * power_enum<base, exp - 1>::value
    };
};


template<long base>
struct power_enum<
    base, 
    0
> {
    enum {
        value = 1
    };
};
#pragma endregion

#pragma region power implementation for signed exponents 

template<
    long base, 
    unsigned long exp, 
    bool negative> 
struct power_double_helper {
    const static double value;
};

template<long base, unsigned long exp, bool negative> 
const double power_double_helper<base, exp, negative>::value = (double) power_enum<base, exp>::value;

template<
    long base, 
    unsigned long exp> 
struct power_double_helper<
    base,
    exp,
    true
> {
    const static double value;
};


template<long base, unsigned long exp> 
const double power_double_helper<base, exp, true>::value = 1.0 / (double) power_enum<base, exp>::value;


template<long base, long exp>
struct power_double {
    const static double value;
};


template<long base, long exp>
const double power_double<base, exp>::value = 
    power_double_helper<
        base,
        exp >= 0 ? exp : -exp,
        exp < 0       
    >::value;
    
#pragma endregion

}

}

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