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,
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,