运维开发网

C语言 代码在g上编译并带有警告,但在clang3.1(Xcode 4.3.3)上的相同代码中出错

运维开发网 https://www.qedev.com 2020-03-02 08:20 出处:网络 作者:运维开发网整理
以下行在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_
以下行在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

铿锵错误::

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.

最后我使用Clang C 11支持编译它,编译得很好.

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消