約思考聲明的變量(| R | L |公關| X GL)值,以下問題來到我的腦海:由&&
考慮以下兩個變量聲明:
X x = ...;
和
X&& x = ...;
,並承擔...
做不提供的x值。
有誰能想到代碼不是使用decltype
其中這有什麼區別?在這兩種情況下,(x)
都會由一個類型爲X
的左值,不是嗎?
約思考聲明的變量(| R | L |公關| X GL)值,以下問題來到我的腦海:由&&
考慮以下兩個變量聲明:
X x = ...;
和
X&& x = ...;
,並承擔...
做不提供的x值。
有誰能想到代碼不是使用decltype
其中這有什麼區別?在這兩種情況下,(x)
都會由一個類型爲X
的左值,不是嗎?
模板非類型參數cannot refer to a temporary。因此,鑑於
struct X {};
X purr() { return {}; }
X x1 = purr();
X&& x2 = purr();
template<X&> class woof {};
我們
woof<x1> w1; // OK
woof<x2> w2; // Error
如果...
不限於X
型prvalue,然後切片是使兩個非相當於少模糊的方式。鑑於:
struct X { virtual ~X() = default; };
struct Y : X {};
Y meow() { return {}; }
然後:
X x1 = meow(); // slices
X&& x2 = meow(); // doesn't slice
這樣:
dynamic_cast<Y&>(x1); // throws std::bad_cast
dynamic_cast<Y&>(x2); // OK
這很好。謝謝。 – JohnB
也許人爲的例子,但與
struct X
{
X() = default;
X(const X&) = delete;
X operator =(const X&) = delete;
X(X&&) = delete;
X operator =(X&&) = delete;
};
X makeX() {return {};}
以下編譯
X&& x = makeX();
而以下不
X x = makeX();
我指的是在使用這樣聲明的變量的差異。 – JohnB