nemequ / hedley

A C/C++ header to help move #ifdefs out of your code

Home Page:https://nemequ.github.io/hedley/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HEDLEY_STATIC_ASSERT: static_assert() expects 2 arguments in C++ <17

aaost opened this issue · comments

Hey, there. Evaluating with gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.11)
__cplusplus version = 201402

Getting these errors:

hedley.h:1323:65: error: expected ‘,’ before ‘)’ token
 #  define HEDLEY_STATIC_ASSERT(expr, message) static_assert(expr)

hedley.h:1323:65: error: expected string-literal before ‘)’ token
 #  define HEDLEY_STATIC_ASSERT(expr, message) static_assert(expr)

According to the static_assert() documentation the function requires 2 arguments until C++17 (and this feature isn't implemented in GCC until v6 according to GCC docs).

Line 1322 in hedley.h is #elif defined(__cplusplus) && (__cplusplus >= 201103L) but should probably be #elif defined(__cplusplus) && (__cplusplus >= 201703L)
Changing to 201703L fixed the build for me. Wanted to pass this by you to make sure this is indeed a typo and not just a peculiarity of my environment.

Thanks for reporting this. I don't have a copy of the C++ standard before C++17, but I did find a C++14 draft online and it looks like cppreference is right.

Wouldn't it be better to add the argument instead of ifdefing out the static_assert? Something like

diff --git a/hedley.h b/hedley.h
index 75c288e..275a7c0 100644
--- a/hedley.h
+++ b/hedley.h
@@ -1329,7 +1329,7 @@ HEDLEY_DIAGNOSTIC_POP
   (defined(__cplusplus) && HEDLEY_TI_VERSION_CHECK(8,3,0))
 #  define HEDLEY_STATIC_ASSERT(expr, message) static_assert(expr, message)
 #elif defined(__cplusplus) && (__cplusplus >= 201103L)
-#  define HEDLEY_STATIC_ASSERT(expr, message) static_assert(expr)
+#  define HEDLEY_STATIC_ASSERT(expr, message) static_assert(expr, message)
 #else
 #  define HEDLEY_STATIC_ASSERT(expr, message)
 #endif

Does that work for you?

Thanks for taking a look so quickly. It looked like there were two options:

  1. (my suggestion) 201703L onwards: use the new version of static_assert() with the default message
  2. (your suggestion) 201103L onwards: continue using the 2011 version of static_assert() with two arguments

But now that I think more about it your solution is the correct one is since it doesn't discard the passed in message and I can confirm that adding the argument also fixes the compilation in C++14.

Okay, that makes sense; I was worried I was missing something. Hedley is due for a release anyways, I'll try to do the proper testing and get one out over the weekend, meanwhile I've gone ahead and pushed the fix to the dev branch.

Thanks again for reporting this!

Yeah, no problem, thanks for the fix!

@nemequ FWIW, this is where I look to find free version of the standard. The "Tim Song" links at the bottom are good: the C++14 version is N4140 which is basically "better" than the paid version of C++14: it's not a working draft but rather full C++14 plus subsequent "editorial" changes.

AFAIK the C standards are totally free these days?