views:

229

answers:

7

Ok, this is a philosophical question, and I would like to hear different opinions. If it is not a secret or trouble, please provide your age, C++ knowledge (scale 0-100, your own opinion), and the amount of years you have been, let's say, closely related to C++.

For a language like C++ the existence of a standard is a must. And good compilers try their best (well, most of the good compilers, at least) to comply. Many compilers have language extensions, some of which are allowed by the standard, some of which are not. Of the latter kind 2 examples:

example1: gcc's typeof

example2: microsoft's compilers allow a pure virtual function declaration to have both a pure-specifier(=0) and a definition (which is prohibited by the standard - let's not discuss why, that's another topic:)

(there are many other examples)

Both examples are useful in the following sense: example1 is a very useful feature which will be available in c++0x under a different name. example2 is also useful, and microsoft has decided not to respect the ban that made no sense.

And I am grateful that compilers provide language extensions that help us developers in our routine. But here's a question: shouldn't there be an option which, when set, mandates that the compiler be as standards compliant as it can, no matter whether they agree with the standard or not. For example visual studio has such an option, which is called disable language extensions. But hey, they still allow example2.

I want everyone to understand my question correctly. It is a GREAT thing that MSVC allows example2, and I would very much like that feature to be in the standard. It doesn't break any compliant code, it does nothing bad. It just isn't standard.

Would you like that microsoft disable example2 when disable language extensions is set to true? Note that the words microsoft, example2, etc. are placeholders :) Why?

Again, just to make sure. The crucial point is: Should a compiler bother to provide a compliant version (optionally set in the settings)(in its limits, e.g. I am not talking about export) for a certain feature when they provide a better alternative that is not standard and is perhaps even a superset of the standard, thus not breaking anything.

OK, hope you want rip me for this kind of discussion. TIA.

+3  A: 

"Not breaking anything" is such a slippery slope in the long run, that it's better to avoid it altogether. My company's main product outlived several generations of compilers (first written in 1991, with RW), and combing through compiler extensions and quiet standards violations whenever it was the time to migrate to a newer dev system took a lot of effort.

But as long as there's an option to turn off or at least warn about 'non-standard extension', I'm good with it. 34, 70, 6.

Cubbi
+1  A: 

How important standards-compliance is depends on what you are trying to achieve.

If you are writing a program that will never be ported outside of its current environment (especially a program that you're not planning to develop/support for a long time) then it's not very important. Whatever works, works.

If you need your program to remain relevant for a long time, and be easily portable to different environments, than you will want it to be standards compliant, since that's the only way to (more or less) guarantee that it will work everywhere.

The trick, of course, is figuring out which situation you are actually in. It's very common to start a program thinking it is a short-term hack, and later on find that it's so useful that you're still developing/maintaining it years later. In that situation your life will be much less unpleasant if you didn't make any short-sighted design decisions at the beginning of the program's lifetime.

Jeremy Friesner
Actually you are right, but you are speaking from the point of view of a developer. We must look at the compiler vendor's point of view
Armen Tsirunyan
@Armen: compilers are built for developers, not compiler venders.
Donotalo
+3  A: 

I would certainly want an option that disables language extensions to disable all language extensions. Why?

  • All options should do what they say they do.
  • Some people need to develop portable code, requiring a compiler that only accepts the standard form of the language.

"Better" is a subjective word. Language extensions are useful for some developers, but make things more difficult for others.

Mike Seymour
+2  A: 

I think that it's critical that a compiler provide a standards-only mode if it wants to be the primary one used while developing. All compilers should, of course, compile standards compliant code, but it's not critical they they don't extend if they don't think of themselves as the primary compiler -- for example, a cross-compiler, or a compiler for a less popular platform that is nearly always ported to, rather than targeted.

Extensions are fine for any compiler, but it would be nice if I had to turn them on if I want them. By default, I'd prefer a standards-only compiler.

So, given that, I expect MSVC to be standards-only by default. The same with gcc++.

Stats: 40, 90, 15

Lou Franco
+4  A: 

Standards compliance is important for the fundamental reason that it makes your code easier to maintain. This manifests in a number of ways:

  • Porting from one version of a compiler to another. I once had to post a 1.2 million-LOC app from VC6 to VC9. VC6 was notorious for being horribly non-Compliant, even when it was new. It allowed non-compliant code even on the highest warning levels that the new compiler rejected at the lowest. If the code had been written in a more compliant way in the first place, this project wouldn't (shouldn't)have taken 3 months.

  • Porting from one platform to another. As you say, the current MS compilers have language extensions. Some of these are shared by compilers on other platforms, some are not. Even if they are shared, the behavior may be subtly different. Writing compliant code, rather that using these extensions, makes your code correct from the word go. "Porting" becomes simply pulling the tree down and doing a rebuild, rather than digging through the bowels of your app trying to figure out why 3 bits are wrong.

  • C++ is defined by the standard. The extensions used by compilers changes the language. New programmers coming online who know C++ but not the dialect your compiler uses will get up to speed more quickly if you write to Standard C++, rather than the dialect that your compiler supports.

John Dibling
_I once had to post a 1.2 million-LOC app from VC6 to VC9._ Oh my.
James McNellis
I think it needs mention that especially with template code it's a good idea to test with at least TWO COMPILERS. In Windows I use g++ for that. Comeu Online also comes in handy for checking validity of small code snippets; however I do not have experience using the full Comeau compiler.
Alf P. Steinbach
I'm happy to say I've never had to port a 1.2 million-LOC app from VC6 to VC9. I can imagine the pain though. ;)
jalf
`<boasting>` I was part of a group that, in a decade, took a project from several 10kLoC on VC6/CW6/GCC2.x to multi-MLoC on VC9/GCC4. I learned to hate VC6 passionately. (And I adored [MWRon](http://mwronstories.blogstream.com/).) `</boasting>`
sbi
Just to correct one minor point: VC 6 was *not* "horribly non-compliant even when it was new". Rather the contrary, when it was new it was unusually good -- in fact, at the time the *only* ones that were noticeably better were the EDG derivatives. The problem was that it remained Microsoft's "current" compiler for ~4 years, starting *just* before the standard was finalized. There was a flurry of activity to finish the standard, a shortly afterwards most other compilers worked really hard on meeting the new standard -- but MS did *nothing*.
Jerry Coffin
@Jerry: to be even more pedantic, when it came out, there *was* no standard, so compliance was a non-issue. However, when the language *was* standardized, the compiler was horribly non-complaint, even if the other compilers were *worse*. :)
jalf
@Alf: good advice -- and welcome, it's good to see you here!
Jerry Coffin
@Jerry: VC6 came out _mere months_ before the standard was ratified. By that time __everybody else had been adding std features for years__. (I distinctly remember Borland doing so in the early 90ies.) By that time __MS had lost their vote in the std committee__, because of not attending for to long; such was their commitment to the C++ std. It took years of the community's enraged shouting until they finally put out a decently std-conforming compiler in 2003. That was five years into the ten years that the first standard was actually expected to last by many at that time.
sbi
Heck, when VC finally made proper scoping for `for` loop variables the default, __that feature was a decade old__. In this industry, that's what's been a century to the steam engine era! There's gazillions of lines of really, really, and I mean _really_, bad C++ code out there, that only ever compiled with VC6, with their owners locked into VC6 forever and absolutely unable to move forward, because they cannot afford to port their multi-MLoC to a std-conforming compiler.
sbi
I remember _spending whole days_ trying to port some new feature in the above mentioned code base to VC6, in the end having to give up on it and having to postpone some feature that would have made bug-free coding easier, until MS finally would bring out a real C++ compiler. (Did I mention I learned to hate VC6 passionately?)
sbi
@sbi: yes, your hatred comes through loud and clear -- and explains (but doesn't excuse) the degree to which you've lost grip on reality with respect to this particular question. I'm well aware (and already stated) that MS failed to update/improve their compiler at a critical time. Nonetheless, when it was new, VC6 was distinctly better than just about any competitor *of the time*, including Borland, gcc, CodeWarrior, Symantec, or Watcom.
Jerry Coffin
@Jerry: VC6 was not exactly the state of the art when it was released. For example, it came with VC5's std lib which didn't even support member templates (although VC6 supported them), making, e.g., the construction of an STL vector from a list's content a serious problem. In other regards, too, GCC, CW, and BCC were way ahead, simply _because they had paid attention to the standard's evolution_ all the time. But even with such things put aside, MS spent half a decade implementing .NET before they finally improved the standard conformance of their compiler. In this business, that's an era.
sbi
@Jerry, @sbi: You seem to be arguing different points. Jerry's side is that the released VC6 product was better than any other released product at that time. I assume this to be true. sbi's side seems to be that regardless of how it compared to the competition it was still a steaming pile, just a better steaming pile (which I agree with) *and* that MS was doing nothing on their side to make it better WRT language conformance. sbi's latter point is clearly true.
John Dibling
@John: In essence, that's it, although I'd insist in there being more std-conforming compilers around in 1998. CodeWarrior was great and even better was their support. (Did I mention [MWRon](http://mwronstories.blogstream.com/) being adorable? `:)`)
sbi
+3  A: 

First, a reply to several comments. The MS VC extension in question is like this:

struct extension { 
    virtual void func() = 0  { /* function body here */ }
};

The standard allows you to implement the pure virtual function, but not "in place" like this, so you have to write it something like this instead:

struct standard { 
    virtual void func() = 0;
};

void standard::func() { ; }

As to the original question, yes, I think it's a good idea for the compiler to have a mode in which it follows (and enforces) the standard as accurately as possible. While most compilers have that, the result isn't necessarily as accurate a representation of the standard as you/I would like.

At least IMO, about the only answer to this is for people who care about portability to have (and use) at least a couple of compilers on a regular basis. For C++, one of those should be based on the EDG front-end; I believe it has substantially better conformance than most of the others. If you're using Intel's compiler on a regular basis anyway, that's fine. Otherwise, I'd recommend getting a copy of Comeau C++; it's only $50, and it's the closest thing to a "reference" available. You can also use Comeau online, but if you use it on a regular basis, it's worth getting a copy of your own.

Not to sound like an EDG or Comeau shill or anything, but even if you don't care much about portability, I'd recommend getting a copy anyway -- it generally produces excellent error messages. Its clean, clear error messages (all by themselves) have saved enough time over the years to pay for the compiler several times over.

Jerry Coffin
Am I the only one that thinks that compliance is necessary not only for porting, but also for students/educational purposes? That is, I read a really difficult passage in the standard, I thought I understood it, now I wanna check it on a real compiler... Agree/Disagree?
Armen Tsirunyan
Any complex, mature software, has user profiles and requirements. Even software in the same category might have different target users and therefore different requirements. If you want to be a good student compiler, then yes, that's a good reason to require compliance. The normal use cases for compilers are creating production software, not checking standard docs.
Lou Franco
That being said, I could imagine a compiler whose only purpose was checking for compliance, giving really great descriptive warnings and errors, and maybe not providing the most optimized code. There are products, like Lint, that do that and don't produce code at all -- they are good for this kind of compliance checking though. I use LLVM's static analyzer for that reason (because I don't trust my compiler to keep me standard)
Lou Franco
@Lou: Great idea. But not producing code at all means it checks only for syntax, and not semantics.
Armen Tsirunyan
@Armen: yes, conformance is useful for learning as well. Not producing code does *not* prevent a static analyzer from checking semantics though; quite the contrary, LLVM's static analyzer spends *most* of its time/effort on semantic analysis (and the same is true with most `lint`s).
Jerry Coffin
Off-topic: Congrats on becoming [Legendary](http://stackoverflow.com/badges/146/legendary)!
James McNellis
@Jerry: Sorry I meant I wouldn't be able to check how the code RUNS, that is, I wouldn't be able to see the program's behavior
Armen Tsirunyan
@James: Thank you. A belated congratulations to you too. For better or worse, I was really busy and didn't get a chance to say anything more promptly...
Jerry Coffin
@Armen: quite true -- to see it run, you clearly need a compiler, not just a static analyzer.
Jerry Coffin
+2  A: 

I think standards compliance is very important.

I always consider source code is more for the human readers than for the machine(s). So, to communicate programmer's intention to the reader, abiding the standard is like speaking a language of lowest common denominator.

Both at home and work, I use g++, and I have aliased it with the following flags for strict standard compliance.

-Wall -Wextra -ansi -pedantic -std=c++98

Check out this page on Strict ANSI/ISO

I am not a standards expert, but this has served me well. I have written STL-style container libraries which run as-is on different platforms, e.g. 32-bit linux, 64-bit linux, 32-bit solaris, and 32-bit embedded OSE.

ArunSaha