Thread View: pl.comp.lang.c
2 messages
2 total messages
Started by Wojciech Muła
Thu, 28 Dec 2017 11:11
[C++] Czy to jest lagalne?
Author: Wojciech Muła
Date: Thu, 28 Dec 2017 11:11
Date: Thu, 28 Dec 2017 11:11
34 lines
1500 bytes
1500 bytes
Normalne kompilatory (GCC 7/clang 5) nie protestują.
Czy ten kod jest legalny? Chodzi o castowanie w capture().
--start--
#include <string>
class Item {
protected:
std::string a;
};
class Builder: public Item {
public:
void set() {
a = "foo";
}
Item&& capture() {
return std::move(*this);
}
};
int main() {
Builder b;
b.set();
Item a = b.capture();
}
--stop--
w.
Re: [C++] Czy to jest lagalne?
Author: bartekltg
Date: Fri, 29 Dec 2017 11:10
Date: Fri, 29 Dec 2017 11:10
62 lines
2595 bytes
2595 bytes
On Thursday, December 28, 2017 at 12:11:35 PM UTC+1, Wojciech Muła wrote:
> Normalne kompilatory (GCC 7/clang 5) nie protestują.
> Czy ten kod jest legalny? Chodzi o castowanie w capture().
>
> --start--
> #include <string>
>
> class Item {
>
> protected:
> std::string a;
> };
>
> class Builder: public Item {
>
> public:
> void set() {
> a = "foo";
> }
>
> Item&& capture() {
> return std::move(*this);
> }
> };
>
> int main() {
>
> Builder b;
> b.set();
>
> Item a = b.capture();
> }
> --stop--
Nie wiem ;>
Ale dlaczego chcesz miec metodą capture*), zamiast pisać:
int main() {
Builder b;
b.set();
Item a = move(b);
}
Zrbi dokładnie to samo, wiele więcej nie zajmuje,za to programista
ma jedną warstewkę myślową mniej, bo widzi standardowe zaklęcie
zamiast pamiętać/sprawdzać, co robi capture.
Jakby musiuało być capture, pewnie bym pisał to tak, że
capture zwraca jakiś opiekt zawierający referencje do b,
a operatory przypisania/konstruktor dostając ten obiekt
robiłby move. Ale to trochę przekombinowane:)
*)zwłaszcza, że to obiekt a przechwytuje, nie b;-)
pzdr
bartekltg
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads