why appear warning's (metaprogramming)?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • krey

    why appear warning's (metaprogramming)?

    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>::RE T + 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<typenam e List::Tail>::RE T
    ? List::value
    : IsEmpty<typenam e List::Tail>::RE T
    };
    };
    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<type name List::Tail>::RE T
    ? List::value
    : MaxElement<type name List::Tail>::RE T
    };
    };
    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\TES T\template03\ma in.cpp [Warning]
    comparison between `enum Cons<1, End>::<anonymou s>' and `enum
    MaxElement<End> ::<anonymous>'
    85 E:\Croitor Mihail\work\TES T\template03\ma in.cpp [Warning] `Cons<1,
    End>::<anonymou s 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<ty pename
    List::Tail>::RE T)
    ? List::value
    : static_cast<int >(MaxElement<ty pename List::Tail>::RE T)
    };
    };
    all warnings dissappear. Question: why i need hear use type reduction?
    why appear this warnings?
    Thanx,
  • kwikius

    #2
    Re: why appear warning's (metaprogrammin g)?


    "krey" <mcroitor@gmail .comwrote in message
    news:7f0f2f77-b955-4fda-8bf9-5e28b1cdaf7b@x4 1g2000hsb.googl egroups.com...
    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


    Comment

    Working...