Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listing 6-15 #174

Closed
theundergroundsorcerer opened this issue May 17, 2020 · 4 comments
Closed

Listing 6-15 #174

theundergroundsorcerer opened this issue May 17, 2020 · 4 comments

Comments

@theundergroundsorcerer
Copy link

The example does not compile with VS 2019 if you do not specify the generic parameter explicitly.
Also move constructor and move assignment operators should be noexcept I think.
Also, the consumer function parameter should be void consumer(SimpleUniquePointer&& consumer_ptr)
and not void consumer(SimpleUniquePointer consumerPtr);

@JLospinoso
Copy link
Owner

Hi, @theundergroundsorcerer, thanks for reaching out.

  1. Did you enable C++17 in VS2019?
  2. Yes, noexcept would be a nice addition. But we don't get to noexcept till chapter 9, so it's a pedagogical example.
  3. What's the advantage to using an rvalue reference here?

@theundergroundsorcerer
Copy link
Author

  1. You are correct, C++17 should be enabled. (I've thought I was already compiling with this option).
  2. noexcept is discussed in chapter 4 actually, where exceptions and constructors are discussed. Move constructor and move assignment in the example are made noexcept in the chapter example.
  3. I think that defining foo f(bar x) is implicit overload. If you want to forbid rvalues as arguments, you have to explicitly delete foo f(bar&& x) which implies that function with this signature is generated by the compiler. In this case, the function accepting rvalue is the only one that is valid, and explicit is usually better than implicit, especially when teaching.

@JLospinoso
Copy link
Owner

  1. Great, glad we figured that out! :)
  2. You're totally right. I'll add a note that these should be noexcept.
  3. I think the explicit nature of an rvalue reference is nice. Since copy is deleted, it doesn't really matter. I'll make a note.

@theundergroundsorcerer
Copy link
Author

theundergroundsorcerer commented May 18, 2020

  1. Hmm, it does matter a bit. Haven't tried with other compilers than visual studio, but when attempting to call a function with deleted copy constructor, you are going to get a sort of confusing error message complaining about lack of copy constructor, which points at deleted function. You won't be told about the function itself. In a larger scenario, for example if such a function is defined in a library with header and object file provided only, it is going to be hard to fix or diagnose, I think.

kruschk pushed a commit to kruschk/ccc that referenced this issue Apr 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants