2017-12-18 1 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); 
} 

아이디어는, 그것이 내가이 직접 ab을 비교하려고 사용하는 경우 운영자 == 및 그러나 != 를 지원하는 두 개의 인자를 가진 함수 foo를 가지고 아주 간단하다 쓴 코드는 내가 코멘트에 말했듯이 난 A와 B가 먼저 비교 한 후 표준 : : 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')

다음 얻을 std::cout에 문의하십시오. 그래서 내 질문은 : gcc가 내 유형을 추론합니까 std::basic_ostreamint 경우 2? 난 다음 인수

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

+4

'표준 : COUT << (A == (B)보다 더 높은 우선 순위를 가지고 있기 때문에 함께 코드를 컴파일 coliru를 사용);'연산자 우선 순위 등등 때문에 – StoryTeller

+0

오, 그래, 아가씨의 바보 같은 날 – Pumkko

답변