2017-12-18 94 views
0

为了学习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

+4

'性病::法院<<(A == B高的优先级来编译代码);'因为运营商的优先顺序,所以如此如此 – StoryTeller

+0

噢是的coures,愚蠢的我 – Pumkko

回答