The code compiles in g ++ with a warning, but gives an error for the same code in clang3.1 (Xcode 4.3.3)

The following line compiles successfully in g ++, but gives an error in clang ::

static_assert(tBits <= sizeof(ULONG)*8, "This is IO method"); 

g ++ warning ::

there are no arguments for "static_assert" that depend on the template parameter, so the declaration of "static_assert" must be available

clang error ::

use of undeclared identifier "static_assert"; Did you mean static_cast?

please help me.

Function declaration from comment:

 template < size_t tBits > HRESULT DoIO( std::bitset< tBits >& bitsetToSerialize ) const 
+4
source share
1 answer

"static_assert" was introduced in C ++ 11 as a language keyword - not a function or macro.

Both compilers provide you with warnings / errors "I do not know this function."

In order for the compiler to give you “I don't know this function” when you use “static_assert”, the compiler should not compile with C ++ 11 support (-std = C ++ 11).

To demonstrate this, I took the following code snippet:

 #include <bitset> template<size_t tBits> int DoIO(std::bitset<tBits>& /*bitsetToSerialize*/) { static_assert(tBits <= sizeof(unsigned long) * 8, "tBits is too big."); return tBits; } 

Then I compiled it using GCC 4.7.3 and got the following error:

  osmith@olivia64 ~ / src $ g ++ -o sa.o -c sa.cpp
 sa.cpp: In function 'int DoIO (std :: bitset <_Nb> &)':
 sa.cpp: 6: 78: error: there are no arguments to 'static_assert' that depend on a template parameter, so a declaration of 'static_assert' must be available [-fpermissive]
 sa.cpp: 6: 78: note: (if you use '-fpermissive', G ++ will accept your code, but allowing the use of an undeclared name is deprecated)

Then I compiled it with C ++ 11 support and compiled it without problems:

  osmith@olivia64 ~ / src $ g ++ -std = c ++ 11 -o sa.o -c sa.cpp -Wall
 osmith@olivia64 ~ / src $

So then I compiled it using Clang

  osmith@olivia64 ~ / src $ clang ++ -o sa.o -c sa.cpp
 sa.cpp: 6: 9: error: use of undeclared identifier 'static_assert';  did you mean 'static_cast'?
         static_assert (tBits <= sizeof (unsigned long) * 8, "tBits is too big.");
         ^
 1 error generated.

and finally, I compiled it using Clang with C ++ 11 support, where it was compiled.

  osmith@olivia64 ~ / src $ clang --version
 Ubuntu clang version 3.2-1 ~ exp9ubuntu1 (tags / RELEASE_32 / final) (based on LLVM 3.2)
 Target: x86_64-pc-linux-gnu
 Thread model: posix
 osmith@olivia64 ~ / src $ clang ++ -std = c ++ 11 -o sa.o -c sa.cpp
 osmith@olivia64 ~ / src $

To be sure, let the compiler help us and enable "-Wall":

  osmith@olivia64 ~ / src $ g ++ -Wall -o sa.o -c sa.cpp
 sa.cpp: 6: 9: warning: identifier 'static_assert' is a keyword in C ++ 11 [-Wc ++ 0x-compat]
 sa.cpp: In function 'int DoIO (std :: bitset <_Nb> &)':
 sa.cpp: 6: 78: error: there are no arguments to 'static_assert' that depend on a template parameter, so a declaration of 'static_assert' must be available [-fpermissive]
 sa.cpp: 6: 78: note: (if you use '-fpermissive', G ++ will accept your code, but allowing the use of an undeclared name is deprecated)
+2
source

Source: https://habr.com/ru/post/1442939/


All Articles