루프 범위 기반하기 때문에 ADL (및 std::
관련 네임 스페이스 인)와 유효 begin(v)
및 end(v)
이 필요합니다, 당신은이를 사용할 수 있습니다
namespace support_begin_end
{
// we use a special namespace (above) to
// contain the using directive for 'std':
using namespace std;
// the second parameter is only valid
// when begin(C()) and end(C()) are valid
template<typename C,
typename=decltype(begin(std::declval<C>()),end(std::declval<C>()))
>
struct impl
{
using type = void; // only here impl
};
// explicitly disable conflicting types here
template<>
struct impl<std::string>;
}
// this uses the above to check for ::type,
// which only exists if begin/end are valid
// and there is no specialization to disable it
// (like in the std::string case)
template<class C,typename = typename supports_begin_end::impl<C>::type>
std::ostream& operator<<(std::ostream& out, const C& v) {
for(auto x : v) out<<x<<' ';
return out;
}
이
Live example
다른 유형이 있음 그러나 범위 기반 루프에 적합합니다. 당신도 그들을 감지 해야할지 모르겠다.
여기서 두 용기/v.begin()
/v.end()
유형을 지원뿐만 아니라 begin(v)
/end(v)
을 지원 타입을 검출하는 갱신 live example이다.
여기에 무슨 일이 일어나고 있는지 조금 설명해 주시겠습니까? 정말 고마워! – lfxgroove
@lfxgroove 업데이트 된 답변이 도움이되는지 확인하십시오. –
확실히, 힙 감사합니다! – lfxgroove