473,466 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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>::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,
Jul 11 '08 #1
1 1376

"krey" <mc******@gmail.comwrote in message
news:7f**********************************@x41g2000 hsb.googlegroups.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
Jul 11 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

467
by: mike420 | last post by:
THE GOOD: 1. pickle 2. simplicity and uniformity 3. big library (bigger would be even better) THE BAD:
12
by: Dave | last post by:
Would people agree with the statement that to a large degree, using template metaprogramming techniques turns a C++ compiler into a C++ interpreter (but just for the metaprogrammed portions of the...
3
by: wakun | last post by:
Hi there, I am working a project in numerical computation in which iterative method is applied for solving equation. The problem is so big and slow. Few days ago, I found a paper on...
9
by: PengYu.UT | last post by:
Hi, I have the code below this email. I want to replace the last 4 lines with a Metaprogramming loop to get something like the following (I don't know the syntax). Is it possible? for type in...
7
by: Joe | last post by:
Hi, I found a concept named template metaprogramming that can be used in C+ + code at compile-time. I am a beginner at C++. But I am a programmer on the .NET platform. Do you know if template...
5
by: iapx86 | last post by:
My parser project calls for a computed goto (see code below). The C preprocessor delivers the desired result, but is ugly. Template metaprogramming delivers results I do not understand. Can...
9
by: andrew cooke | last post by:
Hi, Thanks for the help a couple of days ago. I completed what I was doing and wrote a summary which I've posted at http://acooke.org/cute/PythonMeta0.html (it's kind of long to post here). I...
16
by: Wilson | last post by:
Hi all, I have an interesting problem that I'm hoping can be solved with metaprogramming, but I don't know how far Python supports code generation (and I don't know if I'm taking the correct...
12
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.