Igor🇺🇦
Hide structures from user?
how is this different from namespace?
Roy
struct Test { private: struct hide {}; public: struct show {}; };
Pavel
struct Test { private: struct hide {}; public: struct show {}; };
Only if it's templated code, otherwise you can just put it to cpp
Roy
You can't access to hide struct from Test space?
Roy
You just do not yet realize how flexible such a design is and how convenient it is to use.
Pavel
But header only
That is worth mentioning if you're doing header-only library
Roy
You don't need to worry about where you store your data, you don't need to store anything, components do it all and you just need to write NeedComponent::
Igor🇺🇦
struct Test { private: struct hide {}; public: struct show {}; };
you can create namespace Test { namespace { struct hide {}; }; struct show {}; };
Roy
this trick have bug, not remember reason of bug
Roy
and it's work only for .cpp if i'm not mistake
Roy
how the hell do I know that? nice to see that i'm developing
Igor🇺🇦
Roy
safe from whom?
user, this trick have filename bug, I don't remember the details
Igor🇺🇦
user, this trick have filename bug, I don't remember the details
What do you mean by "filename bug" ?
Roy
https://wiki.sei.cmu.edu/confluence/display/cplusplus/DCL59-CPP.+Do+not+define+an+unnamed+namespace+in+a+header+file
Pavel
you can create namespace Test { namespace { struct hide {}; }; struct show {}; };
Too fast, I wanted to mention that it's against core guidelines also http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rs-unnamed
Pavel
I wonder actually what happens if you declare a function like that and will have a static variable inside. Because functions in unnamed namespaces marked as static by default, so a function for each translation unit will be generated
Igor🇺🇦
I wonder actually what happens if you declare a function like that and will have a static variable inside. Because functions in unnamed namespaces marked as static by default, so a function for each translation unit will be generated
anonymous namespace in header is not very useful regardless of guidelines. We can access it anyway anywhere if we include header. So it doesn't serve any purpose. Anyways, I still don't get the point of deleted destructors.
Roy
I don’t know why you are so fixated on this
Igor🇺🇦
I don’t know why you are so fixated on this
because you wrote it for some reason and C++ supports it.
Igor🇺🇦
className() = delete; should be enough
Roy
className() = delete; should be enough
for stack yes, but not for new
Roy
struct AAA { ~AAA() = delete; }; AAA obj; // error auto ptr = new AAA(); // ok delete ptr; // error
Igor🇺🇦
struct AAA { ~AAA() = delete; }; AAA obj; // error auto ptr = new AAA(); // ok delete ptr; // error
first of all if you delete a constructor new will not work. Second - why is memory leak in your example a good thing?
Anonymous
Language: cpp Source: struct a{ a() = delete; }; int main(){ auto var = new a(); return 0; } Errors: 652169966/source.cpp: In function ‘int main()’: 652169966/source.cpp:5:21: error: use of deleted function ‘a::a()’ auto var = new a(); ^ 652169966/source.cpp:2:5: note: declared here a() = delete; ^ Note: cplusplus_gcc assumed, other valid options are cplusplus_clang, visual_cplusplus, you can be more specific next time.
Roy
you delete ctor
Anonymous
lol what?
You wrote ~
Anonymous
Instead of AAA you wrote ~AAA
Roy
ohh yea, sorry, my mistake
Anonymous
I posted the code with what you were saying
olli
for stack yes, but not for new
you can still have an instance of AAA who "lives" on the "stack" (automatic storage duration)
Anonymous
And even the pointer fails
Roy
Challenge accepted
Roy
auto ptr = new AAA{};
Anonymous
Language: cpp Source: struct a{ a() = delete; }; int main(){ a vall(); return 0; } Note: cplusplus_gcc assumed, other valid options are cplusplus_clang, visual_cplusplus, you can be more specific next time.
Roy
You cant create them on stack
Roy
but can use new and aggregate ctor
Anonymous
Language: cpp Source: struct a{ explicit a() = delete; }; int main(){ a cl{}; return 0; } Errors: 385489401/source.cpp: In function ‘int main()’: 385489401/source.cpp:5:14: error: use of deleted function ‘a::a()’ a cl{}; ^ 385489401/source.cpp:2:14: note: declared here explicit a() = delete; ^ Note: cplusplus_gcc assumed, other valid options are cplusplus_clang, visual_cplusplus, you can be more specific next time.
Anonymous
Language: cpp Source: struct a{ explicit a() = delete; }; int main(){ auto ptr = new a{}; return 0; } Errors: 756292846/source.cpp: In function ‘int main()’: 756292846/source.cpp:5:26: error: use of deleted function ‘a::a()’ auto ptr = new a{}; ^ 756292846/source.cpp:2:14: note: declared here explicit a() = delete; ^ 756292846/source.cpp:5:14: warning: unused variable ‘ptr’ [-Wunused-variable] auto ptr = new a{}; ^~~ Note: cplusplus_gcc assumed, other valid options are cplusplus_clang, visual_cplusplus, you can be more specific next time.
Anonymous
So challenge failed
Roy
yea, you won
Anonymous
yea, you won
Nobody won
Anonymous
We all learned 👍
Roy
But how many mistakes would you make before finding the correct implementation?
Roy
I prefer to be explicit in code design
Roy
if class not creatable - delete all ctors
Anonymous
Without the explicit keyword it did work like this
Anonymous
Language: cpp Source: struct a{ a() = delete; }; int main(){ a ptr{}; return 0; } Warnings: 1801301646/source.cpp: In function ‘int main()’: 1801301646/source.cpp:5:11: warning: unused variable ‘ptr’ [-Wunused-variable] a ptr{}; ^~~ Note: cplusplus_gcc assumed, other valid options are cplusplus_clang, visual_cplusplus, you can be more specific next time.
Anonymous
Language: cpp Source: struct a{ explicit a() = delete; }; int main(){ a ptr{}; return 0; } Errors: 456582155/source.cpp: In function ‘int main()’: 456582155/source.cpp:5:15: error: use of deleted function ‘a::a()’ a ptr{}; ^ 456582155/source.cpp:2:14: note: declared here explicit a() = delete; ^ Note: cplusplus_gcc assumed, other valid options are cplusplus_clang, visual_cplusplus, you can be more specific next time.
Anonymous
Point proven but useless
olli
Can anyone explain why this compiles? What constructor is being called here?
It's "Aggregate initialization" and people considered this example a defect, starting with C++20 it won't compile anymore. Before C++20 an aggregate type was allowed to have "explicitly defaulted or deleted constructors [...]". Since C++20 an aggregate types has "no user-declared or inherited constructors"
Anonymous
Before C++20 you had to explicit the constructor. {} Would call the standard constructor and would be redirected to the special {} constructor
Anonymous
Without explicit the conversion would happen from a() to a special constructor
Anonymous
how would {} call the standard constructor if it's deleted?
There is the call resolution that if a conversion is satisfied then you will be redirected
Anonymous
This behavior has been corrected now
olli
There is the call resolution that if a conversion is satisfied then you will be redirected
From your example above - which constructor is called here?! struct a{ a() = delete; }; int main(){ a ptr{}; return 0; }
Anonymous
From your example above - which constructor is called here?! struct a{ a() = delete; }; int main(){ a ptr{}; return 0; }
https://stackoverflow.com/questions/33988297/deleted-default-constructor-objects-can-still-be-created-sometimes
Anonymous
Language: cpp Source: struct a{ private: a(){} }; int main(){ a ptr{}; return 0; } Errors: 269750205/source.cpp: In function ‘int main()’: 269750205/source.cpp:6:15: error: ‘a::a()’ is private within this context a ptr{}; ^ 269750205/source.cpp:3:6: note: declared private here a(){} ^ Note: cplusplus_gcc assumed, other valid options are cplusplus_clang, visual_cplusplus, you can be more specific next time.
Anonymous
The private constructor manage to do this and bypass the madness above
olli
Before C++20 you had to explicit the constructor. {} Would call the standard constructor and would be redirected to the special {} constructor
Yes I am aware. I wanted to point out that {} does not necessarily invoke the "standard constructor"
Anonymous
Yes I am aware. I wanted to point out that {} does not necessarily invoke the "standard constructor"
Before C++20 it was like a new special constructor was there and invisibile
olli
Before C++20 it was like a new special constructor was there and invisibile
"it was like" - that's vague phrasing and might be misleading. Before C++20 it was considered an aggregate type and could be aggregate initialized
Sync
I'd like to join
Roshan
Is there some way to write: player1.setPosition().x = 55;
Roshan
Player1 is an object, but what is setPosition() ? Can we access variables of functions in a class?
Roshan
Roshan
Of x
What is setPosition()