std :: forwardはどのように機能しますか?

ダビデ

重複の可能性:
フォワードを使用する利点

私はそれが何をするのか、そしていつそれを使うのかを知っていますが、それでもそれがどのように機能するのか頭を包むことができません。std::forwardテンプレート引数の推論の使用が許可された場合、可能な限り詳細に説明し、いつ正しくないかを説明してください

私の混乱の一部は、このです:「それは名前を持っている場合、それは左辺値だ」 -それがない理由の場合だならばstd::forward、私は合格したときに異なる動作をthing&& xthing& x

まず、std::forward標準に従って行われるかを見てみましょう

§20.2.3 [forward] p2

戻り値: static_cast<T&&>(t)

(ここTで、は明示的に指定されたテンプレートパラメーターでtあり、は渡された引数です。)

ここで、参照の折りたたみルールを思い出してください。

TR   R

T&   &  -> T&  // lvalue reference to cv TR -> lvalue reference to T
T&   && -> T&  // rvalue reference to cv TR -> TR (lvalue reference to T)
T&&  &  -> T&  // lvalue reference to cv TR -> lvalue reference to T
T&&  && -> T&& // rvalue reference to cv TR -> TR (rvalue reference to T)

この答えから恥知らずに盗まれました。)

次に、完全な転送を採用したいクラスを見てみましょう。

template<class T>
struct some_struct{
  T _v;
  template<class U>
  some_struct(U&& v)
    : _v(static_cast<U&&>(v)) {} // perfect forwarding here
                                 // std::forward is just syntactic sugar for this
};

そして今、呼び出しの例:

int main(){
  some_struct<int> s1(5);
  // in ctor: '5' is rvalue (int&&), so 'U' is deduced as 'int', giving 'int&&'
  // ctor after deduction: 'some_struct(int&& v)' ('U' == 'int')
  // with rvalue reference 'v' bound to rvalue '5'
  // now we 'static_cast' 'v' to 'U&&', giving 'static_cast<int&&>(v)'
  // this just turns 'v' back into an rvalue
  // (named rvalue references, 'v' in this case, are lvalues)
  // huzzah, we forwarded an rvalue to the constructor of '_v'!

  // attention, real magic happens here
  int i = 5;
  some_struct<int> s2(i);
  // in ctor: 'i' is an lvalue ('int&'), so 'U' is deduced as 'int&', giving 'int& &&'
  // applying the reference collapsing rules yields 'int&' (& + && -> &)
  // ctor after deduction and collapsing: 'some_struct(int& v)' ('U' == 'int&')
  // with lvalue reference 'v' bound to lvalue 'i'
  // now we 'static_cast' 'v' to 'U&&', giving 'static_cast<int& &&>(v)'
  // after collapsing rules: 'static_cast<int&>(v)'
  // this is a no-op, 'v' is already 'int&'
  // huzzah, we forwarded an lvalue to the constructor of '_v'!
}

この段階的な回答が、あなたや他の人がどのようにstd::forward機能するかを理解するのに役立つことを願っています

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

2 つのオーバーロードされた std::forward はどのように機能しますか?

分類Dev

std :: strlenは内部的にどのように機能しますか?

分類Dev

std :: flushはどのように機能しますか?

分類Dev

std :: tieはどのように機能しますか?

分類Dev

std :: enable_ifはどのように機能しますか?

分類Dev

`std :: mem :: swap`はどのように機能しますか?

分類Dev

`std :: less`はどのように機能しますか?

分類Dev

std :: current_exceptionはどのように機能しますか?

分類Dev

std :: optionは内部でどのように機能しますか?

分類Dev

std :: uncaught_exceptionはどのように機能しますか?

分類Dev

このstd :: is_classの実装はどのように機能しますか?

分類Dev

std :: pairのこのconst参照はどのように機能しますか?

分類Dev

libcxxのstd :: is_functionの実装はどのように機能しますか?

分類Dev

std :: find_last_ofは実際にどのように機能しますか?

分類Dev

std :: vector :: reservedは実際にどのように機能しますか?

分類Dev

libc ++のstd :: is_literal_typeはどのように機能しますか?

分類Dev

std :: great <>はセットでどのように機能しますか?

分類Dev

std :: piecewise_construct構文はどのように機能しますか?

分類Dev

std :: setwは文字列出力でどのように機能しますか?

分類Dev

C ++:std :: stringでヌル文字はどのように機能しますか?

分類Dev

std :: notify_all_at_thread_exitはどのように機能しますか?

分類Dev

std.string.toStringzはdlangでどのように機能しますか?

分類Dev

std :: array境界チェックはどのように機能しますか?

分類Dev

アロケータはstd :: vectorでどのように機能しますか?

分類Dev

std :: string length()関数はどのように機能しますか?

分類Dev

std :: unique_lockとstd :: condition_variableはどのように機能しますか

分類Dev

「std :: cout << std :: endl;」はどのように機能しますか コンパイル?

分類Dev

std :: make_sharedとstd :: make_uniqueは舞台裏でどのように機能しますか?

分類Dev

Eric Nieblerによるstd :: is_functionの実装はどのように機能しますか?

Related 関連記事

  1. 1

    2 つのオーバーロードされた std::forward はどのように機能しますか?

  2. 2

    std :: strlenは内部的にどのように機能しますか?

  3. 3

    std :: flushはどのように機能しますか?

  4. 4

    std :: tieはどのように機能しますか?

  5. 5

    std :: enable_ifはどのように機能しますか?

  6. 6

    `std :: mem :: swap`はどのように機能しますか?

  7. 7

    `std :: less`はどのように機能しますか?

  8. 8

    std :: current_exceptionはどのように機能しますか?

  9. 9

    std :: optionは内部でどのように機能しますか?

  10. 10

    std :: uncaught_exceptionはどのように機能しますか?

  11. 11

    このstd :: is_classの実装はどのように機能しますか?

  12. 12

    std :: pairのこのconst参照はどのように機能しますか?

  13. 13

    libcxxのstd :: is_functionの実装はどのように機能しますか?

  14. 14

    std :: find_last_ofは実際にどのように機能しますか?

  15. 15

    std :: vector :: reservedは実際にどのように機能しますか?

  16. 16

    libc ++のstd :: is_literal_typeはどのように機能しますか?

  17. 17

    std :: great <>はセットでどのように機能しますか?

  18. 18

    std :: piecewise_construct構文はどのように機能しますか?

  19. 19

    std :: setwは文字列出力でどのように機能しますか?

  20. 20

    C ++:std :: stringでヌル文字はどのように機能しますか?

  21. 21

    std :: notify_all_at_thread_exitはどのように機能しますか?

  22. 22

    std.string.toStringzはdlangでどのように機能しますか?

  23. 23

    std :: array境界チェックはどのように機能しますか?

  24. 24

    アロケータはstd :: vectorでどのように機能しますか?

  25. 25

    std :: string length()関数はどのように機能しますか?

  26. 26

    std :: unique_lockとstd :: condition_variableはどのように機能しますか

  27. 27

    「std :: cout << std :: endl;」はどのように機能しますか コンパイル?

  28. 28

    std :: make_sharedとstd :: make_uniqueは舞台裏でどのように機能しますか?

  29. 29

    Eric Nieblerによるstd :: is_functionの実装はどのように機能しますか?

ホットタグ

アーカイブ