operator<<
의 호출이 두 개의 매개 변수 함수 호출을 생성한다고 생각했습니다. 그래서, 왜 이것이 컴파일되지 않습니까?ostream 용 람다를 만드는 방법은 무엇입니까?
#include <iostream> // ostream
#include <iomanip> // setw, setfill
using std::ostream; using std::setw; using std::setfill;
struct Clock {
int h_, m_, s_;
Clock(int hours, int minutes, int seconds)
: h_{hours}, m_{minutes}, s_{seconds} {}
void setClock(int hours, int minutes, int seconds) {
h_ = hours; m_ = minutes; s_ = seconds;
}
friend ostream& operator<<(ostream&os, const Clock& c) {
auto w2 = [](ostream&os, int f) -> ostream& {
return os << setw(2) << setfill('0') << f; };
return os << w2(c.h_) <<':'<<w2(c.m_)<<':'<<w2(c.s_); // ERROR
}
};
오류가 나는 또한 전화 os << w2(os,c.h_)
하지만 GCC를 시도하고 내가 그 말도 동의했다
$ g++-6 -std=gnu++1y ...
file.cpp: In function ‘std::ostream& operator<<(std::ostream&, const Clock&)’:
file.cpp:745:33: error: no match for call to ‘(operator<<(std::ostream&, const Clock&)::<lambda(std::ostream&, int)>) (const int&)’
return os << w2(c.h_) <<':'<<w2(c.m_)<<':'<<w2(c.s_);
^
(GCC-6)이다. 또한 가능한 한 자동으로 람다를 사용해 보았습니다.
auto w2 = [](auto&os, auto f) {
return os << setw(2) << setfill('0') << f; };
도 운이 좋습니다.
힌트가 있습니까?
두 매개 변수가 필요한 매개 변수 하나만 람다에 전달합니다. 또한 람다의 반환 값을'std :: ostream &'인'operator <<'에 전달합니다. – Galik