I am currently doing the following to generate a value at compile time, which works:
//if B is true, m_value = TRUEVAL, else FALSEVAL, T is the value type
template<bool B, class T, T TRUEVAL, T FALSEVAL>
struct ConditionalValue
{
typedef T Type;
Type m_value;
ConditionalValue():
m_value(TRUEVAL)
{}
};
template<class T, T TRUEVAL, T FALSEVAL>
struct ConditionalValue<false, T, TRUEVAL, FALSEVAL>
{
typedef T Type;
Type m_value;
ConditionalValue():
m_value(FALSEVAL)
{}
};
Then you can simply do something like this:
template<class T>
void loadPixels(uint32 _w, uint32 _h, T * _pixels)
{
PixelDataType::Type pixelType = PixelDataType::Auto; //enum I want to set
ConditionalValue<boost::is_same<T, uint8>::value, PixelDataType::Type, PixelDataType::UInt8, PixelDataType::Auto> checker;
pixelType = checker.m_value;
ConditionalValue<boost::is_same<T, uint16>::value, PixelDataType::Type, PixelDataType::UInt16, PixelDataType::Auto> checker2;
pixelType = checker2.m_value;
...
}
I know this example does not make much sense, but I use that code to set the value of an enum at compile time.- So here is my question: Is there something like that in std/boost type traits allready? When browsing through the reference I only found conditional which does almost what I want, but only generates a type, not a value.
EDIT:
Updated example.
Edit2:
I just realized that boost::is_same::value is all I need to solve my problem.- As for the answer to the question: There does not seem to be anything included in std/boost for good reason as pointed out by thiton
EDIT3:
If you are still looking for a solution to create a value at compile time, you can either use my code wich works. If you are looking for something very close to boost/stl Kerrek’s or Nawaz seem to be valid solutions too. If you are looking for a solution that assigns the correct enum at compile time Luc Touraille approach seems to be interesting even though i decided it’s overkill for my situation!
I think it’s the simple answer: Because the operator ?: can select values quite well. Types are harder to select, that’s why boost constructs exist for that. For pathological cases, the boost::mpl magic Luc suggested is fine, but it should be quite rare.