0

I would like to start using static_assert in the codebase that I work on. Unfortunately, not all C++ compilers support them. In the past, we've used a compile-time assert macro that works reasonably for all the compilers I've tried (gleaned from SO!), but, it gives slightly awkward compile error messages.

We support a large number of compilers, including ones which do not have support for static_assert. Also, because our product is an SDK with source code our customers can recompile it with any compiler that they wish. So, while I could introduce conditional compilation for it in all the compilers we use, it's not really possible for me to do it for any 'unknown' compiler.

Is there some compile-time predefined macro or other facility that is standard across all C++ compilers for determining the availability of static_assert, or, are you just required to 'know' what every compiler supports?

MuertoExcobito
  • 9,741
  • 2
  • 37
  • 78
  • There is no standard predefined macro, you could check `__cplusplus` but that doesn't work on all compilers either. You'll need to check compiler versions or use [`BOOST_STATIC_ASSERT_MSG`](http://www.boost.org/doc/libs/release/doc/html/boost_staticassert.html). – Praetorian May 15 '15 at 18:07
  • If I understood your actual question you want a standard way to perform a static assert with a **nice error message** that works in all C++ compilers that might exist? Well then I suggest you to change the title to something along "How to have a nice static assert error message without in a standard but not C++11 way?". Would bring the correct people to the question including from search engines. – Denilson Amorim May 15 '15 at 18:50
  • @thelink2012 I think the question title is accurate. I would like to use the built-in C++11 static_assert, but I want a standard way of knowing whether it is available or not. I have no reasonable hope that there exists a macro implementation of static_assert that will perform nicely on all compilers. Otherwise, there'd be little motivation to create static_assert in the standard in the first place. – MuertoExcobito May 15 '15 at 18:57
  • Your question doesn't state very obvious requirement that you need to check if `static_assert` keyword is supported or not (if not, you have custom macros(s)). I found this: http://stackoverflow.com/questions/14314018/ You may search for something like "How to check if C/C++ compiler supports a keyword". Sadly C++ standard doesn't have any thing standardized for this. – Ajay May 17 '15 at 14:15

2 Answers2

4

You might consider using Boost's static assert.

Note on the boost website:

Boost libraries are intended to be widely useful, and usable across a broad spectrum of applications.

For this reason, boost usually intentionally lags behind in use of language features. You can find the compiler compatibility list here.


If you must roll your own implementation, then here is a Dr. Dobbs article. If I'm not mistaken, Andrei Alexandrescu wrote about this in his Modern C++ Design.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
  • We can't use boost in my project, so this is not an answer. – MuertoExcobito May 15 '15 at 18:07
  • *boost usually intentionally lags behind in use of language features* - Boost cannot unilaterally introduce language features. If you meant library features, then the reality is quite the opposite. A lot of the features added to the standard library in recent history were first introduced in Boost. – Praetorian May 15 '15 at 18:10
  • @Praetorian I don't think we're really in disagreement. I was writing this in the context of the OP's apprehension of compiler support. – Ami Tavory May 15 '15 at 18:11
  • 1
    Yeah, maybe I did misunderstand your intent. @MuertoExcobito If you cannot use Boost, you can always read their implementation and reinvent the wheel yourself – Praetorian May 15 '15 at 18:16
  • @Praetorian I would like to just use boost, but, there are several reasons why we can't. However, boost doesn't have support for all the compilers that we use (some are proprietary). Looking at what boost does was informative though - they do conditional compilation for static_assert per compiler. So, if they can't figure it out, I'm assuming the answer to my question is just 'no' (which I would accept as an answer). – MuertoExcobito May 15 '15 at 18:33
  • @AmiTavory The Dr. Dobbs article essentially explains how to implement a variant of our current static assertion macro. However, no version of it (that I know about) gives a 'nice' compiler error message on all compilers, which is the motivation to switch to `static_assert` where possible. – MuertoExcobito May 15 '15 at 18:36
1

In C++14, there are feature test macros, which allow you to generalize the use of C++11/14/17 features. For static_assert, the macro is __cpp_static_assert.

If your compiler does not inherently support these (yet), you can define them, based on knowledge of what your compiler does support, but they will be forward compatible with any standardized 'unknown' compiler.

Note: this answer was obtained from a question I asked generalizing this one, to any C++11 feature (Availability of C++11 features). I think there was some confusion about the motivation of this particular case, and the answers given tried to solve providing a nice static assert, more than than the actual question as it was asked (which, they didn't actually do).

Community
  • 1
  • 1
MuertoExcobito
  • 9,741
  • 2
  • 37
  • 78