Visual C ++ Unconformable Code Example?

What are some code examples that do not conform to the standard when using Visual C ++? Something that is allowed to compile under visual C ++, but nothing else.

+7
source share
6 answers

You can find all Microsoft language extensions here ; You may also want to look at areas of the language where VC ++ is not standard.

The one that I thought was standard (I noticed this when I turned on the / Za switch) is a "magic lithium value":

char *p; (( int * ) p )++; 
+3
source

In some version of Visual C ++, it is customary to pass a non-constant reference to a temporary object. Something like that:

 void DoSomething(std::string& str); void NonConformantFunction() { DoSomething("Temporary std::string created here"); } 
+2
source

"Something that is allowed to compile under visual C ++, but nothing else"

and

"which are not standards compliant"

don't describe exactly the same thing. The compiler can be completely standard, with extensions unique to this compiler, while non-compliance is something that is clearly prohibited by the standard. There are also a number of “undefined” or “implementation defined” parts of the ISO standard that may interfere with portability without being incompatible. In addition, many supported extensions are supported by other compilers, so examples of one of your limitations, but not the other.

Now that it is said that the main VC ++ extension that would display its non-portable code is all the C ++ / CLI extensions, as well as the .NET Framework class library that requires them.

+2
source

There is an official page on microsoft.com that lists parts where VC ++ is not compatible with the standard. However, another problem is that it is compatible with the standard by default . For example, the default scope for for variables is still incorrect in VC ++ 2010 .

+1
source

I do not have a VC compiler to test this, but if I remember correctly, this will compile in Visual Studio regardless of the commented errors:

 template <typename T> struct base { void foo() { T::type v = 0; // standard requires typename here std::cout << v << std::endl; } }; template <typename T> struct derived : base<T> { void bar() { foo(); // foo() is not dependent this should not compile } }; struct test { typedef int type; }; int main() { derived<test> o; o.bar(); } 
+1
source

One thing that MSVC ++ allows you to do is explicitly specialize templates within a class. For example.

 class X { public: template <typename T> void doStuff(T value); template <> void doStuff<bool>(bool value) { // ..do something specific to bool. } }; 

This compiles in VS, but trying to compile in GCC will give an error telling you that you have an explicit specialization in the field of namespace. The solution for this is to simply drag and drop the specialization.

 class X { public: template <typename T> void doStuff(T value); }; template <> void X::doStuff<bool>(bool value) { // ..do something specific to bool. } 

GCC is correct on this issue, although according to the specification, which states that all explicit specializations must be in the namespace area.

It may be worth noting that in the latter case, you should define your specialization in the header file, not the implementation file, as you usually expect. Both of the compilers mentioned do not comply with the standard that would solve this problem, namely the export keyword specified in the specialization in the implementation file. However, this function is not implemented by most compilers, and there are plans to remove it from the next version of the specification.

+1
source

All Articles