当使用该requires子句的类模板的成员在类外定义时,gcc如果requires未指定,则clang不会抱怨,而会。
考虑下面的代码片段:
#include <concepts>
template<typename Container>
requires std::integral<typename Container::value_type>
class Foo {
public:
void func();
};
template<typename Container>
void Foo<Container>::func()
{}
Run Code Online (Sandbox Code Playgroud)
编译使用gcc没有抱怨。
同时clang报如下错误:
? clang++ -std=c++2a test.cpp
test.cpp:10:1: error: requires clause differs in template redeclaration
template<typename Container>
^
test.cpp:4:19: note: previous template declaration is here
requires std::integral<typename Container::value_type>
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
如果我改变定义如下:
template<typename Container>
requires std::integral<typename Container::value_type>
void Foo<Container>::func()
{}
Run Code Online (Sandbox Code Playgroud)
现在clang不抱怨了。
输出gcc --version: …
(最初与此问题分开。)
在下面的代码片段中,
#include <concepts>
template<
typename T,
typename value_type = typename T::value_type
>
concept check_type = std::default_initializable<T>;
struct Foo {};
template<check_type T>
void func(T t) {}
int main()
{
Foo foo;
func(foo);
}
Run Code Online (Sandbox Code Playgroud)
struct Foo不包含类型别名,value_type但它在 GCC 编译时没有错误。
查看在编译器资源管理器上测试的结果。
但是,使用 Clang 时,它会报告以下错误消息:
? clang++ -std=c++20 asdf.cpp
asdf.cpp:17:5: error: no matching function for call to 'func'
func(foo);
^~~~
asdf.cpp:12:6: note: candidate template ignored: constraints not satisfied [with T = Foo]
void func(T t) …Run Code Online (Sandbox Code Playgroud) GCC 和 Clang 都支持一个名为的实现定义函数__builtin_parity,该函数有助于确定数字的奇偶校验。
根据海湾合作委员会的规定:
\n\n\n内置函数:int __builtin_parity (unsigned int x)
\n
\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 返回 x 的奇偶校验,即 x 模 2 中 1 的位数。
这意味着如果 1 位的数量为偶数,则返回 0,如果为奇数,则返回 1。
\n我在编译器资源管理器上测试的 Clang 也是如此。
\n然而,实际的奇偶校验标志然而,当设置的位数为偶数时,设置
\n为什么会这样呢?
\nGCC 实现了P0634R3,它放弃了指定typename类型何时在已知上下文中的需要。
是否也适用concept?
因为下面的代码不能编译:
template<typename T>
concept sample_concept =
std::default_initializable<T::value_type> &&
requires (T t) {
{ t.some_func(std::declval<T::some_type>()) }
-> std::same_as<T::iterator>;
};
Run Code Online (Sandbox Code Playgroud)
但是当我typename在T::value_typeand前面指定时T::iterator,它会编译。