-
-
Notifications
You must be signed in to change notification settings - Fork 235
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
Comments
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 Andreas |
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 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 😄
*/
}; |
Hello @kunaltyagi,
I'm still not sure whether I understand correctly what you like to see. In your example, 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 |
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.
And yes, that's what I desired, but I can write some generic code to make sure the "possibilities are investigated".
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)
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:
Hope the above rambling helped. Thanks for pointing out that clang is lazy. |
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
The text was updated successfully, but these errors were encountered: