Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why appear warning's (metaprogramming)?

Reply
Thread Tools

why appear warning's (metaprogramming)?

 
 
krey
Guest
Posts: n/a
 
      07-11-2008
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,
 
Reply With Quote
 
 
 
 
kwikius
Guest
Posts: n/a
 
      07-11-2008

"krey" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
SessionId doesn't appear in the Request.Uri, why? Hope Paka ASP .Net 1 05-23-2005 04:17 PM
Why bmp file doesn't appear on Crystal Report for ASP NET? Jorge_Beteta ASP .Net 0 02-03-2004 01:29 AM
<DIV> show/hide...both appear...why oh why? KathyB Javascript 2 09-27-2003 02:42 PM



Advertisments