3

Visual Studio 2010 features a number of extensions (activated by default) / discrepancies with regard to the C++ Standard.

Such discrepancies can be surprising, and elicit a different behavior than other behavior. VS is notably famous for being extremely lax in template code validation, and template code that was accepted and compiled by VS will often be rejected outright by more compliant compilers (CLang, Comeau, icc, gcc, ... to name a few).

The goal of this question is to provide a reference (thus the FAQ tag) for these discrepancies.

Please provide one answer per discrepancy (check for duplicate) and for each:

  • Explain the discrepancy
  • Tell us if it is possible to disable this (and if so, how)
  • Explain the consequences (apart from the mere rejection)

Note: C++0x is the next standard, so avoid listing C++0x extensions, since they'll be standard soon

From @Matteo Italia: Visual Studio Compliance Page

sbi
  • 219,715
  • 46
  • 258
  • 445
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • Are you looking for bugs too? Because there are some bugs that are not extensions. – James McNellis Mar 17 '11 at 17:26
  • 1
    What version if Visual Studio? – AnT stands with Russia Mar 17 '11 at 17:33
  • 1
    You need to specify a version. A lot of people are posting answers with issues that don't occur in VS2010. – Puppy Mar 17 '11 at 17:49
  • @DeadMG, @AndreyT: let's focus on VS2010 as it is more compliant and earlier version market's share should drop gradually. @James: I consider bugs as discrepancies too, yes. They are always annoying for portability. – Matthieu M. Mar 17 '11 at 18:43
  • @Matthieu: If you want an answer for every bug and/or group of bugs... I don't know how to say this politely, but: there is going to need to be a whole lot of answers. :-| – James McNellis Mar 17 '11 at 19:57
  • @James: well, it's a FAQ, so I am mainly interested in the bugs that users are most likely to encounter. Rather than coming up with it up-front, I'll probably come back to this thread to add answers when a bug/discrepancy pop up on SO :) I'd rather not duplicate VS bug database ^^ – Matthieu M. Mar 18 '11 at 07:30
  • I somewhat disagree with this being a C++ FAQ. – sbi May 26 '11 at 18:11
  • Oh, and it seems nobody mentioned missing two-phase lookup. – sbi May 26 '11 at 18:12
  • @sbi: I did, feel free to edit the tags if you wish :) – Matthieu M. May 27 '11 at 06:15
  • @Matthieu: I removed the tag. And, yes, you mentioned it, just not under its name, so I had forgotten about that by the time I had read all answers. `:(` – sbi May 27 '11 at 07:21

5 Answers5

6

First of all, I'd link Microsoft's take on this topic.

All the Microsoft language extensions can be found here; there's also a page where the areas of the language where VC++ is not compliant to the standard are listed.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
4

By default the compiler allows binding a temporary to a non-const reference.

Remedy: use warning level 4

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • Is there no way to disable this one ? I really wonder about this in conjunction with r-value references during overload resolution. – Matthieu M. Mar 17 '11 at 17:33
  • 1
    GThere is the "disable extensions" switch, but that breaks the Windows headers. :-( It works fine with rvalue reference parameters, because that will be a better match for a temporary. – Bo Persson Mar 17 '11 at 17:37
1

Visual C++ does not fully support value initialization (or rather, there are bugs in all current versions of Visual C++, from Visual C++ 2005 through Visual C++ 2010 SP1).

There are several reported bugs about this (see also this answer to another question).

Consequence: some forms of code that should value initialize an object leave the object or some part of the object uninitialized.

Workaround: do not rely on value initialization.

Community
  • 1
  • 1
James McNellis
  • 348,265
  • 75
  • 913
  • 977
0

Discrepancy: Visual Studio does not bind non-dependent names in templates during first evaluation.

The Standard requires two-phases evaluation:

  • First: check basic templates well-formedness, bind non-dependent names (which comprises overload resolution)
  • Second: Instantiation proper

Disable ? It is not subject to any option or switch, it is simply not implemented.

Consequences:

Visual Studio only does the second phase, which affects:

  • Errors in template code are detected at instantiation only, so you'd better instantiate all the templates you write early (think of it as compilation unit test).
  • Missing template or typename keywords are not detected by VS
  • Overloads declared after the`template may be picked up by overload resolution. Not so much of an issue since reverting the include order would produce the same result.
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • That's not true- VS will throw on `typename` keywords. Not `template`, though. – Puppy Mar 17 '11 at 17:33
  • @DeadMG: from memory one could write `typedef T::iterator iterator;` within a class and not be bothered by the compiler. – Matthieu M. Mar 17 '11 at 18:44
  • This is not completely true. However, there are contexts where only a warning will be generated, while standard compliance required an error. – Paul Michalik Mar 26 '11 at 10:41
0

I use a blog as a notebook for non-complicance issues I find in VS2005. I don't see a point in reposting the whole thing here

http://atarasevich.blogspot.com/2008/02/microsoft-vs2005-c-non-compliance.html

http://atarasevich.blogspot.com/2008/02/microsoft-vs2005-c-non-compliance_07.html

http://atarasevich.blogspot.com/2008/02/microsoft-vs2005-c-non-compliance_08.html

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Sorry for not precising the version. I don't know how many people are still using older versions of VS, but I expect most users coming on SO to be using more recent versions (VS 9 / VS 10). – Matthieu M. Mar 17 '11 at 18:45