GCC和Clang在与__builtin_constant_p相关的static_assert方面有所不同
GCC and Clang differ in static_assert related to __builtin_constant_p
我碰巧发现GCC和Clang在编译以下代码时有所不同:
struct Foo
{
int mem = 42;
};
int main()
{
constexpr Foo foo;
static_assert(__builtin_constant_p(foo));
return 0;
}
我用g++ -std=c++17
和clang++ -std=c++17
编译。
特别是
- g++
g++-9 (Homebrew GCC 9.3.0_1) 9.3.0
编译,而 - clang++
Apple clang version 11.0.3 (clang-1103.0.32.62)
编译失败,抱怨
error: static_assert failed due to requirement '__builtin_constant_p(foo)'
static_assert(__builtin_constant_p(foo));
^ ~~~~~~~~~~~~~~~~~~~~~~~~~
我没有发现任何迹象表明__builtin_constant_p
应该有任何区别。
对于__builtin_constant_p
GCC称
您可以使用内置函数__builtin_constant_p来确定一个值在编译时是否为常量。。。
Clang表示
Clang支持许多与GCC语法相同的内置库函数,包括__builtin_nan、__builtine_constant_p、__buintin_choose_expr、__buin.types_compatible_p、__buine_assume_aligned、__sync_fetch_and_add等。
问题:虽然我知道__builtin_constant_p
是编译器扩展,但哪一个应该是正确的?
这两个问题都记录得很差,所以我怀疑你的问题是否有合适的答案。
如果你需要一个变通方法:如果参数不是int,clang似乎会放弃。
所以这是有效的:
struct Foo
{
int mem = 42;
};
int main()
{
constexpr Foo foo;
static_assert(__builtin_constant_p(foo.mem));
return 0;
}
相关文章:
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 收到错误"invalid use of non-static data member 'stu::n' "
- LNK1104:无法打开libpjproject-i386-Win32-vc14-Debug-Static.lib
- 我应该在 C++ 中何时/为什么使用 STATIC?
- 在VS2019项目中集成ImageMagick:x64-windows-static library
- 如何在 assert() 和 static_assert() 之间调度,如果在 constexpr 上下文中依赖?
- 如何处理Boost Spirit X3导致Visual Studio 2019 "static initialization order fiasco"?
- 我关于函数"Assert"的C++代码有问题
- "static char __ = []() -> char"的含义
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- 无法在 DLL 中链接 SDL2-static.lib
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- assert() 在发布模式下充当标识函数吗?
- 如何编写一个接受如下断言消息的自定义断言函数:assert(false) << "assertio
- C++线程"Call to non-static member function without an object argument"
- 出现这种错误的原因是什么"invalid use of non-static data member "
- static是如何使用ClassA::m_variable处理所有类对象的
- LNK2001:未解析的外部符号public:static类std::vector
- static assert - C++ concepts vs static_assert