c++ - 代码在带有警告的 g​​++ 上编译,但在 clang3.1(Xcode 4.3.3) 上给出相同代码的错误

标签 c++ macos

下一行在 g++ 上编译成功,但在 clang::上出错

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

g++ 警告::

there are no arguments to 'static_assert' that depend on a template parameter, so a declaration of 'static_assert' must be available

clang 错误::

use of undeclared identifier 'static_assert'; did you mean 'static_cast'?

请帮帮我。

来自评论的函数声明:

template < size_t tBits >
HRESULT DoIO( std::bitset< tBits >& bitsetToSerialize ) const

最佳答案

“static_assert”作为语言关键字在 C++11 中引入 - 而不是函数或宏。

两个编译器都给你“我不知道这个函数”警告/错误。

要让编译器在您使用“static_assert”时给您“我不知道这个函数”,编译器不得使用 C++11 支持 (-std=c++11) 进行编译。

为了证明这一点,我采用了以下代码:

#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;
}

然后我用 GCC 4.7.3 编译它,我得到了以下错误:

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)

然后我在启用 C++11 支持的情况下编译它并且编译没有问题:

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

所以,然后我用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.

最后我使用支持 C++11 的 Clang 编译了它,它编译得很好。

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 $

只是为了确定,让编译器有机会帮助我们并打开“-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)

关于c++ - 代码在带有警告的 g​​++ 上编译,但在 clang3.1(Xcode 4.3.3) 上给出相同代码的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13135875/

相关文章:

macos - 删除终端中新选项卡的 "Last login"消息

debugging - 如何使用 stackshot 来调试我的应用程序?

python - 为什么当我在 Mac OS X 终端应用程序中键入时不显示标准输入?

ios - 通过 Mac 应用程序向 iPad 应用程序发送通知

c++ - 编译支持 MySQL 的 QuickFIX C++

c++ - 在相同位置测试具有相同值的数组

macos - Mac OS 10.14 Mojave + qt5.5 + gem capybara-webkit

c++ - C++和SFML,没有重载函数 “sf::RenderWindow.draw()”的实例与参数列表匹配

c++ - 字符串前面的 'L' 在 C++ 中意味着什么?

java - C++ 和 Java 性能