私はこのサンプルプログラムを持っています:
#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;
    });
}
ご覧のとおり、さまざまな方法で引数を渡します。
- Message は、最終的に PrintImpl 関数に転送する必要があるため、ユニバーサル リファレンスとして渡されます。
- ここでは装飾が const ref として渡されます。これは、その値が 2 回使用され、 forward を 2 回使用しても安全かどうかわからないためです。(最初の前進で動かされるかも?)
- printImpl は const 参照として渡されます。これは、forward を使用する理由が見当たらないためです。ただし、これが賢明かどうかはわかりません。(通り過ぎるべき&&ですか?もしそうなら、私も使うべきstd::forwardですか?)
私は正しい選択をしていますか?