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

Home Posts Topics Members FAQ

Template Metaprogramming

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! ]

Aug 12 '05 #1
21 2060
Protoman wrote:
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.
What seems to be the problem? Read FAQ 5.8, while you're looking for
an answer.
Here it is:

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

template<int n>
class Sqr
{
public:
static const int RET = n*n;
};
template<int n> int Sqr<n>::RET; // definition. required if 'RET' is
// used in the program

Sqr sqr; ^^^^^^^
What is that supposed to do? Define an object 'sqr' of type.. which?
'Sqr' is not a class. It's a class template. In order for it to become
a class, you need to supply the template argument.

Sqr<10> sqr; // for example
int main()
{
cout << sqr<10>::RET << endl;
If 'sqr' is an object, you can't use either <> or :: after it. If it is
not an object, then what is it?

cout << Sqr<10>::RET << endl;
or
cout << sqr.RET << endl;
system ("PAUSE");
return 0;
}
(a) You didn't define the 'RET' static data member. All static data
members need to be defined if they are used outside the class
definition.

(b) You didn't use the static member properly. If you want to use it
through an object, the access is via the . operator (dot), if you
want to use it through the class, then you need the class name
before the scope resolution operator.
By all the websites I've read, this should work absolutely fine.
Please list the URLs so we could include them into the list of web sites
to avoid.
Could
you help me? And, if it's any help, I'm using Bloodshed's Dev-Cpp
v4.9.9.2 compiler.


V
Aug 12 '05 #2
Hi

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

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

int main()
{
cout << sqr<10>::RET << endl;
RET is a static member of Sqr<10>, so this should read:

cout << Sqr<10>::RET << endl;
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! ]

Aug 12 '05 #3

Protoman schreef:
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;
}


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! ]

Aug 12 '05 #4
Protoman wrote:
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;
};
Leave ALL_UPPERCASE for macros, and only for those. Just an advise though.
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).
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.
system ("PAUSE");
Nonportable. Ugly.
std::string dummy;
std::cout << "<enter>\n";
std::getline( std::cin, dummy);
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! ]

Aug 12 '05 #5
"Protoman" <Pr**********@gmail.com> writes:
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;
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.
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.
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! ]

Aug 12 '05 #6

Protoman wrote:
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.


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! ]

Aug 12 '05 #7
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! ]

Aug 12 '05 #8
> 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;
Instead of the "static const int RET" use the typical idiom:
enum { square_value = n*n };
};

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.

int main()
{
cout << sqr<10>::RET << endl;
sqr<10> is again incorrect. Just use Sqr<10>::RET.
system ("PAUSE");
return 0;
}

By all the websites I've read, this should work absolutely fine. Could
Which websites are these?
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! ]

Aug 12 '05 #9

Protoman wrote:
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.
Here it is:

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

template<int n>
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}; };

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).

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.) 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.)
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! ]

Aug 12 '05 #10
"Ulrich Eckhardt" <ec******@satorlaser.com> wrote in message
news:oa************@sadoc.satorlaser-intern.com...
....
system ("PAUSE");


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


Portable, but still ugly. This would suffice:

std::cerr << "\nPress Enter\n";
std::cin.ignore();

- gene
Aug 12 '05 #11
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! ]

Aug 13 '05 #12
On 12 Aug 2005 05:08:13 -0400, Protoman wrote:
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;
};
instead of:
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! ]

Aug 13 '05 #13
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! ]

Aug 13 '05 #14
U=BFytkownik Protoman napisa=B3:
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:
#include <iostream>
#include <cstdlib>
using namespace std;

template<int n>
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! ]

Aug 13 '05 #15
-- Protoman wrote:
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! ]

Aug 13 '05 #16
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! ]

Aug 13 '05 #17
Protoman wrote:
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.


#include <iostream>
using namespace std;

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

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

You can't declare a variable of type Sqr, since Sqr isn't a type.

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

Aug 13 '05 #18
Here; figure this out:

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

template <int n>
struct Sqr
{
enum { square_value = n*n };
};
int main()
{
Sqr<int> sqr;
cout << sqr<10>::square_value << endl;
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Sep 11 '05 #19
OK, how do I calculate the root of a number w/TMP?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Sep 11 '05 #20
Protoman wrote:
OK, how do I calculate the root of a number w/TMP?


Get "C++ Templates" by David Vandevoorde and Nicolai M. Josuttis.

http://www.amazon.com/exec/obidos/tg...l/-/0201734842

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

Sep 12 '05 #21
On Sun, 11 Sep 2005 17:14:18 -0400, Protoman wrote:
OK, how do I calculate the root of a number w/TMP?


Here's the simplest way possible:

//START
template<int i, int dec=-1, int less=-1> struct Sqrt
{
static const int val=Sqrt<i, dec-1, (dec*dec<=i) >::val;
};

template<int i, int dec> struct Sqrt<i, dec, 1>
{
static const int val=dec+1;
};

template<int i> struct Sqrt<i, -1, -1>
{
static const int val=Sqrt<i, i, 0>::val;
};
//END

example:

cout << sqrt<386>::val << endl;
Of course, for large values, it might take a while, and you'll need to set
ypur template recursion depth quite high. You might wish to consider using
a more advanced technique such as binary search or Newton's method.

-Ed

--
(You can't go wrong with psycho-rats.) (er258)(@)(eng.cam)(.ac.uk)

/d{def}def/f{/Times findfont s scalefont setfont}d/s{10}d/r{roll}d f 5/m
{moveto}d -1 r 230 350 m 0 1 179{1 index show 88 rotate 4 mul 0 rmoveto}
for /s 15 d f pop 240 420 m 0 1 3 { 4 2 1 r sub -1 r show } for showpage
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Sep 13 '05 #22

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

Similar topics

0
by: Dave | last post by:
For those who might be so inclined, I was wondering if I might get honest critiques of my first real venture into template metaprogramming. This template metaprogram sorts a list of integers at...
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...
5
by: Mohammad | last post by:
Hi, Is it possible to disable a method of a template class depending on the typename at compile time? thanks!
5
by: Mark Stijnman | last post by:
I am trying to teach myself template metaprogramming and I have been trying to create lists of related types. I am however stuck when I want to make a template that gives me the last type in a...
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...
1
by: Ted | last post by:
I have cross posted this to comp.lang.c++ and to sci.math.num- analysis in the belief that the topic is of interest to some in both groups. I am building my toolkit, in support of my efforts in...
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...
3
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
12
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.