4

Possible Duplicate:
C++11 Feature Checking

I'm particularly interested in the case of noexcept specifications which seem to have littered the C++11 standard library with the introduction of GCC 4.7. In this case, detecting compiler version is adequate; is that the best mechanism to produce portable code?

#include <system_error>

class server_error_category : public std::error_category
{
  public:
    virtual const char* name () const {  ...  }
    //
    // fails beginning with gcc4.7, 
    // looser throw specifier ...
    // overriding 'virtual const char* std::error_category::name() const noexcept (true)

    ...
};
Community
  • 1
  • 1
Andres Jaan Tack
  • 22,566
  • 11
  • 59
  • 78
  • 5
    [Boost.Config](http://www.boost.org/libs/config/) will define the macro `BOOST_NO_NOEXCEPT` for compilers that do not support `noexcept` specifications. If you're concerned about portability with compilers other than GCC, I'd recommend letting Boost do the hard work for you. – ildjarn Apr 23 '12 at 18:54
  • @ildjam Gar, if only that weren't so recent! – Andres Jaan Tack Apr 23 '12 at 19:43
  • You can do `#if defined(__has_feature) && __has_feature(cxx_noexcept)` and you'll be guaranteed not to use noexcept on compilers that don't implement it. On the other hand you may not get to take advantage of noexcept on compilers that _do_ support it simply because they don't support the feature checking macros... (AFAIK __has_feature only works on Clang) – bames53 Apr 23 '12 at 20:14
  • @bames53 : `__has_feature` is not standardized, it's merely a Clang-specific extension. – ildjarn Apr 23 '12 at 21:06
  • @ildjarn It's true that it's not standardized, but it's the sort of thing that should be a de facto standard. – bames53 Apr 23 '12 at 21:54
  • @bames53 I'm fairly certain it's not available in gcc. I haven't been able to find any docs that say otherwise. – Dan Albert Apr 22 '14 at 23:39
  • @DanAlbert It's not. However the feature has now been standardized in C++ [SD-6](https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations) – bames53 Apr 23 '14 at 03:01
  • @bames53 That paper describes an alternative to `__has_feature()` where each feature has its own macro in the format `__cpp_FEATURE_NAME`. Those are also only recommendations for addition to the next standard; they may be rejected. – Dan Albert Apr 23 '14 at 03:57
  • @DanAlbert they are not recommendations for addition to the language standard: there are no plans to include them in ISO/IEC 14882. SD-6 instead stands on its own as the committee's recommendations for feature testing. – bames53 Apr 23 '14 at 13:57

1 Answers1

2

If you just want to check whether noexcept is applied to a particular method in compile-time (instead of preprocessing-time), you could simply use the noexcept operator.

noexcept(std::declval<std::error_category>().name())
// return 'true' if the expression is 'noexcept'

and then you could make the method conditionally noexcept using the syntax

virtual const char* name () const
     noexcept(noexcept(std::declval<std::error_category>().name()))
{ ... }

You can't check whether a library supports noexcept is preprocessing time. Version checking is the only method.

But you could just throw in the noexcept anyway. It is valid for the subclass's override to be noexcept even if the base class is not.

virtual const char* name () const noexcept { ... }
// valid even in 4.6.

(Note that Boost.Config doesn't really help because noexcept is supported since 4.6 in the language, but the library usage appears in 4.7.)

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005