Talk:C++/Archive 5

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
Archive 1 Archive 3 Archive 4 Archive 5 Archive 6 Archive 7 Archive 10

printf()

I'm sick of everyone fighting over the cout declaration in the hello world application. We should stop butting heads over this and come up with a mutually acceptable solution. sprintf() is a nice, unambiguous c++ function that will serve well in the application without leaving room for arguement. —The preceding unsigned comment was added by 207.207.127.254 (talkcontribs).

While sprintf() is a lovely function, we have this bit of text just above the hello.cpp example:
The following is a Hello world program which uses the C++ standard library stream facility to write a message to standard output.
Given that, your sprintf() example doesn't deliver the promised goods.
Atlant 17:46, 30 April 2007 (UTC)
I'd also add: the sprintf code you wrote will fail to compile, and even if it did compile, it would not produce any output. Now, please, stop trying to make a point with these edits and let it go, or I will seek outside help on this. ATren 17:53, 30 April 2007 (UTC)

I accidentally (by habit) wrote sprintf instead of printf. The following is functional, standards conforming, nonredundant code:


The slash was backwards, the lib is fine.

#include <stdio.h>
int main(){
        printf("Hello, world!\n");
        return 0;
}

A one sentence edit to the preface of the hello world application would remove reference to streaming. I am not trying to prove a point, ATren. Please stop following me around reverting my changes and let the rest of Wikipedia decide on this one. Let's call a truce: I won't edit it if you/Xerxesnine doesn't. Let the leveller heads around here take a stab at it. I see no reason not to clear up this ofstream debate once and for all. -Norvig

printf/sprintf is an artifact of old style C streams. C streams are not typesafe, which is why C++ iostreams were developed in the first place. Experts recommend against the use of C streams in C++ code because (a) functions like printf are not extensible to non-primitive types, and (b) the use of variable arguments in printf makes it easy to write code that has the wrong number of arguments, yet still compiles. In fact, recent versions of popular compilers will actually give you deprecation warnings for certain C-streams functions (citing security concerns due to unchecked buffers).
The fact is: C++ streams are appropriate for a C++ hello world program, and there is absolutely nothing wrong with the existing code. Why do you insist on changing it? ATren 18:23, 30 April 2007 (UTC)

As previously stated, the ostream include is senseless, confusing to a newbie, and potentially dangerous in some environments. A hello world application is designed to create a simple framework in which the newbie can "learn by seeing/doing". The current example less clear than the proposed one.


(edit conflict) Shouldn't that be a backslash n instead of a forwardslash n? And cstdio instead of stdio.h? And really, hello,world should use streams because this is the preferred C++ way of doing things, though this isn't necessarily compelling. --Yamla 18:23, 30 April 2007 (UTC)
Suggesting the C version of "hello,world" in the C++ article... this is a troll, right?
The last thing we'd want is for a newbie to "learn by seeing/doing" things wrongly. Therefore we should present best-practice example code, not dumbed-down, lowest-common-denominator, or C-legacy code. If things are not clear to the newbie (OMG OMG why dose it includes ostreams???/) they should be explained by concise comments. SheffieldSteel 03:09, 2 May 2007 (UTC)

Lets try to stay civil in this matter, SheffieldSteel, and not reduce the discussion to mockery. printf is *not* legacy code, as it has many uses in modern day coding, and is arguably more powerful than C++ streams. printf is not the "lowest common denominator", but rather displays "Hello, world!" in a clear, consise, logical manner so someone new to C++ can pick up the syntax and "feel" easily, in a perfectly standards-conforming manner. This is what a hello world application is for. Please restrain yourself from parody/mockery of useful suggestions in the future.

Some programmers prefer printf, but experts still generally recommend the C++ streams. See the C++ FAQ Lite. There is no consensus for changing this hello world to use the old-style C streams - this is an article about C++, not C, and therefore the C++ streams should be presented. It's that simple. You may prefer printf in your code, which is fine, but C++ streams are more appropriate here in a C++ article. ATren 12:13, 2 May 2007 (UTC)
(Observation) You know, there's something ironic about the use of C++ FAQ Lite to argue for the use of C++ streams I/O when the very next FAQ in that list is "Why does my program go into an infinite loop when someone enters an invalid input character?" (which is, essentially, an explanation of how easy it is to screw yourself with C++ streams I/O).
Atlant 12:19, 2 May 2007 (UTC)
But that item describes a misuse of the library - specifically, by not checking the status of the stream, you risk missing an end of file or some other exit condition. This is not an argument against the library, just describing how it is commonly misused. printf can just as easily be misused and cause runtime havoc (e.g. printf("%s",1)). Any library can be misused. ATren 12:28, 2 May 2007 (UTC)

Yes, but some are more easily misused than others. In your printf example, it's much easier to tell why it went wrong. the iostream example took a closer examination before I was able to spot it. The hello world is all about clarity, adherence to the standards, and ease of use. We should apply the principle of least surprise when generating hello world applications for new users. —The preceding unsigned comment was added by 207.207.127.254 (talkcontribs).

How about this example:

printf("%s %c %d, %f, %d, %s, %c %f", ",0,",',',0,0.0,0,"\",',',",'"',0.0)

? Is this easy to tell what's wrong? Especially when it compiles perfectly fine? Any moderately complex use of a library is subject to misuse. But the point here is, this is an article on C++, so C++ streams should be used. That's all. ATren 17:19, 2 May 2007 (UTC)

Yes, but are we advocating the use of " printf("%s %c %d, %f, %d, %s, %c %f", ",0,",',',0,0.0,0,"\",',',",'"',0.0)" in the hello world application, or the use of "printf("hello world")"? A similar, as obfuscated iostream call could easily be created. Please restrict your examples to those that actually matter given the discussion context.

And there is nothing obfuscated or tricky about std::cout << "Hello, World" << std::endl; either. So there is no reason to replace it. ATren 18:01, 2 May 2007 (UTC)

So, you say that printf is at least as understandable as std:cout, and they're both just as simple and safe, and I say that the ofstream include is confusing, and could potentially cause errors in systems that don't protect the file from being double-included. Why shouldn't we change it again?

Because the ostream version is correct, and appropriate. ATren 00:55, 3 May 2007 (UTC)

"Correct and appropriate" doesn't contribute anything to the discussion. Saying "It's right because it's right" will just create an endless arguement, and isn't helpful.

I'm not going to continue to go around in circles in this debate. There is zero consensus for this change, and there's plenty of support for keeping it exactly the way it is. ATren 19:36, 3 May 2007 (UTC)
I just removed another edit from 207.207.127.254 (talk · contribs) to the main article. This was clearly made against consensus (I can see nobody other than that editor arguing for that version), used the compatibility stdio.h include instead of the standard cstdio include or better yet, the C++ include, iostream. And the C function printf instead of std::cout. As this is meant to be a C++ example, we should include a C++ hello, world rather than a C hello, world. If this continues, I will report that editor for vandalism. --Yamla 02:25, 4 May 2007 (UTC)

Sorry for the misunderstanding, Yamla. I didn't mean to vandalize. I was under the impression that I could discuss my ideas here, and, barring legitimate objection, post the change without being prosecuted. I'm not saying anything inflammatory or wrong (except the legacy c include -- dates me I guess!). If there is something else I need to do be more correct, please point me to the policies and I'll abide by them. I know this is a sensitive subject for some reason, but C++ *is* a superset of C, so it's perfectly legitimate to use this simpler example to help new users on their way. I'm not sure what I'm doing wrong here. Why the animosity?

—The preceding unsigned comment was added by 207.207.127.254 (talk)

Thank you Yamla; that was the point I was trying to make. Will try to be more civil in future. SheffieldSteel 03:36, 4 May 2007 (UTC)
I've reported this edit against consensus on AN/I. I am requesting either a block or a semi-protect. ATren 03:49, 4 May 2007 (UTC)

(I am probably going to regret this.) In light of the increasing heat of the exchange above I would like to ask the participants to take a step back and to reconsider the goal of their contributing to wikipedia. If Wikipedia works at all it does so by collaboration, and by reaching for consensus amongst the editors, and by respecting the opinions of other collaborators. However, opinions, even if they appear to reflect the opinion of a majority of collaborators, have to be substantiated by arguments.

That being said, I would like to explain why I would prefer for the "Hello, world!" example provided by a page on the C++ programming language to make use of the features provided by the language. After all, why do we at all bother with providing a "Hello, World!" example? I can think of the following reasons:

  • Tradition
  • It gives readers an example for a small but complete example program with easily understood meaning.
  • It gives readers familiar with other programming language a point for comparison of those languages.

C++ more or less contains all of C, so much so that a C sprintf "Hello, world!" programm is also a C++ "Hello, world!" programm, and I think the article could very well contain a note of this fact when giving the C++ "Hello, world!" programm. By giving a C "Hello, world!", the first two reasons I gave above are satisfied. By giving a std::cout "Hello, world!" I think we could add value to the article for those readers already familiar with C, thereby giving more emphasis to the third reason. — Tobias Bergemann 09:07, 4 May 2007 (UTC)

Tobias Bergemann, I think that was well said, and agree with you. The only point I'd make is I wish people would stop saying sprintf() in this discuission when I think it's been stipulated-to that we all mean printf() ;-).
Atlant 12:15, 4 May 2007 (UTC)

Does anybody here agree with Norvig that a printf should be used instead of std::cout? Does anybody here know of a compiler that would fail to compile code that includes both <iostream> and <ostream>, as Norvig claims? (This compiler, by the way, would be in direct conflict with the standard, as I indicated above). Does anybody here agree that the C++ sample program should be identical to the C sample program, using not a single C++-specific feature? Perhaps I am misjuding consensus here, so I'm just curious if anybody else shares Norvig's views. ATren 14:53, 4 May 2007 (UTC)

Well, if this were my universe, I'd erradicate C++ completely in the hope that a useful object oriented language would arrise to fill the niche. However, accepting for the moment that I can't, I think we should present C++ using typical C++ features, i.e. streams instead of printf(), and use the language in a standard-conforming way, broken compilers be damned. --Stephan Schulz 15:54, 4 May 2007 (UTC)

Thank you all for your calm, informative comments. I'm sorry for making the change earlier, but before it was only ATren, who has a long history of disagreeing with me via statements that is way was "Correct and appropriate", with no other evidence. Not to mention trying to get me banned from Wikipedia for no reason. I misunderstood the consensus, and had no intention of modifying the article against others' will.

That said, I'd like to contribute another point towards my arguement. On a hello world application, ideally each statement has a clear, consise purpose, so that the new user can look at each statement and derive meaning. With the current cout-based example, we include a spurious include to <ostream>, which has no obvious link to the rest of the code, and the new programmer could even delete it without consequence when experimenting with the code. This leaves room for ambiguity as to the purpose of this include, and may confuse the user. The printf example does not have a similar fault, and it's syntax is arguably more understandable.

Thanks for your time. I appreciate constructive discussion.

Most of his comments about me are flat out wrong, and I am prepared to prove it if anyone questions. I tried to help him by guiding him through the rules and regulations here, and he basically told me to leave him alone while he continued to make changes without consensus. I only sought admin help after his last change to the article.
He refuses to acknowledge consensus (at least four of us disagreed with his changes) and continues to make the same arguments over and over again even though nobody agrees with him. No matter how much he tries to demonize me (and earlier, Xercesnine) his actions are not consistent with a new user seeking to contribute in good faith. He started by removing the one line in the entire article that said "don't remove me", then reverted 4 times, then started this endless circular argument, and now he's playing the role of the helpless victim. Sounds like he's gaming the system to me.... ATren 18:20, 4 May 2007 (UTC)

I'm not "gaming the system", I don't even know what that means! The problem with Wikipedia is that *anyone* could've put that line in. I simply assumed that the person who put the line in didn't know what he/she was doing. As previously stated, these were my first edits, before I was aware of user pages, or even discussion pages. I've come a long way. I'm obeying the rules. Please, let's move on to the subject at hand. I don't see this arguement as circular. Can you point to anything circular about it? I think the new users to the debate have definitely contributed some good points, and while I don't agree with them, they definetly hold water. Debates can be fun, ATren, and I normally enjoy them. If you don't wish to contribute anymore, simply stop, and I won't edit anything on the C++ page unless the others agree with me. Thanks!

OK, if you want to continue this debate, let's start with that compiler that won't compile with both <ostream> and <iostream> includes. Which compiler is it? Please specify the compiler brand, target architecture, and version number, so we can investigate for ourselves. ATren 19:16, 4 May 2007 (UTC)
Carry on with the great debate; don't mind me. I just want to say for the record that I think a lot of readers may be coming to C++ from C (I know I did, way back when), and because of that, the more we can contrast hello.cpp with hello.c, the better - as long as the differences are explained, of course. SheffieldSteel 19:32, 4 May 2007 (UTC)
I agree that a C++ hello, world example is appropriate for an article on C++, and that std::cout is the C++ way of doing things, while printf is the C way. That said, having to include ostream is annoying. Still, there's so much going on behind the scenes with any hello, world example in any language that I don't think this is a significant problem. I agree with SheffieldSteel that it is appropriate to show the contrast between C and C++ with the hello, world example. --Yamla 20:03, 4 May 2007 (UTC)
I also concur the existing "hello, world" should remain unchanged, for the above reasons. That makes at least six contributors who share this view (Atren, Yamla, SheffieldSteel, Tobias Bergemann, Atlant, myself). To my knowledge, there is nobody who concurs with 207.207.127.254 (talk contribs). It is my sincere hope that 207.207.127.254 (talk contribs) gives up trying to make a WP:POINT, creates an account, and henceforth contributes constructively to Wikipedia. Xerxesnine 22:05, 4 May 2007 (UTC)
Yes, Xerxesnine, there appears to be a consensus against me. Yet, how am I supposed to know this before bringing it up in the discussion? I understand, and won't bring it up again unless another Wikipedian agrees with me, but I still don't understand your animosity. Everyone else seems to conduct themselves professionally enough, but you/ATren (which I'm beginning to think are the same person) seem to take my point of view personally. Both accuse me of making a point - what point am I trying to make? I don't understand this accusation, and I don't understand you and your sock puppet's constant aggression against me. Like I said, I will respect the consensus and not change the article, but I'm disappointed that those who could have welcomed me to Wikipedia have treated me in such a manner, when the rest have been so kind. —The preceding unsigned comment was added by 207.207.127.254 (talkcontribs). 6 May, 2007
I'm happy to see you have a good attitude towards this, and I hope you've learned a little bit about how Wikipedia works through all of this. ATren 18:20, 6 May 2007 (UTC)
Just for the record, I think it likely that the anonymous user was contributing in good faith. Most likely, he was simply unaware that a consensus already existed for the existing version, backed by information from the official standard and many, many months of us reverting people who think they know better than the official standard. It is, after all, why we specifically leave a comment there telling people not to change it. That said, ignoring that comment probably was not a good plan. --Yamla 22:17, 4 May 2007 (UTC)

Recent changes to "Hello, World" section

I reverted two minor changes to the "Hello, World" program:

  • Reverted the comment "provided to adhere to the standard" - there is already a footnote about the standards conformance, and I think it's cleaner to indicate in the code what the header provides, rather than why it's needed. The current version ("provides std::ostream, std::endl") is also consistent with the comment above ("provides std::cout")
  • Removed the comment about "//" denoting a comment. I guess this would be fine, but let's discuss here before changing the article.

The "Hello, World" section is (for some reason) very contentious, so any change should be discussed before we do it. ATren 15:06, 8 May 2007 (UTC)

To ATren, et al:

   It seems like you think that the C++ article is your personal property, and that all changes need to be rubberstamped by

you before being approved. The comment description in particular looks informative, and there is certainly no controversy over that. I suggest that ATren, Yamla, and Xerxesnine stop editing / discussing the C++ article while there heads cool over the matter, and see what the *actual* consensus is. Your actions amount to censorship, and this should not be tolerated in Wikipedia.

There's no need to get anything rubberstamped. Every editor is free to either discuss changes first, here, and try to obtain consensus, or go ahead and make changes without consensus, but don't be surprised if those edits are reverted. The fact is that various changes have been discussed extensively, and the current consensus version reflects that. SheffieldSteel 22:35, 20 May 2007 (UTC)

using namespace std

Someone has recently changed the code to remove std:: prefixes and add "using namespace std;" to the hello world program. I believe there was weak consensus that using namespace std; was inappropriate (that's my view, as well as Xerxesnine's) so I've reverted it, but perhaps this warrants more discussion.

My view is that experts such as Bjarne Stroustrup and Herb Sutter (and probably others) have advised against using this construct in all but the most specialized cases, e.g. porting a large project from the pre-namespace days. I don't think a 5-line "hello, world" should use a blunt tool that basically disables a core language feature (namespaces) and which several prominent experts advise should only be used for backwards compatibility with old code. I've yet to find an expert who advocates "using namespace std;" for general use. I'd be curious to hear others' views on this matter... ATren 13:54, 24 May 2007 (UTC)

Unfortunately, I learned with "using namespace std;" because I knew no better... It wasn't until quite awhile after I learned C++ that I found out all of the problems associated with using that. I've had to fight an uphill battle ever since against my urge to use this, but now I only infrequently find myself thinking about doing so... that said, I think that we should not be exemplifying things which will (often) have to be unlearned, especially in a "Hello, World!" example. A note could always be added (with a link to another section discussing namespaces, possibly?) to the effect that the std:: is clarifying the namespace and that there are alternate ways of doing the same thing (i.e. using std::cout;...). I do not advocate changing the helloworld example, but for people who are used to seeing it with the "using namespace std;" I think there should be a note to that effect somewhere in the general vicinity... Maybe it will catch someone before they end up like me. — EtherealFlaim 08:30, 26 May 2007 (UTC)

GLR parser is referenced in the article but it is not a link to the GLR parser page on Wikioedia

please make it a link (http://en.wikipedia.org/wiki/GLR_parser)

Thank you for your suggestion. When you feel an article needs improvement, please feel free to make those changes. Wikipedia is a wiki, so anyone can edit almost any article by simply following the Edit this page link at the top. The Wikipedia community encourages you to be bold in updating pages. Don't worry too much about making honest mistakes — they're likely to be found and corrected quickly. If you're not sure how editing works, check out how to edit a page, or use the sandbox to try out your editing skills. New contributors are always welcome. You don't even need to log in (although there are many reasons why you might want to). --Yamla 18:32, 28 May 2007 (UTC)

Hello world program

The hello world program, that must be the minimum program for a language, can be redured. Cause iostream provides the whole class ostream and objects cout and endl, #include <ostream> is completely useless. Besides, it could also be simplified by adding the using namespace std line and not referring to the namespace std each time looked. --201.235.108.196 00:36, 29 May 2007 (UTC)

This has already been discussed. ostream is not completely useless. Just because it works in your particular compiler does not mean that it is correct according to C++ standards. --Yamla 01:08, 29 May 2007 (UTC)
Actually, in the C++ standart all the ostream class is in iostream --201.235.108.196 20:50, 29 May 2007 (UTC)
This is false. See 27.3 in the standard. This has already been pointed out, please do not make misleading claims like this again. --Yamla 21:10, 29 May 2007 (UTC)
I agree with the above anon user. There seems to be quite a bit of consensus gathered against the ostream inclusion. Perhaps you should address this in a way other than a holier-than-thou, backhanded response. Wikipedia is about consensus: if the vast majority is against the ostream inclusion, please just remove it and stop undoing edits to make a point.
Off the top of my head, there are at least 5 registered users who have endorsed the current version, and provided good reasons based on the existing wording of the standard (which is the most definitive reference on C++). On the other hand, a few anon IPs have expressed opposition with the reasoning "it works on my compiler", completely ignoring any argument that omitting the ostream header is incorrect by the wording of the standard. The consensus seems to clearly favor of including the header. ATren 18:45, 29 June 2007 (UTC)

Current usage of the language

Belem tower 09:28, 29 May 2007 (UTC) Would be interessant a link to a list of significative commercial/free software programs done with C++. This could enforce the improvement "Since the 1990s, C++ has been one of the most popular commercial programming languages."

Not really, no. I mean, millions of lines of code and literally thousands of programs have been written in C++. --Yamla 13:08, 29 May 2007 (UTC)
Exactly, take a look at google's results to see how many. --h2g2bob (talk) 21:53, 29 May 2007 (UTC)

Size of executables compared with C

Belem tower 09:28, 29 May 2007 (UTC) Can anyone add a comparative with C of the size of the executables and the reasons of why this is going this way ? Sorry for bad english.

No. This would depend on the compilers and is too platform-specific. --Yamla 13:09, 29 May 2007 (UTC)

What is an Class instance if not an object, or how can an object not occupy memory?

The following bit makes little sense to me, despite many years of object oriented programming in many languages, C++ included:

A very common source of confusion is a subtle terminology issue: because of its derivation from C, in C++ the term object means memory area, just like in C, and not class instance, which is what it means in most other object oriented languages. For example, in both C and C++, the statement int i; defines an object of type int, that is the memory area where the value of the variable i will be stored on assignment

I'm guessing that the author alludes to the fact that many languages only support pointer-to-object. Futher, I'm guessing that the confusion would be the confusion that "pass-by-value" languages such as Java are in fact passing pointers-as-values, and thus modification of the parameters affect the values as viewed by the caller. If I've guessed correctly, I'd like to rewrite that section for clarity, or simply delete it. If my guesses were off, could someone enlighten me as what the above section means? Esben 22:43, 7 July 2007 (UTC)

What is meant is that in the language of the standard all class instances are objects, but not all objects are class instances. — Tobias Bergemann 06:54, 8 July 2007 (UTC)
That would be correct. Since a class can be anything, even a single variable, and is not automatically a pointer to data, all variables and class instances are objects. However since objects can also be variables they are not necessarily classes (the inverse fallacy). Then there is also the principle of assign to class instances and not classes but I don't think this is referring to that. -Andrew flame him | stalk him 12:50, 14 July 2007 (UTC)
Interesting, I wasn't aware that the standard used "object" in that sense. But I must ask... is that really worth mentioning in wikipedia? It seems like an obscure detail in the language specification, rather than something most C++ users must be aware of. E.g, all C++ users I know use "object" as "instance of a class" and "typename" or similar for the general form you mention above. Nor have I ever come across anyone really confused by this issue, despite the claim in the quote. So, I think we should delete the quoted piece. Any protests? (And please tell me if I am going about this in the wrong way, I'm not exactly a battlehardened wikiveteran :) ). Esben 09:19, 20 July 2007 (UTC)
I think I agree that the disctinction between object and object of class type is mostly relevant only when C++ users directly refer to the standard, and therefore confusion will rarely happen. I for one won't revert if you remove the quoted piece.
By the way, and just for future reference, here is a quote of the relevant part of BS ISO/IEC 14882:2003:
1.8 The C++ object model [intro.object]

1/ The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is a region of storage. … An object is created by a definition, by a new-expression or by the implementation when needed. The properties of an object are determined when the object is created. An object can have a name. An object has a storage duration which influences its lifetime. An object has a type. The term object type refers to the type with which the object is created. Some objects are polymorphic; the implementation generates information associated with each such object that makes it possible to determine that object's type during program execution. For other objects, the interpretation of the values found therein is determined by the type of the expressions used to access them.

2/ Objects can contain other objects, called sub-objects. A sub-object can be a member sub-object, a base class sub-object, or an array element. An object that is not a sub-object of any other object is called a complete object.

Re: I'm not exactly a battlehardened wikiveteran — Neither am I, but I think you are doing fine.
Tobias Bergemann 10:04, 20 July 2007 (UTC)
Very well, I have deleted the paragraph, since it as I mentioned above is more confusing than enlightening. Thank you for you help and your quote. —Preceding unsigned comment added by Esben (talkcontribs) 14:28, 31 August 2007 (UTC)