C++概念和std :: cout

为了学习C++概念,我尝试重新创建一个EqualityComparable概念。下面是我写C++概念和std :: cout

#include <iostream> 

template<typename T>

concept bool EqualityComparable = requires(T a, T b)

{

{a == b};

{a != b};

};

void foo(EqualityComparable a, EqualityComparable b)

{

//auto t = a == b;

//std::cout << t; //Case 1 : This compiles

std::cout << a == b; //Case 2 : This does not

}

int main()

{

foo(4,2);

}

这个想法非常简单,那就是有一个函数foo与当我使用我尝试直接在比较ab支持运营商==!= 但是两个参数的代码来电std::cout我得到以下编译器错误

main.cpp: In instantiation of 'void foo(auto:1, auto:1) [with auto:1 = int]': main.cpp:19:12: required from here main.cpp:14:20: error: no match for 'operator==' (operand types are 'std::basic_ostream' and 'int')

正如我在评论说,如果我比较A和b,然后再调用的std ::法院一切工作正常。所以我的问题我:为什么海湾合作委员会推断我的类型是std::basic_ostreamint情况2? 我用coliru以以下的参数

g++ -std=c++1z -O2 -fconcepts -Wall -pedantic -pthread main.cpp && ./a.out

回答:

由于运营商<<比运营商==

Operator precedence

以上是 C++概念和std :: cout 的全部内容, 来源链接: utcz.com/qa/262075.html

回到顶部