473,851 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ 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++.m oderated. First time posters: Do this! ]

Aug 12 '05 #1
21 2108
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++.m oderated. 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++.m oderated. 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++.m oderated. First time posters: Do this! ]

Aug 12 '05 #5
"Protoman" <Pr**********@g mail.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++.m oderated. 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++.m oderated. 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++.m oderated. 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++.m oderated. 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++.m oderated. First time posters: Do this! ]

Aug 12 '05 #10

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

Similar topics

0
1260
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 compile time. It works, but if my approach is more awkward or "bad" than need be, I'd love to hear suggestions for improvement! #include <iostream>
12
2167
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 code)? It's not a perfect analogy, but it seems to be a reasonable statement...
5
1728
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
3374
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 list. I started by using a linked list of types with templates like: struct MyClass1 {}; struct MyClass2 {}; struct MyClass3 {}; struct NullType {};
7
3584
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 metaprogramming is supported in C# (.NET)? For reference I found it: http://en.wikipedia.org/wiki/Template_metaprogramming. Thanks to all.
1
2394
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 producing high performance C++ code; code that is provably correct. The issue here is orthogonal to the question of expression templates, which at present seem to me to be relatively simple.
5
3625
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 anyone explain why loop unrolling doesn't play well with templates? Or better, can someone submit a code fragment to get desired results? Here are the command-lines I use to generate code: "g++ -DTEMPLATE=0 -o gotofun0 gotofun.cpp" works exactly as...
3
1859
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 Gurtovoy. I’m not doing this because I want to be a Meta Programming guru (because a lot of that stuff looks too crazy for use in the real world). Rather I want to learn heavyweight templates and this is the only
12
3375
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
0
9897
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11019
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10728
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9506
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7073
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5933
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4143
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3179
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.