why appear warning's (metaprogramming)?

K

krey

Good Day,

after reading nice book "Genering Programming" by Czarnecki &
Eisenecker i tried to extend an example with static lists. I tried to
add a metafunction for calculating max element from list. Obtained
next code (working):

#include <iostream>

/// some code from Czarnecki & Eisenecker
/// static list realisation:
/// Cons<5, Cons<3, Cons<8, Cons<1, End> > > >

const int endValue = ~(~0u >> 1); // smallest int

// End of list
struct End
{
enum { value = endValue };
typedef End Tail;
};

// list's element
template < int value_, class Tail_ = End>
struct Cons
{
enum { value = value_ };
typedef Tail_ Tail;
};

// list's length calculation
template<class List>
struct Length
{
enum {RET = Length<typename List::Tail>::RET + 1 };
};
template<>
struct Length<End>
{
enum { RET = 0 };
};

// checking list
template<class List>
struct IsEmpty
{
enum {RET = false };
};
template<>
struct IsEmpty<End>
{
enum { RET = true };
};

// getting last element
template<class List>
struct Last
{
enum
{
RET = IsEmpty<typename List::Tail>::RET
? List::value
: IsEmpty<typename List::Tail>::RET
};
};
template<>
struct Last<End>
{
enum { RET = endValue };
};

// getting element from concret position
template<class List, int position_>
struct GetAt
{
enum
{
RET = GetAt<typename List::Tail, position_ - 1>::RET
};
};
template<class List>
struct GetAt<List, 1>
{
enum { RET = List::value };
};

// calc. max element
template<class List>
struct MaxElement
{
enum
{
RET = List::value > MaxElement<typename List::Tail>::RET
? List::value
: MaxElement<typename List::Tail>::RET
};
};
template<>
struct MaxElement<End>
{
enum { RET = End::value };
};

int main(int argc, char** argv)
{
typedef Cons<5, Cons<3, Cons< 8, Cons<1, End> > > > List;
std::cout << "list(3) = " <<GetAt<List, 3>::RET << std::endl;
std::cout << "max element = " << MaxElement<List>::RET;
return 0;
}

But gcc compiler get some warnings:
85 E:\Croitor Mihail\work\TEST\template03\main.cpp [Warning]
comparison between `enum Cons<1, End>::<anonymous>' and `enum
MaxElement<End>::<anonymous>'
85 E:\Croitor Mihail\work\TEST\template03\main.cpp [Warning] `Cons<1,
End>::<anonymous enum>' vs `MaxElement<End>::<anonymous enum>'
.... and etc for each comparasion.
After modifying MaxElement metafunction in next mode:
template<class List>
struct MaxElement
{
enum
{
RET = List::value > static_cast<int>(MaxElement<typename
List::Tail>::RET)
? List::value
: static_cast<int>(MaxElement<typename List::Tail>::RET)
};
};
all warnings dissappear. Question: why i need hear use type reduction?
why appear this warnings?
Thanx,
 
K

kwikius

krey said:
all warnings dissappear. Question: why i need hear use type reduction?
why appear this warnings?
Thanx,

IIRC you are only meant to compare enums of the same group. metaprogramming
often abuses enums for its own purposes, basically because they work more
consistently particularly on older compilers. There are also technical
issues with static const ints regarding whether they have addresses (e.g
when passing by const ref), which AFAIK have never been absolutely resolved.

regards
Andy little
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top