【发布时间】:2021-04-02 23:30:25
【问题描述】:
有人知道下面的不一致吗?其中 gcc 和 clang 在涉及 c++20 概念 时表现不同。
在 gcc 中声明的 concept 基本上可以找到我的自定义 operator==,即使它是在 concept 之后声明的,但对于普通函数来说并非如此(使用用户定义的名称)。而 clang 中的 concept 在这两种情况下都无法找到我的任何声明,除非它们在 concept 之前声明。
主要问题是:“哪个编译器的行为正确?”
注意:如果我的所有声明都在 concept 之前声明,则两个编译器都可以正常工作。
-
输出: /// gcc-10.2 和 gcc-11
EqComparable=1 Comparable=0 -
输出:/// clang-11 和 clang-12
EqComparable=0 Comparable=0
#include <iostream>
#include <cstdlib>
#include <string>
#include <regex>
template< typename T1, typename T2 >
concept EqualityComparable = requires( T1 t1, T2 t2 ) { t1 == t2; };
template< typename T1, typename T2 >
concept Comparable = requires( T1 t1, T2 t2 ) { compare(t1,t2); };
std::string operator==( const std::string&, const std::regex& )
{
return {"hello"};
}
std::string compare( const std::string&, const std::regex& )
{
return {"hello"};
}
int main()
{
std::cout << "EqComparable=" << EqualityComparable<std::string, std::regex> << std::endl;
std::cout << "Comparable=" << Comparable< std::string, std::regex > << std::endl;
}
【问题讨论】:
标签: c++ c++20 c++-concepts