나는이 예제 프로그램이 있습니다패스 템플릿 인수 및 또는 &&
- 메시지가 전달됩니다 : 당신은 내가 인수를 전달하는 다른 방법을 사용하여 볼 수 있듯이
#include <iostream> template<typename Message, typename Decoration, typename PrintImpl> void print_surrounded(Message&& msg, const Decoration& decoration, const PrintImpl& print_impl) { print_impl(decoration); // should forward be used? print_impl(" "); print_impl(std::forward<Message>(msg)); print_impl(" "); print_impl(decoration); } template<typename Message, typename PrintImpl> void pretty_print(Message&& msg, const PrintImpl& print_impl) { print_surrounded(std::forward<Message>(msg), "***", print_impl); } int main() { pretty_print("So pretty!", [](const char* msg) { std::cout << msg; }); }
결국 PrintImpl 함수로 전달되어야하므로 범용 참조로 사용됩니다.
- 데코레이션은 값이 두 번 사용되기 때문에 const ref로 전달됩니다. 앞으로 두 번 사용하면 안전할지 모르겠습니다. (첫 번째 전달에 의해 사라질 수 있습니다.)
- 앞으로 사용할 이유가 없기 때문에 PrintImpl이 const 참조로 전달됩니다. 그러나 이것이 현명한 지 확실하지 않습니다.
내가 올바른 선택을하고 있는가 (예, 나는 또한 std::forward
를 사용해야하는 경우 나? &&
통과해야)?
이것은 CPS가 의미하는 바를 정확히 이해하지 못했습니다. http://en.wikipedia.org/wiki/Continuation-passing_style 또한 주석의 줄에서'decoration'가 아니라'print_impl'과 함께'forward'를 사용할 것인지 묻는다고 가정합니다. 맞습니까? – sehe
@sehe 좋아, CPS 부품을 제거했습니다. – StackedCrooked
그냥 명확히하기 위해 : 여기에 정확한 용어를 "포착"합니까? 클로저 객체 (람다 표현식에 의해 생성 됨)에 의한 변수 캡처를 위해 "캡쳐"가 예약되어 있다고 생각했습니다. 논증의 "통과"가 올바른 용어가 아닌가? – Walter