C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template Metaprogramming
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Protoman
Guest





PostPosted: Fri Aug 12, 2005 9:08 am    Post subject: Template Metaprogramming Reply with quote



I've been looking at template metaprogramming. It seems really cool,
make the compiler do most of the work. I have very simple program that
uses TMP,it calculates the square of a number, but it doesn't seem to
work. Here it is:

#include <iostream>
#include <cstdlib>
using namespace std;

template<int n>
class Sqr
{
public:
static const int RET = n*n;
};

Sqr sqr;

int main()
{
cout << sqr<10>::RET << endl;
system ("PAUSE");
return 0;
}

By all the websites I've read, this should work absolutely fine. Could
you help me? And, if it's any help, I'm using Bloodshed's Dev-Cpp
v4.9.9.2 compiler.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
msalters
Guest





PostPosted: Fri Aug 12, 2005 7:41 pm    Post subject: Re: Template Metaprogramming Reply with quote




Protoman schreef:

Quote:
I've been looking at template metaprogramming. It seems really cool,
make the compiler do most of the work. I have very simple program that
uses TMP,it calculates the square of a number, but it doesn't seem to
work. Here it is:

#include <iostream
#include using namespace std;

template class Sqr
{
public:
static const int RET = n*n;
};

Sqr sqr;

int main()
{
cout << sqr<10>::RET << endl;
system ("PAUSE");
return 0;
}

With template metaprogramming, you manipulate types, not objects. Of
course, a type cannot be modified, so it has to be right the first
time.
Your Sqr template is a good example. If you create the type Sqr<10>,
the constant Sqr<10>::RET is set to 100.

However, you try to use Sqr (which isn't a type) to create an object
sqr, and then you try to modify the object inside main. That doesn't
work, but the object simply isn't needed. Use the type Sqr<10>.

HTH,
Michiel Salters


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ulrich Eckhardt
Guest





PostPosted: Fri Aug 12, 2005 7:42 pm    Post subject: Re: Template Metaprogramming Reply with quote



Protoman wrote:

Quote:
I've been looking at template metaprogramming. It seems really cool,
make the compiler do most of the work. I have very simple program that
uses TMP,it calculates the square of a number, but it doesn't seem to
work. Here it is:

#include <iostream
#include using namespace std;

template class Sqr
{
public:
static const int RET = n*n;
};

Leave ALL_UPPERCASE for macros, and only for those. Just an advise though.

Quote:
Sqr sqr;

This is already wrong, you cannot create objects of a tempate type, only
objects of a template-instantiation (I only hope that the terminology is
right).

Quote:
cout << sqr<10>::RET << endl;

As already said, above should not even compile, so I wonder why you post
even more code. However, 'Sqr<10>::RET' is the thing you're looking for.

Quote:
system ("PAUSE");

Nonportable. Ugly.
std::string dummy;
std::cout << " std::getline( std::cin, dummy);

Quote:
And, if it's any help, I'm using Bloodshed's Dev-Cpp
v4.9.9.2 compiler.

That's not a compiler but an IDE. The compiler in use there is GCC.

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Markus Moll
Guest





PostPosted: Fri Aug 12, 2005 7:43 pm    Post subject: Re: Template Metaprogramming Reply with quote

Hi

Protoman wrote:

Quote:
template<int n
class Sqr
{
public:
static const int RET = n*n;
};

Sqr sqr;

Sqr is a template-id, so this makes no sense.

Quote:

int main()
{
cout << sqr<10>::RET << endl;

RET is a static member of Sqr<10>, so this should read:

cout << Sqr<10>::RET << endl;

Quote:
system ("PAUSE");
return 0;
}

By all the websites I've read, this should work absolutely fine.

Where have you been reading?


Markus


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
noahwoo
Guest





PostPosted: Fri Aug 12, 2005 8:10 pm    Post subject: Re: Template Metaprogramming Reply with quote

Maybe your idea can be expressed like this :
(Tested by gcc)
#include <iostream>
#include <cstdlib>
using namespace std;

template<int n>
class Sqr
{
public:
static const int RET = n*n;

};

// Sqr<10> sqr;

int main()
{
cout << Sqr<10>::RET << endl;
system ("PAUSE");
return 0;
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Anthony Williams
Guest





PostPosted: Fri Aug 12, 2005 8:11 pm    Post subject: Re: Template Metaprogramming Reply with quote

"Protoman" <Protoman2050 (AT) gmail (DOT) com> writes:

Quote:
I've been looking at template metaprogramming. It seems really cool,
make the compiler do most of the work. I have very simple program that
uses TMP,it calculates the square of a number, but it doesn't seem to
work. Here it is:

#include <iostream
#include using namespace std;

template class Sqr
{
public:
static const int RET = n*n;
};

Sqr sqr;

Sqr on its own is the name of a template. To create an object, you need a
template specialization like Sqr<10>. For your test, you don't need an object,
though.

Quote:
int main()
{
cout << sqr<10>::RET << endl;

If you create an object sqr above, then you don't need the <10>
here. Alternatively, you can ditch the object and just say Sqr<10> here.

Quote:
system ("PAUSE");
return 0;
}

HTH

Anthony
--
Anthony Williams
Software Developer

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Andy
Guest





PostPosted: Fri Aug 12, 2005 8:12 pm    Post subject: Re: Template Metaprogramming Reply with quote

Quote:
I've been looking at template metaprogramming. It seems really cool,
make the compiler do most of the work. I have very simple program that
uses TMP,it calculates the square of a number, but it doesn't seem to
work. Here it is:

#include <iostream
#include using namespace std;

template class Sqr
{
public:
static const int RET = n*n;

Instead of the "static const int RET" use the typical idiom:
enum { square_value = n*n };

Quote:
};

Sqr sqr;

This is incorrect. Sqr is a class template, you cannot instantiate it.
You must provide the necessary template arguments and instantiate a
template class e.g. "Sqr<10> sqr". Also, global objects are a **bad**
idea.

Quote:

int main()
{
cout << sqr<10>::RET << endl;

sqr<10> is again incorrect. Just use Sqr<10>::RET.

Quote:
system ("PAUSE");
return 0;
}

By all the websites I've read, this should work absolutely fine. Could

Which websites are these?

Quote:
you help me? And, if it's any help, I'm using Bloodshed's Dev-Cpp
v4.9.9.2 compiler.

With the modifications it works on Dev-Cpp 4.9.9.2.


Cheers,
Andy


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Greg
Guest





PostPosted: Fri Aug 12, 2005 8:13 pm    Post subject: Re: Template Metaprogramming Reply with quote


Protoman wrote:
Quote:
I've been looking at template metaprogramming. It seems really cool,
make the compiler do most of the work. I have very simple program that
uses TMP,it calculates the square of a number, but it doesn't seem to
work. Here it is:

#include <iostream
#include using namespace std;

template class Sqr
{
public:
static const int RET = n*n;
};

Sqr sqr;

int main()
{
cout << sqr<10>::RET << endl;
system ("PAUSE");
return 0;
}

By all the websites I've read, this should work absolutely fine. Could
you help me? And, if it's any help, I'm using Bloodshed's Dev-Cpp
v4.9.9.2 compiler.



I don't see how this code could have compiled. The declaration of "sqr"
has as its type "Sqr" but without an int value that is needed as its
template parameter. This line can be deleted moreover since "sqr" is
not needed for the purposes of this example. Just change the output
line to use the template class name:

cout << Sqr<10>::RET << endl1;

will output 100 on any conforming compiler.

Greg


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Martin Bonner
Guest





PostPosted: Fri Aug 12, 2005 8:15 pm    Post subject: Re: Template Metaprogramming Reply with quote


Protoman wrote:
Quote:
I've been looking at template metaprogramming. It seems really cool,
make the compiler do most of the work. I have very simple program that
uses TMP,it calculates the square of a number, but it doesn't seem to
work.
You don't explain what you mean by "doesn't work". Compiler error?

Linker error? Runtime crash? Unexpected output? You should also tell
us what the error message / output is (and in the case of unexpected
output, what you expected).

However in this case, I can guess what the problem is.

Quote:
Here it is:

#include <iostream
#include using namespace std;

template class Sqr
{
public:
static const int RET = n*n;
Note: This is perfectly valid Standard C++, HOWEVER

a) You should reserved all-caps identifiers for preprocessor macros
b) Some older compilers may have problems with this. The work round
would be to use
enum {value=n*n};
Quote:
};

Sqr sqr;
This is your problem. You are trying to declare an object (called

'sqr') of type Sqr. The problem is that 'Sqr' is not a type, it is a
template which defines how to create a large number of types. You
*could* use:

Sqr<10> sqr10;

That would declare an an object (called sqr10) of type Sqr<10>.
However, you don't actually need an object at all (that was the point
of the 'static' on the declaration of RET - in this context static
means "associated with the type, not with an individual object of the
type).

Quote:

int main()
{
cout << sqr<10>::RET << endl;

This is essentially the same problem: you are confused between objects
and templates. You can either write:
Sqr<10>::RET
or
sqr10.RET
(I would prefer the former.)
Quote:
system ("PAUSE");
Note: I trust you realize this is not portable? (It is completely

unrelated to your problem, and is fine for this toy application, but if
you run the application direct from the command line you won't need it
at all.)

Quote:
return 0;
}

By all the websites I've read, this should work absolutely fine.
I think you need to read the websites again (or read better ones)!



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
tony_in_da_uk@yahoo.co.uk
Guest





PostPosted: Sat Aug 13, 2005 1:04 am    Post subject: Re: Template Metaprogramming Reply with quote

What you're trying to do isn't actually template metaprogramming in any
meaningful sense, as metaprogramming involves templates instantiating
themselves or other templates to implement an algorithm, but I'll
ignore that for now and help you get it working as presented.

Perhaps you should look at the compiler error messages? The simplest
way to get what you've done going is to change "sqr<10>::RET" to
"Sqr<10>::RET", and remove the "Sqr sqr;" line (you'd need Sqr<10>
sqr;" there to get the intended effect, but you don't need an instance
of the templated class in order to use a static member.

Cheers, Tony


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Sasko Mateski
Guest





PostPosted: Sat Aug 13, 2005 1:11 am    Post subject: Re: Template Metaprogramming Reply with quote

On 12 Aug 2005 05:08:13 -0400, Protoman wrote:

Quote:
I've been looking at template metaprogramming. It seems really cool,
make the compiler do most of the work. I have very simple program that
uses TMP,it calculates the square of a number, but it doesn't seem to
work. Here it is:

#include <iostream
#include using namespace std;

template class Sqr
{
public:
static const int RET = n*n;
};

instead of:

Quote:
Sqr sqr;

int main()
{
cout << sqr<10>::RET << endl;
system ("PAUSE");
return 0;
}


try just:
int main()
{
cout << Sqr<10>::RET << endl;
system ("PAUSE");
return 0;
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Valery Aronov
Guest





PostPosted: Sat Aug 13, 2005 1:13 am    Post subject: Re: Template Metaprogramming Reply with quote

Try this instead:

....
Sqr<10> sqr;

int main() {
cout << sqr.RET << endl;
system ("PAUSE");
return 0;
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Stefan Chrobot
Guest





PostPosted: Sat Aug 13, 2005 1:19 am    Post subject: Re: Template Metaprogramming Reply with quote

U=BFytkownik Protoman napisa=B3:
Quote:
I've been looking at template metaprogramming. It seems really cool,
make the compiler do most of the work. I have very simple program tha=
t
uses TMP,it calculates the square of a number, but it doesn't seem to
work. Here it is:

I think you meant:

Quote:
#include <iostream
#include using namespace std;

template class Sqr
{
public:
static const int RET = n*n;
};

int main()
{
cout << Sqr<10>::RET << endl;
system ("PAUSE");
return 0;
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Dan McLeran
Guest





PostPosted: Sat Aug 13, 2005 1:24 am    Post subject: Re: Template Metaprogramming Reply with quote

You're declaring a variable of Sqr type with no template arguments.
Here is a solution that works for both MSVC6 and GCC 3.3.1:

#include <iostream>
#include <cstdlib>
using namespace std;

template<int n>
class Sqr
{
public:
typedef enum {RET = n*n} ResultType;

};


int main()
{
cout << Sqr<10>::RET << endl;
system ("PAUSE");
return 0;
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
mlimber
Guest





PostPosted: Sat Aug 13, 2005 1:26 am    Post subject: Re: Template Metaprogramming Reply with quote

-- Protoman wrote:
Quote:
I've been looking at template metaprogramming. It seems really cool,
make the compiler do most of the work. I have very simple program that
uses TMP,it calculates the square of a number, but it doesn't seem to
work. Here it is:

[snip]
Sqr sqr;
[snip]


The problem is here you are trying to declare an instance of the Sqr<>
class without the template type list. You need to specify the
int-to-be-squared in the typename (e.g., Sqr<10>::RET). In fact,
there's not much sense in your example in declaring an instance of the
class Sqr<> at all -- the square is supposed to be calculated and
inlined at compile-time, so the there's no runtime cost.

Cheers!

M


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.