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

throwing copy and move constructors cause final_act to not execute the action #283

Closed
ericniebler opened this issue Apr 4, 2016 · 11 comments

Comments

@ericniebler
Copy link
Contributor

The constructor of final_act here moves from argument f into member f_. If that throws, then the action is not executed, which is kinda the whole point of this utility. This paper shows how to do it right.

@jack17529
Copy link
Contributor

Can I work on this issue?

@neilmacintosh
Copy link
Collaborator

@jack17529 Sure! Feel free to put together a PR.

@gdr-at-ms
Copy link
Member

throwing moves are oddities: see rule C.66

@ericniebler
Copy link
Contributor Author

throwing moves are oddities

In a language where a "move" may in fact be a copy, probably not as odd as one might hope.

@gdr-at-ms
Copy link
Member

The Core Guidelines are focusing on helping "ordinary" programmers, as the expense of failing at language lawyering. There is a clear and bright distinction between a copy constructor and a move constructor. A move isn't a copy, and that is the primary and higher order bits that should be most helpful to most programmers.

@jack17529
Copy link
Contributor

@neilmacintosh , this may sound silly but I have never used narrow_cast , can you please explain me this line - https://github.com/Microsoft/GSL/blob/master/include/gsl/gsl_util#L108

@neilmacintosh
Copy link
Collaborator

@jack17529 Sure. (and questions are always welcome and never silly ;-)) If you look just a few lines up in the same file, you'll see that narrow_cast is just an alternative name for static_cast. The value in having a different name is that it conveys important semantic information to the reader of the code: that it is understood that the cast moves the value into a narrower data type (and that potentially, the value will be changed as a result).

@jack17529
Copy link
Contributor

jack17529 commented Jul 21, 2017

@neilmacintosh Narrower data type means for eg. casting it from 16 bit type to 8 bit type then 8bit bit data type will be narrow data type and it will also depend on weather the data type is unsigned or signed.Because it is like static_cast thus it must be type safe.If there is an exception then it will produce narrowing_error. Is it right what I understood.

@neilmacintosh
Copy link
Collaborator

@jack17529 yes, you have it pretty much right there. Casting from a type with a larger number of bits to smaller is definitely the simplest example. However, even going between signed and unsigned variants of the same type can "narrow", because each allows different value ranges.

Note that only using narrow produces a narrowing_error exception. narrow_cast is unchecked and really exists just to document that a narrowing is occurring. You can find a more complete description with examples in the Guidelines here: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Res-narrowing.

@jack17529
Copy link
Contributor

We are having problem in this move ctor -
explicit final_act(F f) noexcept : f_(std::move(f)), invoke_(true) {}

and this more specifically - f_(std::move(f)) as it may throw an exception .
Then I should use move_if_noexcept defined in .

Am I right in doing so ?

@JordanMaples
Copy link
Contributor

Maintainers' call: The code this issue refers to has considerably changed, sorry for the delay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants