21
4

3 に答える 3

26

You can't take a non-const reference to a temporary like foo2 does.

Notice that this isn't specifically default parameters. You get the same error for function variables: http://ideone.com/g7Tf7L

#include <string>
using std::string;

#include <iostream>
using std::cout; using std::endl;

int main()
{
    string s1        = string("s1"); // OK, copy it
    const string& s2 = string("s2"); // OK, const reference to it
    string& s3       = string("s3"); // ERROR! non-const reference not allowed!

    cout
            << s1 << ", "
            << s2 << ", "
            << s3 << endl;
    return 0;
}

When you take a const reference to a temporary, the lifetime of the temporary is extended to the lifetime of the reference (§12.2, quoted from my copy of C++11 draft n3337):

There are two contexts in which temporaries are destroyed at a different point than the end of the fullexpression.

...

The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference except:

  • A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the constructor exits.
  • A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full-expression containing the call.
  • The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not extended; the temporary is destroyed at the end of the full-expression in the return statement.
  • A temporary bound to a reference in a new-initializer (5.3.4) persists until the completion of the full-expression containing the new-initializer.
于 2013-01-30T20:22:25.737 に答える
3

驚くかもしれませんが、一時的な式の値を定数参照にバインドすると、式の有効期間が参照の有効期間まで延長されます。ただし、非定数 (左辺値) 参照ではこれを行うことはできません。

于 2013-01-30T20:26:10.950 に答える
0

これらの関数への引数は参照ではないため、foo3との宣言は有効です。foo4

foo2非 const 参照を一時にバインドできないため、 の宣言は不正です。

では、なぜfoo1合法の宣言なのですか?この宣言を有効にするのは、非常に重要なconst修飾子です。

于 2013-01-30T20:25:38.343 に答える