3

I'm a beginner to C++ and I'm learning about some of the C++11 features. One thing I noticed was that in some parts of the Visual C++ stdlib, the authors used the _NOEXCEPT macro instead of the noexcept keyword. Hovering over the macro says #define _NOEXCEPT noexcept, so I'm a bit confused what the point of it is. What's the difference between the two, and should I prefer one over the other?


EDIT: I just searched for it on GitHub and it looks like clang uses it as well, so it isn't a Visual C++ specific macro.

James Ko
  • 32,215
  • 30
  • 128
  • 239
  • 4
    Looking at your standard library implementation is fine. Do not use it as a guide to how to write your own code. – Benjamin Lindley Nov 21 '15 at 06:01
  • If you toggle c++11 support, will the macro the be defined as (deprecated) [`throw()`](http://en.cppreference.com/w/cpp/language/except_spec)? – decltype_auto Nov 21 '15 at 06:07

2 Answers2

5

_NOEXCEPT is not a standard macro. That's indicated by the leading underscore, which says this belongs to the implementation.

Most probably this macro was defined as throw() until the Visual C++ compiler gained support for the noexcept keyword introduced in C++11.

I.e., that it was originally provided as a means to write code that would be compatible with more than one version of the compiler. In a similar vein one might define a macro like CPPX_NORETURN to use compiler-specific means to indicate a no-return function, where C++ attribute [[noreturn]] is not supported. And so on.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
4

Macros everywhere. There are many different reasons to define macros. A common reason is to write code which can be compiled on different C or C++ compilers with minimum changes.

For example, assume there is a code which should be able to compile in a compiler that doesn't support noexcept. Instead of writing two separate codes, you can use the keyword as a macro:

#define _NOEXCEPT noexcept

Then, for compilers which don't have noexcept you can simply disable _NOECXEPT (even though it could be done automatically with conditional macros).

And, welcome to the C/C++ world...

James Ko
  • 32,215
  • 30
  • 128
  • 239
masoud
  • 55,379
  • 16
  • 141
  • 208