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

Emit code to see Rule of 3/5/0 #291

Closed
kunaltyagi opened this issue Jan 27, 2020 · 4 comments
Closed

Emit code to see Rule of 3/5/0 #291

kunaltyagi opened this issue Jan 27, 2020 · 4 comments
Labels
awaiting-followup Waiting for more input enhancement New feature or request

Comments

@kunaltyagi
Copy link

Is it possible to emit code for 'default' ctors (as comment or anyhow) and marks them with "As per Rule of 3/5/0"? If they can't be emitted, provide a comment saying: "can't due to presence of X".

This will help newcomers understand all this a bit better.

Thanks for the awesome work

@andreasfertig
Copy link
Owner

Hello @kunaltyagi,

you're welcome!

I'm not sure, if I understand your request. C++ Insights does emit compiler generated functions as a comment. However, Clang only generates them when actually used. Here is an example cppinsights.io/s/12a9481b:

class Zero
{
};


class ZeroUsed
{
};

int main()
{
  Zero zero{};
  
  ZeroUsed zeroUsed{};
  ZeroUsed zeroUsed2{zeroUsed};
  
}

In case of Zero nothing is shown, as Clang does not create anything in this case. This is different in ZeroUsed. There a copy construction happens which makes Clang generate a copy-constructor. Is this already something that helps you and wasn't documented properly or do you like more? If not, please give me an example before and after.

Andreas

@andreasfertig andreasfertig added the awaiting-followup Waiting for more input label Jan 27, 2020
@kunaltyagi
Copy link
Author

If not, please give me an example before and after.

Sure. The 3/5/0 rule is a bit difficult. For example: when will a move ctor or move assignment be available, and when it wouldn't be. What's the simplest method to ensure that it is present? Will a = default work or will a complete definition needs to be provided?

In order to make this more palatable, it might help if the following happens:

// before
struct EmitDestroyed {
~EmitDestroyed() { std::cout << "Dead\n"; }
};
struct CountCopies {
  CountCopies() = default;
  CountCopies(const CountCopies& other) x(other.x + 1) {}
private:
  int x = 0;  // ignore overflow
};
struct Owner {
std::unique_ptr<int> ptr;
};

// after
struct EmitDestroyed {
~EmitDestroyed() { std::cout << "Dead\n"; }
/*
constexpr AllDefault() noexcept = default;
... 4 copy/move ctor/assignment if possible
*/
};
struct CountCopies {
  CountCopies() = default;
  CountCopies(const CountCopies& other) x(other.x + 1) {}
private:
  int x = 0;  // ignore overflow
/*
The autogen/not autogen code here
*/
};
struct Owner {
std::unique_ptr<int> ptr;
/*
Q. What all does the compiler does for us?
A. Try out at cppinsights.io 😄 
*/
};

@andreasfertig andreasfertig added the enhancement New feature or request label Jan 27, 2020
@andreasfertig
Copy link
Owner

Hello @kunaltyagi,

The 3/5/0 rule is a bit difficult.
I agree.

I'm still not sure whether I understand correctly what you like to see.

In your example, EmitDestroyed is not used. Clang does not border to generate code for this. Not even in the front-end. That's why there is nothing shown in C++ Insights. Which is quite helpful, if you're looking into performance stuff. The same goes for Owner which is probably more interesting due to the unqiue_ptr. In this case C++ Insights shows directly, that the copy-constructor is deleted, because Clang says so. In case you use this struct, for example with a move, other special members appear.

Is what you would like to see more a "what is possible in general"? Currently, C++ Insights shows what happens, not what could happen. I'm happy to think about another direction. It is just that when I teach move semantics in my training classes it takes quite some time and I do not claim that I cover everything. I'm not sure, if C++ Insights without a book/training/class or whatever helps in this case that much. Can you maybe share how you got to this point and why? Private mail is fine.

Andreas

@kunaltyagi
Copy link
Author

Clang does not border to generate code for this.

Thanks Andreas. Essentially, clang doesn't create functions if they aren't used. This is pretty helpful. I created a template function which tries to use the functions so they get emitted.

This works semi as-expected, semi because the dtor was missing but a private inheritance fixed that up. I think it will not change the semantics, but I'll have to check to ensure that.

Is what you would like to see more a "what is possible in general"?

And yes, that's what I desired, but I can write some generic code to make sure the "possibilities are investigated".

Currently, C++ Insights shows what happens, not what could happen.

I think that's fair. It instills the C++ paradigm better: why generate a function if not used (eg: templates which can have compilation errors not detected till someone instantiates them)

if C++ Insights without a book/training/class or whatever helps in this case that much.

Books are alright, but having tools which can check the intuition is better. For example: Given a type, if confused why you get the behavior (eg: copy not move), you can ask questions like:

  • What does the compiler already generates for me?: Check as usual
  • What can you do with a given type?: Check with the function template I used above
  • Does the type's semantics make sense?: Checklist basic categories of classes
  • Does the (generic) code require the correct semantics (aka the smallest set of features possible)?: Use cppinsights on 6 classes [each class has one thing deleted (ctor + rule of 5)]

Can you maybe share how you got to this point and why?

Hope the above rambling helped. Thanks for pointing out that clang is lazy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-followup Waiting for more input enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants