| JREF Homepage | Swift Blog | Events Calendar | $1 Million Paranormal Challenge | The Amaz!ng Meeting | Useful Links | Support Us |
![]() |
|
|
|
|||||||
| Notices |
| Welcome to the JREF Forum, where we discuss skepticism, critical thinking, the paranormal and science in a friendly but lively way. You are currently viewing the forum as a guest, which means you are missing out on discussing matters that are of interest to you. Please consider registering so you can gain full use of the forum features and interact with other Members. Registration is simple, fast and free! Click here to register today. |
|
|
#1 |
|
Graduate Poster
Join Date: Mar 2002
Posts: 1,823
|
C++ template question
I've been messing about with recursive template classes. Here's something I don't get. The code below shows a template with a recursively defined static const member.
Code:
#include <iostream>
using namespace std;
template<int n> struct Recursive {
static const Recursive<n-1> r;
static const int val = n;
public:
Recursive() {
cout << "Recursive<" << n << ">()" << endl;
}
void foo() const {
cout << val << " ";
r.foo();
}
};
template <int n> const Recursive<n-1> Recursive<n>::r;
template<> struct Recursive<0> {
static const int val = 0;
public:
Recursive() {
cout << "Recursive<0>()" << endl;
}
void foo() const {
cout << val << endl;
}
};
int main() {
Recursive<12> rec;
//rec.foo();
}
Code:
Recursive<12>() Code:
Recursive<11>() Recursive<10>() Recursive<9>() Recursive<8>() Recursive<7>() Recursive<6>() Recursive<5>() Recursive<4>() Recursive<3>() Recursive<2>() Recursive<1>() Recursive<0>() Recursive<12>() 12 11 10 9 8 7 6 5 4 3 2 1 0 I don't actually know how to turn off optimisation on gcc (or even if any optimisation is on by default), to test my hypothesis, so I throw myself upon the good offices of you, the C++ street smart reader. I promise to be suitably impressed by your knowledge. |
|
__________________
"When we type away on discussion boards and post comments on our blogs, it feels like we’re sitting outside a pub in the evening sunshine with our attractive, cool friends – but we aren’t. That’s something we used to do before we got addicted to the internet." - Jon Ronson |
|
|
|
|
|
#2 |
|
Director of Hatcheries and Conditioning
Join Date: Jul 2002
Location: Mt Disappointment
Posts: 33,344
|
I suggest you make a human sacrifice to Satan and ask him, since C++ templates are the work of the devil.
|
|
__________________
Continually pushing the boundaries of mediocrity. Everything is possible, but not everything is probable. For if a man pretend to me that God hath spoken to him supernaturally, and immediately, and I make doubt of it, I cannot easily perceive what argument he can produce to oblige me to believe it. Hobbes |
|
|
|
|
|
#3 |
|
Illuminator
Join Date: Nov 2002
Posts: 3,607
|
I think the compiler is doing the right thing.
The C++ standard says, in section 14.7.1, paragraph 1:
Quote:
Still, a_unique_person might not be so far off. Here is section 14.7.3, paragraph 7 of the standard:
Quote:
|
|
|
|
|
#4 |
|
Muse
Join Date: Sep 2003
Location: Rancho Santa Fe
Posts: 734
|
69dodge,
Quite right. I verified the same results with MSFT 2005. Optimization makes no difference, foo() forces recursive instantiation hence the constructors are executed per the standard. Templates are powerful but at times invoke a lot of cursing so unique person may have a point. Never underestimate the power of the dark side. |
|
__________________
Flying's easy. Walking on water, now that's cool. |
|
|
|
|
|
#5 |
|
Nap, interrupted.
Join Date: Aug 2001
Location: a little toolshed
Posts: 18,592
|
Just to summarize:
Originally Posted by AUP
Quote:
Quote:
|
|
__________________
Millions long for immortality who do not know what to do with themselves on a rainy Sunday afternoon. ---Susan Ertz RIP Mr. Skinny |
|
|
|
|
|
#6 |
|
Graduate Poster
Join Date: Mar 2002
Posts: 1,823
|
69dodge, I am indeed impressed. Your explanation makes sense. Thanks.
Templates are indeed scary. But Alexandrescu's Modern C++ Design arrived this morning, and I have ordered Czarnecki and Eisenecker's Generative Programming, so I need to limber up before I tackle them... |
|
__________________
"When we type away on discussion boards and post comments on our blogs, it feels like we’re sitting outside a pub in the evening sunshine with our attractive, cool friends – but we aren’t. That’s something we used to do before we got addicted to the internet." - Jon Ronson |
|
|
|
|
|
#7 |
|
Illuminator
Join Date: Nov 2002
Posts: 3,607
|
|
|
|
|
|
#8 |
|
Nap, interrupted.
Join Date: Aug 2001
Location: a little toolshed
Posts: 18,592
|
I'm sorry, but an object oriented programming language originally designed so that it could be implemented as a preprocessor is just not right.
~~ Paul |
|
__________________
Millions long for immortality who do not know what to do with themselves on a rainy Sunday afternoon. ---Susan Ertz RIP Mr. Skinny |
|
|
|
|
|
#9 |
|
Graduate Poster
Join Date: Sep 2001
Location: Oxford, UK
Posts: 1,833
|
Pual,
Quote:
In terms of templates, they aren't even really an essential part of the object oriented nature of C++. The ability of C++ to be used (effectively) as an object oriented language comes from class inheritance and virtual member functions. Templates are mostly for purposes of code re-use, and are very useful even in a completely non-object oriented context. In fact, most of my C++ programs are procedural, rather than object oriented, but I still use templates quite extensively. In terms of the original Cfront implementation of C++, this was not so much a preprocessor as a code translator. I could be wrong, but my understanding of it is not that C++ was designed so that it could be implemented in terms of a front-end converter to C, but rather it was designed to be a super-set of C, and to be (almost) completely backwards compatible with C. That said, it makes sense that such a language would initially be implemented as a converter to C. Anyway, I don't think templates are so bad. I think that like much of the complaints against C++, the arguments against templates mostly amount to the misguided notion that if a feature is there, you must use it. Anything you can do in C you can do in exactly the same way in C++ (with trivial exceptions of a few details involving type casting). So the only time a C++ programmer should ever find himself doing something differently in C++ than he would in C, is if there is some tangible benefit to doing it that way. If a programmer finds that the way he does things in C++ is somehow not an improvement on how he would do it in C, then the problem lies with the programmer, not the language. Of course, one could argue that C++ is not an ideal object oriented language because it was not designed from the ground up to be object oriented, and if all you do is object oriented programming, then C++ may not be the best choice. But for me, this is actually a strength. C++ is not really so much an object oriented language, but rather a generic language which can be used for object oriented or procedural programming. In principle, any language can be used for either purpose, but they are usually so highly geared towards one as to make using it for the other purpose a complete nightmare. In my opinion, C++ does a very nice job of bridging the gap, allowing for a single language which not only does both jobs very well, but also allows for extremely easy integration of both types of programming into a single project. Dr. Stupid |
|
__________________
A poke in the eye makes Baby Jesus cry. |
|
|
|
|
|
#10 |
|
Director of Hatcheries and Conditioning
Join Date: Jul 2002
Location: Mt Disappointment
Posts: 33,344
|
I think the evidence is in the application. The C++ doco itself admits that the correct use of templates becomes pretty incomprehensible.
|
|
__________________
Continually pushing the boundaries of mediocrity. Everything is possible, but not everything is probable. For if a man pretend to me that God hath spoken to him supernaturally, and immediately, and I make doubt of it, I cannot easily perceive what argument he can produce to oblige me to believe it. Hobbes |
|
|
|
|
|
#11 |
|
Nap, interrupted.
Join Date: Aug 2001
Location: a little toolshed
Posts: 18,592
|
Originally Posted by Stimpy
But I have a stilted view of it anyway, since 90% of my experience with it was trying to write generators that could produce C++ from higher-level specifications. It was hell. ~~ Paul |
|
__________________
Millions long for immortality who do not know what to do with themselves on a rainy Sunday afternoon. ---Susan Ertz RIP Mr. Skinny |
|
|
|
|
|
#12 |
|
Graduate Poster
Join Date: Mar 2002
Posts: 1,823
|
I think they are. Witness the proliferation of container classes before the STL was added to the standard library. Without a base Object class that all other classes extend from (like Smalltalk, ObjC/Cocoa and Java do), containers can't be responsible for memory management of what they contain, as void* types (the rough C-equivalent of a generic 'object') can't call virtual destructors. Templates get round this nicely.
I would welcome being corrected on this point, if there is an alternative. |
|
__________________
"When we type away on discussion boards and post comments on our blogs, it feels like we’re sitting outside a pub in the evening sunshine with our attractive, cool friends – but we aren’t. That’s something we used to do before we got addicted to the internet." - Jon Ronson |
|
|
|
|
|
#13 |
|
Thinker
Join Date: Aug 2003
Location: Reading, UK
Posts: 223
|
|
|
|
|
|
#14 |
|
Thinker
Join Date: Aug 2003
Location: Reading, UK
Posts: 223
|
[quote=Paul C. Anagnostopoulos;2145985I just think C++ is a bit of a hack. First, it had to be implemented as a preprocessor.[/quote]
You are confusing the language with the implementation. It's nonsense to suggest a language is a hack just because it was first implemented as a preprocessor for some other compiler. Objective-C was first implemented as a C preprocessor and that's a lovely language. C itself was first implemented as a Macro-11 preprocessor and the implementation I use now (gcc) is implemented as a preprocessor for whatever assembler runs on your target architecture.
Quote:
Quote:
|
|
|
|
|
#15 |
|
Director of Hatcheries and Conditioning
Join Date: Jul 2002
Location: Mt Disappointment
Posts: 33,344
|
|
|
__________________
Continually pushing the boundaries of mediocrity. Everything is possible, but not everything is probable. For if a man pretend to me that God hath spoken to him supernaturally, and immediately, and I make doubt of it, I cannot easily perceive what argument he can produce to oblige me to believe it. Hobbes |
|
|
|
|
|
#16 |
|
Graduate Poster
Join Date: Apr 2005
Location: The other other place
Posts: 1,589
|
|
|
__________________
And I looked. And behold a green horse, and his name that sat on him was death. ~Tyndale New Testament |
|
|
|
|
|
#17 |
|
Graduate Poster
Join Date: Mar 2002
Posts: 1,823
|
The problem is not inheritance per se, but inheritance used without discipline, in lieu of more appropriate mechanisms being absent from a language. The problem is that inheritance does at least double-duty: code reuse and polymorphism.
MI is ok, as long as you have only one 'proper' class as the base class, and everything else is mixins. That works out tolerably in Python and C++, and at least in the latter you can differentiate between private and public inheritance. But there's no way to enforce such discipline in the language. I think Ruby's approach of single inheritance, but allowing mixins through modules, works the best of all the mainstream languages. |
|
__________________
"When we type away on discussion boards and post comments on our blogs, it feels like we’re sitting outside a pub in the evening sunshine with our attractive, cool friends – but we aren’t. That’s something we used to do before we got addicted to the internet." - Jon Ronson |
|
|
|
|
|
#18 |
|
Thinker
Join Date: Aug 2003
Location: Reading, UK
Posts: 223
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
|
|