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

Suggestion: Guidance for calling non const methods from const via pointers (std::propagate_const, gsl::owner,not_null...) #566

Open
jmlundberg opened this issue Mar 27, 2016 · 3 comments
Assignees
Labels

Comments

@jmlundberg
Copy link

jmlundberg commented Mar 27, 2016

The section "Con.2: By default, make member functions const"

States under Enforcement: "Flag a member function that is not marked const, but that does not perform a non-const operation on any member variable."

Experience with resharper C++ which does this, is unfortunately that it's often doing more harm than good, because many/most pointer types (smart or plain, owning or not) do not propagate const, which makes resharper suggest possibly dangerously adding const. You all know it,

struct C{
  std::unique_ptr<X> uptr;
  Y* ptr;
  void do(){
    uptr->mutation();
    ptr->mutation();
 }
}

Adding const here becomes as bad as casting away const or making members mutable, and less visible. Also relates to thread safety implications of const.

I found no guidance related to this. A way to deal with this is http://en.cppreference.com/w/cpp/experimental/propagate_const , and something like

template<typename T,D> using cprop_uptr = std::propagate_const<std::unique_ptr<T,D>>

One useful practice (not for all?) is sticking to only const propagating pointer types as pointer members. (This may also be an argument for pushing the use of references over non-owning pointers. edit, no - as jbcoe notes)

This somewhat relates to GSL issue 89: microsoft/GSL#89 (not_null<unique_ptr<>> doesn't work #89 ) and indirectly to SO question http://stackoverflow.com/questions/33306553/gslnot-nullt-vs-stdreference-wrappert-vs-t

Has const propagation been considered for gsl smart pointers (owner<>, ... ) ? Or use operator.() ?

@jmlundberg jmlundberg changed the title Suggestion: Guidance for calling non const methods from const context via non-const propagating pointers (std::propagate_const, gsl::owner,not_null...) Suggestion: Guidance for calling non const methods from const via pointers (std::propagate_const, gsl::owner,not_null...) Mar 27, 2016
@AndrewPardoe AndrewPardoe assigned hsutter and unassigned gdr-at-ms Nov 28, 2016
@jbcoe
Copy link

jbcoe commented Mar 17, 2018

Re: "This may also be an argument for pushing the use of references over non-owning pointers." references have the same const-propagation issues as pointers when used as member variables.

@isocpp isocpp deleted a comment from kevin-shanghai Mar 19, 2018
@jmlundberg
Copy link
Author

Are you aware of any new insights or best practices on this? @jbcoe @AndrewPardoe

@carlosgalvezp
Copy link

Hi, are there any updates on the topic? I think it's interesting to look at this issue from the perspective of "semantic const" instead of "syntactic const".

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

No branches or pull requests

6 participants