| |
| ▲ | sgsjchs 5 days ago | parent [-] | | If the moves were destructive, I'd design it to have the default constructor call `::socket` and destructor call `::close`. And there wouldn't be any kind of "closed" state. Why would I want it? | | |
| ▲ | spacechild1 5 days ago | parent [-] | | Your socket class would have no default constructor? And you would never want to close the socket before the object's lifetime ends? Really? | | |
| ▲ | sgsjchs 4 days ago | parent | next [-] | | In this case, I would want the address family and protocol to be statically known, so it would have default constructor. But for example, a file might not have one, sure. As for closing before lifetime ends, why? I can just end lifetime. Wrap it in an optional if the type system can't figure it out like with a struct member. | | |
| ▲ | spacechild1 4 days ago | parent [-] | | > so it would have default constructor. And what's the underlying value of such a default constructed socket? I assume it would be -1 resp. INVALID_SOCKET, in which case the destructor would have to deal with it. > Wrap it in an optional if the type system can't figure it out like with a struct member. So you essentially must wrap it in an optional if you want to use it as a member variable. I find this rather pointless as sockets already have a well-defined value for empty state (-1 resp. INVALID_SOCKET). By wrapping it in a optional you are just wasting up to 8 bytes. Sure, you can implement a socket class like that, but it's neither necessary nor idiomatic C++. | | |
| ▲ | sgsjchs 4 days ago | parent [-] | | > And what's the underlying value of such a default constructed socket? I assume it would be -1 resp. INVALID_SOCKET No, as explained, the default value would be the result of `::socket` call, i.e. a fresh OS-level socket. > So you essentially must wrap it in an optional if you want to use it as a member variable. No, you only must wrap it if you really want this closed state to exist. > Sure, you can implement a socket class like that, but it's neither necessary nor idiomatic C++. Obviously. Because the moves are not destructive. If they were, this design would be superior. And the wasted space for optional is solvable, just like for non-nullable pointers. | | |
| ▲ | spacechild1 4 days ago | parent [-] | | > If they were, this design would be superior. I see how destructive moves would slightly simplify the implementation, but what difference would it make apart from that? (Don't get me wrong, I totally think that destructive moves are a good idea in general, I just don't see the qualitative difference in this particular case.) > And the wasted space for optional is solvable, just like for non-nullable pointers. In the case of non-nullable pointers the library author knows that they can use NULL as a sentinel value and write a corresponding specialization. But what could you possibly do with an arbitrary user-defined class? | | |
| ▲ | sgsjchs 4 days ago | parent [-] | | > what difference would it make The same difference as making pointers always non-nullable and reintroducing nullability via an optional wrapper only when semantically appropriate. > what could you possibly do with an arbitrary user-defined class Just add some customization points to std::optional so that users can define which value of the class to treat as noneopt internally. | | |
| ▲ | spacechild1 3 days ago | parent [-] | | > The same difference as making pointers always non-nullable and reintroducing nullability via an optional wrapper only when semantically appropriate. Again, I don't see what this has to do with destructive moves. If you want a socket class that always refer to an open socket, you can already do that. Same for non-nullable pointer wrappers. Conversely, destructive moves don't prevent you from implementing a socket class with a close() method. These concepts are really orthogonal. > Just add some customization points to std::optional so that users can define which value of the class to treat as noneopt internally. How is this supposed to work? The very point of your socket class is that it always contains a valid socket handle. Once you introduce a sentinel value, you are back to square one. If the optional class is able to construct a socket with the sentinel value, so is the user. | | |
| ▲ | sgsjchs 3 days ago | parent [-] | | > Again, I don't see what this has to do with destructive moves. If you want a socket class that always refer to an open socket, you can already do that. Technically you can, but it's unreasonable to create an os-level socket just to put into the moved-out object where it will be immediately destroyed again. This is not an issue when the moves are destructive. > How is this supposed to work? The very point of your socket class is that it always contains a valid socket handle. Once you introduce a sentinel value, you are back to square one. If the optional class is able to construct a socket with the sentinel value, so is the user. That's not true. The sentinel value need not be exposed in the public interface of the class, it can only be accessible via the customization point of the optional. | | |
| ▲ | spacechild1 3 days ago | parent [-] | | > Technically you can, but it's unreasonable to create an os-level socket just to put into the moved-out object where it will be immediately destroyed again. This is not an issue when the moves are destructive. No, the class can use a sentinel value internally only to mark moved-from objects. That's exactly where we actually started the conversation. That's why I said that destructive moves would only somewhat simplify the move operations, but not make a qualitative difference (in this area). > The sentinel value need not be exposed in the public interface of the class, it can only be accessible via the customization point of the optional. Since the optional would need to construct an instance with the sentinel value, I thought that the "sentinel" constructor must be public. However, you might be right that one could write a template specialization that contains the template argument as a friend class. In this case you could use a private constructor. Note that the destructor still has to handle the sentinel value... But I guess this is just something you have to accept. | | |
| ▲ | sgsjchs 3 days ago | parent [-] | | > No, the class can use a sentinel value internally only to mark moved-from objects. That's exactly where we actually started the conversation. The issue is that the "moved-from" state is exposed to the user when the moves are not destructive. The author of the class has to consider behavior for every method in sentinel state, even when it's just to assert that the state isn't sentinel or "lol it's UB". And the user has to be careful not to accidentally misuse an object in sentinel state. Just like how every time you touch a nullable pointer you have to consider if it can be null and what to do in that case. As long as the sentinel state is exposed at all (via non-destructive move), there is little gain in not providing full support for it. However, with destructive moves the sentinel value either doesn't exist at all or only exists completely internally as an optimization, and all this mental overhead disappears. | | |
| ▲ | spacechild1 3 days ago | parent [-] | | I see your point. Just a few things: 1. This is only relevant when using such class as a local variable. Member variables are typically not moved-from. 2. In my understanding the user has the freedom to specify what constitutes a "valid but unspecified state" and it would be perfectly ok to mandate that anything you can do with a moved-from object is to either destroy or reassign it. 3. The problems with the state of moved-from objects from the perspective of a library author could have been prevented simply by imposing stricter requirements in the standard (e.g. every usage except destruction, and possible reassignment, shall be UB). 4. With all the issues you've pointed out, it is still be perfectly possible and reasonable to design a socket class your way (= no closed socket state) in C++, yet somehow most people seem to prefer open() and close() methods instead of modelling the state with an optional. Even in the presence of destructive moves, I don't think that one way is necessarily better than the other and it is mostly a matter of culture and personal preference. All the being said, I definitely agree that destructive moves are good thing, in particular if the compiler prevents you accidentally accessing moved-from objects (which is a mistake that is very easy to make in C++). | | |
| ▲ | sgsjchs 3 days ago | parent [-] | | Indeed, the "valid but unspecified state" refers only to some types defined in the he standard library. It essentially means that you can only call methods which have no preconditions and don't depend on what that state is, e.g. assignment or destruction, or something like string::clear or vstring::assign if you want defined outcomes. In general each type is free to guarantee whatever the author wants about the moved from state, e.g. moved-from std::unique_ptr is always null. |
|
|
|
|
|
|
|
|
|
| |
| ▲ | 7jjjjjjj 4 days ago | parent | prev [-] | | With destructive moves, you can end an object's lifetime whenever you want. | | |
| ▲ | spacechild1 4 days ago | parent [-] | | How would I use such a socket class as a member variable? How do I reopen the socket? | | |
| ▲ | sgsjchs 4 days ago | parent [-] | | Reopen by constructing and assigning a new socket. | | |
| ▲ | spacechild1 4 days ago | parent [-] | | So I essentially have to wrap it in something like std::optional. Well, that's certainly one way to write a socket class, but I'd say it's not idiomatic C++. (I have never seen a socket class being implemented like that.) | | |
| ▲ | sgsjchs 4 days ago | parent [-] | | You don't need optional in this case, the assignment would just destroy the old socket and immediately move the new one in its place. | | |
| ▲ | spacechild1 3 days ago | parent [-] | | Well, reopening a socket implies that I have manually closed the socket, which does require an optional with your implementation. |
|
|
|
|
|
|
|
|