why appear warning's (metaprogramming)?

Discussion in 'C++' started by krey, Jul 11, 2008.

  1. krey

    krey Guest

    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,
     
    krey, Jul 11, 2008
    #1
    1. Advertising

  2. krey

    kwikius Guest

    "krey" <> wrote in message
    news:...
    > 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
     
    kwikius, Jul 11, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    7
    Views:
    288
    Andy Fish
    Feb 26, 2005
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    906
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,978
    Smokey Grindel
    Dec 2, 2006
  4. Disc Magnet
    Replies:
    0
    Views:
    164
    Disc Magnet
    Apr 18, 2011
  5. Andy Fish
    Replies:
    7
    Views:
    131
    Andy Fish
    Feb 26, 2005
Loading...

Share This Page