You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cpp-conformance-improvements-2017.md
+42-17Lines changed: 42 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,7 @@ translation.priority.ht:
29
29
---
30
30
31
31
# C++ conformance improvements in [!INCLUDE[vs_dev15_md](misc/includes/vs_dev15_md.md)]
32
-
32
+
For improvements in Update Version 15.3, see [Bug fixes in Visual Studio Update Version 15.3](#update_153).
33
33
## New language features
34
34
With support for generalized constexpr and NSDMI for aggregates, the compiler is now complete for features added in the C++14 Standard. Note that the compiler still lacks a few features from the C++11 and C++98 Standards. See [Visual C++ Language Conformance](visual-cpp-language-conformance.md) for a table that shows the current state of the compiler.
## <aname="update_153"></a> Visual Studio 2017 Update Version 15.3
353
353
### Calls to deleted member templates
354
-
In previous versions of Visual Studio, the compiler in some cases would fail to emit an error for calls to a deleted member template. The following code now produces C2280, "'int S<int>::f<int>(void)': attempting to reference a deleted function":
354
+
In previous versions of Visual Studio, the compiler in some cases would fail to emit an error for ill-formed calls to a deleted member template which would’ve potentially caused crashes at runtime. The following code now produces C2280, "'int S<int>::f<int>(void)': attempting to reference a deleted function":
355
355
```cpp
356
356
template<typename T>
357
357
structS {
@@ -366,7 +366,7 @@ decltype(S<int>::f<int>()) i; // this should fail
366
366
To fix the error, declare i as `int`.
367
367
368
368
### Pre-condition checks for type traits
369
-
Visual Studio 2017 Update 1 improves pre-condition checks for type-traits to more strictly follow the standard. One such check is for assignable. The following code produces C2139 in Update 1:
369
+
Visual Studio 2017 Update Version 15.3 improves pre-condition checks for type-traits to more strictly follow the standard. One such check is for assignable. The following code produces C2139 in Update Version 15.3:
370
370
371
371
```cpp
372
372
struct S;
@@ -379,7 +379,7 @@ static_assert(__is_convertible_to(E, E), "fail"); // this is allowed in VS2017 R
379
379
### New compiler warning and runtime checks on native-to-managed marshaling
380
380
Calling from managed functions to native functions requires marshalling. The CLR performs the marshaling but it doesn't understand C++ semantics. If you pass a native object by value, CLR will either call the object's copy-constructor or use BitBlt, which may cause undefined behavior at runtime.
381
381
382
-
Now the compiler will emit a warning if it can know at compile time that a native object with deleted copy ctor is passed between native and managed boundary by value. For those cases in which the compiler doesn't know at compile time, it will inject a runtime check so that the program will call std::terminate immediately when an ill-formed marshalling occurs. In Update 3, the following code produces C4606 "
382
+
Now the compiler will emit a warning if it can know at compile time that a native object with deleted copy ctor is passed between native and managed boundary by value. For those cases in which the compiler doesn't know at compile time, it will inject a runtime check so that the program will call std::terminate immediately when an ill-formed marshalling occurs. In Update Version 15.3, the following code produces C4606 "
383
383
'A': passing argument by value across native and managed boundary requires valid copy constructor. Otherwise the runtime behavior is undefined".
384
384
```cpp
385
385
classA
@@ -410,10 +410,10 @@ int main()
410
410
f(A()); // This call from managed to native requires marshalling. The CLR doesn't understand C++ and uses BitBlt, which will result in a double-free later.
411
411
}
412
412
```
413
-
To fix the error, mark the caller as native to avoid marshalling.
413
+
To fix the error, remove the `#pragma managed` directive to mark the caller as native and avoid marshalling.
414
414
415
415
### Experimental API warning for WinRT
416
-
WinRT APIs that are released for experimentation and feedback will be decorated with `Windows.Foundation.Metadata.ExperimentalAttribute`. In Update 3, the compiler will produce warning C4698 when it encounters the attribute. A few APIs in previous versions of the Windows SDK have already been decorated with the attribute, and calls to these APIs will start triggering this compiler warning. Newer Windows SDKs will have the attribute removed from all shipped types, but if you are using an older SDK, you'll need to suppress these warnings for all calls to shipped types.
416
+
WinRT APIs that are released for experimentation and feedback will be decorated with `Windows.Foundation.Metadata.ExperimentalAttribute`. In Update Version 15.3, the compiler will produce warning C4698 when it encounters the attribute. A few APIs in previous versions of the Windows SDK have already been decorated with the attribute, and calls to these APIs will start triggering this compiler warning. Newer Windows SDKs will have the attribute removed from all shipped types, but if you are using an older SDK, you'll need to suppress these warnings for all calls to shipped types.
417
417
The following code produces warning C4698: "'Windows::Storage::IApplicationDataStatics2::GetForUserAsync' is for evaluation purposes only and is subject to change or removal in future updates":
### Out-of-line definition of a template member function
433
-
Update 3 produces an error when it encounters an out-of-line definition of a template member function that was not declared in the class. The following code now produces error C2039: 'f': is not a member of 'S':
433
+
Update Version 15.3 produces an error when it encounters an out-of-line definition of a template member function that was not declared in the class. The following code now produces error C2039: 'f': is not a member of 'S':
434
434
435
435
```cpp
436
436
structS {};
@@ -451,11 +451,11 @@ void S::f(T t) {}
451
451
```
452
452
453
453
### Attempting to take the address of "this" pointer
454
-
In C++ 'this' is an prvalue of type pointer to X. You cannot take the address of 'this' or bind it to an lvalue reference. In previous versions of Visual Studio, the compiler would allow you to circumvent this restriction by performing a cast. In Update 3, the compiler produces error C2664.
454
+
In C++ 'this' is an prvalue of type pointer to X. You cannot take the address of 'this' or bind it to an lvalue reference. In previous versions of Visual Studio, the compiler would allow you to circumvent this restriction by performing a cast. In Update Version 15.3, the compiler produces error C2664.
455
455
456
456
### Conversion to an inaccessible base class
457
-
Update 3 produces an error when you attempt to convert a type to a base class which is inaccessible. The following code now raises
458
-
error C2243: 'type cast': conversion from 'D *' to 'B *' exists, but is inaccessible:
457
+
Update Version 15.3 produces an error when you attempt to convert a type to a base class which is inaccessible. The compiler now raises
458
+
"error C2243: 'type cast': conversion from 'D *' to 'B *' exists, but is inaccessible". The following code is ill-formed and can potentially cause a crash at runtime. The compiler now produces C2243 when it encounters code like this:
459
459
460
460
```cpp
461
461
#include<memory>
@@ -470,7 +470,7 @@ void f()
470
470
```
471
471
### Default arguments are not allowed on out of line definitions of member functions
472
472
Default arguments are not allowed on out-of-line definitions of member functions in template classes. The compiler will issue a warning under /permissive, and a hard error under /permissive-
473
-
In Update 3, the following code produces warning C5034: 'A<T>::f': an out-of-line definition of a member of a class template cannot have default arguments:
473
+
In previous versions of Visual Studio, the following ill-formed code could potentially cause a runtime crash. Update Version 15.3 produces warning C5034: 'A<T>::f': an out-of-line definition of a member of a class template cannot have default arguments:
474
474
```cpp
475
475
476
476
template <typename T>
@@ -484,10 +484,10 @@ T A<T>::f(T t, bool b = false)
484
484
...
485
485
}
486
486
```
487
-
To fix the error, remove the "= false" default argument.
487
+
To fix the error, remove the "= false" default argument.
488
488
489
489
### Use of offsetof with compound member designator
490
-
In Update 3, using offsetof(T, m) where m is a "compound member designator" will result in a warning when you compile with the /Wall option. The following code produces "warning C4841: non-standard extension used: compound member designator in offseto":
490
+
In Update Version 15.3, using offsetof(T, m) where m is a "compound member designator" will result in a warning when you compile with the /Wall option. The following code is ill-formed and could potentially cause crash at runtime. Update Version 15.3 produces "warning C4841: non-standard extension used: compound member designator in offseto":
491
491
492
492
```cpp
493
493
@@ -508,7 +508,7 @@ constexpr auto off = offsetof(A, arr[2]);
508
508
```
509
509
510
510
### Using offsetof with static data member or member function
511
-
In Update 3, using offsetof(T, m) where m refers to a static data member or a member function will result in an error. The following code produces "error C4597: undefined behavior: offsetof applied to member function 'foo'" and "error C4597: undefined behavior: offsetof applied to static data member 'bar'":
511
+
In Update Version 15.3, using offsetof(T, m) where m refers to a static data member or a member function will result in an error. The following code produces "error C4597: undefined behavior: offsetof applied to member function 'foo'" and "error C4597: undefined behavior: offsetof applied to static data member 'bar'":
512
512
```cpp
513
513
514
514
#include<cstddef>
@@ -525,7 +525,7 @@ Constexpr auto off2 = offsetof(A, bar);
525
525
This code is ill-formed and could potentially cause crash at runtime. To fix the error, change the code to no longer invoke undefined behavior. This is non-portable code that is disallowed by the C++ standard.
526
526
527
527
### New warning on declspec attributes
528
-
In Update 3, the compiler no longer ignores attributes if __declspec(…) is applied before extern "C" linkage specification. Previously, the compiler would ignore the attritbute, which could have runtime implications. When the `/Wall /WX` option is set, the following code produces "warning C4768: __declspec attributes before linkage specification are ignored":
528
+
In Update Version 15.3, the compiler no longer ignores attributes if __declspec(…) is applied before extern "C" linkage specification. Previously, the compiler would ignore the attritbute, which could have runtime implications. When the `/Wall /WX` option is set, the following code produces "warning C4768: __declspec attributes before linkage specification are ignored":
In previous versions of Visual Studio, the compiler did not detect when a call to a deleted destructor occurred in the context of the expression associated with 'decltype'. In Update 3, the following code produces "error C2280: 'A<T>::~A(void)': attempting to reference a deleted function":
542
+
In previous versions of Visual Studio, the compiler did not detect when a call to a deleted destructor occurred in the context of the expression associated with 'decltype'. In Update Version 15.3, the following code produces "error C2280: 'A<T>::~A(void)': attempting to reference a deleted function":
543
543
544
544
```cpp
545
545
template<typename T>
@@ -559,6 +559,31 @@ void h()
559
559
g(42);
560
560
}
561
561
```
562
+
### Unitialized const variables
563
+
Visual Studio 2017 RTW release had a regression in which the C++ compiler would not issue a diagnostic if a 'const' variable was not initialized. This regression has been fixed in Visual Studio 2017 Update 1. The following code now produces "warning C4132: 'Value': const object should be initialized":
564
+
565
+
```cpp
566
+
constint Value;
567
+
```
568
+
To fix the error, assign a value to `Value`.
569
+
570
+
### Empty declarations
571
+
Visual Studio 2017 Update Version 15.3 now warns on empty declarions for all types, not just built-in types. The following code now produces a level 2 C4091 warning for all four declarations:
572
+
573
+
```cpp
574
+
structA {};
575
+
template <typename> struct B {};
576
+
enum C { c1, c2, c3 };
577
+
578
+
int; // warning C4091 : '' : ignored on left of 'int' when no variable is declared
579
+
A; // warning C4091 : '' : ignored on left of 'main::A' when no variable is declared
580
+
B<int>; // warning C4091 : '' : ignored on left of 'B<int>' when no variable is declared
581
+
C; // warning C4091 : '' : ignored on left of 'C' when no variable is declared
582
+
```
583
+
584
+
To remove the warnings, simply comment-out or remove the empty declarations. In cases where the un-named object is intended to have a side effect (such as RAII) it should be given a name.
585
+
586
+
The warning is excluded under /Wv:18 and is on by default under warning level W2.
0 commit comments