Deletion of copy-ctor & copy-assignment - public, private or protected?Must a deleted constructor be private?Can copy-constructor with delete qualifier be private?What are the differences between a pointer variable and a reference variable in C++?What is the difference between public, protected, package-private and private in Java?In C#, what is the difference between public, private, protected, and having no access modifier?Difference between private, public, and protected inheritanceWhat is the copy-and-swap idiom?What is The Rule of Three?What are the basic rules and idioms for operator overloading?Why should C++ programmers minimize use of 'new'?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsDefault move constructor/assignment and deleted copy constructor/assignment
What is the meaning of "You've never met a graph you didn't like?"
PTIJ: Which Dr. Seuss books should one obtain?
Why does a 97 / 92 key piano exist by Bösendorfer?
Do I have to take mana from my deck or hand when tapping a dual land?
Naive definition of treewidth
How to make a small $varhexagon$ in latex?
Why is participating in the European Parliamentary elections used as a threat?
Adding up numbers in Portuguese is strange
Would this string work as string?
Why do Radio Buttons not fill the entire outer circle?
What happens if a creature's ETB would bounce Thalia, Heretic Cathar?
When and why was runway 07/25 at Kai Tak removed?
Given this phrasing in the lease, when should I pay my rent?
Do people actually use the word "kaputt" in conversation?
Consistent Linux device enumeration
Echo with obfuscation
Distinction between 地平線 【ちへいせん】 and 水平線 【すいへいせん】
Why does the Persian emissary display a string of crowned skulls?
Why didn't Voldemort know what Grindelwald looked like?
How to split IPA spelling into syllables
Why would five hundred and five be same as one?
When is the exact date for EOL of Ubuntu 14.04 LTS?
Would a primitive species be able to learn English from reading books alone?
What does "tick" mean in this sentence?
Deletion of copy-ctor & copy-assignment - public, private or protected?
Must a deleted constructor be private?Can copy-constructor with delete qualifier be private?What are the differences between a pointer variable and a reference variable in C++?What is the difference between public, protected, package-private and private in Java?In C#, what is the difference between public, private, protected, and having no access modifier?Difference between private, public, and protected inheritanceWhat is the copy-and-swap idiom?What is The Rule of Three?What are the basic rules and idioms for operator overloading?Why should C++ programmers minimize use of 'new'?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsDefault move constructor/assignment and deleted copy constructor/assignment
In order to make an object non-copiable we can explicitly delete both its copy-constructor and copy-assignment operator.
My question is: What is the right place to do it - in the public
, private
or protected
section of the class? And - does this choice make any difference?
c++ c++11 access-modifiers deleted-functions
add a comment |
In order to make an object non-copiable we can explicitly delete both its copy-constructor and copy-assignment operator.
My question is: What is the right place to do it - in the public
, private
or protected
section of the class? And - does this choice make any difference?
c++ c++11 access-modifiers deleted-functions
1
If you throw your old shoes away, do you think about where to store them?
– Klaus
Mar 17 at 10:11
12
@Klaus: No, but you think about where to throw them...
– einpoklum
Mar 17 at 11:21
add a comment |
In order to make an object non-copiable we can explicitly delete both its copy-constructor and copy-assignment operator.
My question is: What is the right place to do it - in the public
, private
or protected
section of the class? And - does this choice make any difference?
c++ c++11 access-modifiers deleted-functions
In order to make an object non-copiable we can explicitly delete both its copy-constructor and copy-assignment operator.
My question is: What is the right place to do it - in the public
, private
or protected
section of the class? And - does this choice make any difference?
c++ c++11 access-modifiers deleted-functions
c++ c++11 access-modifiers deleted-functions
edited Mar 17 at 16:29
StoryTeller
102k12215279
102k12215279
asked Mar 17 at 10:01
SajalSajal
640515
640515
1
If you throw your old shoes away, do you think about where to store them?
– Klaus
Mar 17 at 10:11
12
@Klaus: No, but you think about where to throw them...
– einpoklum
Mar 17 at 11:21
add a comment |
1
If you throw your old shoes away, do you think about where to store them?
– Klaus
Mar 17 at 10:11
12
@Klaus: No, but you think about where to throw them...
– einpoklum
Mar 17 at 11:21
1
1
If you throw your old shoes away, do you think about where to store them?
– Klaus
Mar 17 at 10:11
If you throw your old shoes away, do you think about where to store them?
– Klaus
Mar 17 at 10:11
12
12
@Klaus: No, but you think about where to throw them...
– einpoklum
Mar 17 at 11:21
@Klaus: No, but you think about where to throw them...
– einpoklum
Mar 17 at 11:21
add a comment |
3 Answers
3
active
oldest
votes
what is the right place to do it - in the public, private or protected section of the class?
I would put them in the public
section.
This is because deleting a constructor or an assignment operator is orthogonal to making them private
/ protected
; and when these aren't deleted, they are public
. Putting the deletions in one of those two sections seems to me like hinting "If I hadn't deleted them, I would have made them private/protected" - which is not a message you want to convey in your case.
Note, though, that the compiler doesn't care which section you put the deletion in.
7
Exactly this. Back in the day we made these things private specifically to deny folks access to them, but this was always a hack and was only because we couldn'tdelete
them. That consideration is no longer in play. I don't remember whether the "copy constructor is private" diagnostic tends to take precedence over the "copy constructor is deleted" diagnostic (I doubt it) but even if it doesn't changing the access level is not the right thing to do for the reasons you give.
– Lightness Races in Orbit
Mar 17 at 16:32
@LightnessRacesinOrbit I know I've seen some compilers give both errors when a function is private and deleted. The one about private access becomes just extra noise.
– aschepler
2 days ago
@aschepler Fair does
– Lightness Races in Orbit
2 days ago
@aschepler: Examples?
– einpoklum
2 days ago
gcc 7.4, but not gcc 8.1: godbolt.org/z/udzwB2 (so I guess they improved that).
– aschepler
2 days ago
|
show 2 more comments
Does where we put the deleted definition make any difference?
From a pure language standpoint it makes absolutely zero difference. Name lookup and overload resolution happen before access checking. And attempting to refer to a deleted function at the end of overload resolution makes your program ill-formed, period. A compiler may or may not issue another diagnostic about the accessibility, but the program already has an error that must be reported.
So you can put that deleted definition with whatever accessibility you desire. I think most will keep it private, to be inline with the "old" practice of making a class non-copyable (put the declaration of those members in the private section of the class, and not define them), if only to help those who know the old ways "get it" sooner. A mixture of idioms, if you would.
Marking as private is also something you can't avoid if you need to support both C++03 and C++11 mode. With the help of a macro, a header can be made to conform to both standards easily:
#if __cplusplus >= 201103L
#define DELETED_DEFINITION = delete
#else
#define DELETED_DEFINITION
#endif
class noncopyable
private:
// This header can be compiled as both C++11 and C++03
noncopyable(noncopyable const&) DELETED_DEFINITION;
void operator=(noncopyable const&) DELETED_DEFINITION;
;
If you want backwards compatibility then this is a must.
– Lightness Races in Orbit
Mar 17 at 16:32
1
@LightnessRacesinOrbit If you want backward compability, you should NEVER use C++11 features. In the case of deleted functions, the workaround is proposed by StoryTeller. But what is the backward compability solution when using lambdas, stl, concurrency, etc. ?
– hsalimi
2 days ago
@hsalimi There's only so far you can go with C++11 features if you want a useful compatibility layer, true, but I did have an event framework lib in a previous project that could be compiled in either C++03 or C++11 mode (it was still used by legacy embedded projects), and in the latter case it had a bunch of optimisations (mostly relating to rvalue refs) that improved things greatly without bunging up the interface too badly. I did need it to switch between Boost.Thread andstd::thread
, but then in the latter case it alleviates a library (and link!) dependency so it's not for naught.
– Lightness Races in Orbit
2 days ago
@hsalimi But yeah it meant I couldn't use lambdas in the lib ... or, at least, doing so would have been more trouble than it were worth.
– Lightness Races in Orbit
2 days ago
add a comment |
From Scott Meyers's book, Effective Modern C++ (Item 10), it seems that it is better to define them as public:
By convention, deleted functions are declared public, not private.
There’s a reason for that. When client code tries to use a member
function, C++ checks accessibility before deleted status. When client
code tries to use a deleted private function, some compilers complain
only about the function being private, even though the function’s
accessibility doesn’t really affect whether it can be used. It’s worth
bearing this in mind when revising legacy code to replace
private-and-not-defined member functions with deleted ones, because
making the new functions public will generally result in better error
messages.
In addition, I believe that a deleted copy constructor/assignment, should be part of the class interface to be shared with ALL of the class users. Such kind of information should not be kept as secret by making them private.
1
Meyers contradicts StoryTeller's answer, and my tests. It's still good advice, but I find einpoklum's reasoning to be superior.
– Lightness Races in Orbit
2 days ago
@LightnessRacesinOrbit I did the check on VS2013. The error message is different than g++ and it rightly shows the error. In addition, from a conceptual point of view, when a class deletes a ctor/cctor, etc., the class wants to say to ALL of its clients that: "Hey, this member is deleted, and you are not allowed to use it". There is no secret about this fact to keep it private.
– hsalimi
2 days ago
Agreed; that is einpoklum's reasoning that I find excellent ;)
– Lightness Races in Orbit
2 days ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55205874%2fdeletion-of-copy-ctor-copy-assignment-public-private-or-protected%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
what is the right place to do it - in the public, private or protected section of the class?
I would put them in the public
section.
This is because deleting a constructor or an assignment operator is orthogonal to making them private
/ protected
; and when these aren't deleted, they are public
. Putting the deletions in one of those two sections seems to me like hinting "If I hadn't deleted them, I would have made them private/protected" - which is not a message you want to convey in your case.
Note, though, that the compiler doesn't care which section you put the deletion in.
7
Exactly this. Back in the day we made these things private specifically to deny folks access to them, but this was always a hack and was only because we couldn'tdelete
them. That consideration is no longer in play. I don't remember whether the "copy constructor is private" diagnostic tends to take precedence over the "copy constructor is deleted" diagnostic (I doubt it) but even if it doesn't changing the access level is not the right thing to do for the reasons you give.
– Lightness Races in Orbit
Mar 17 at 16:32
@LightnessRacesinOrbit I know I've seen some compilers give both errors when a function is private and deleted. The one about private access becomes just extra noise.
– aschepler
2 days ago
@aschepler Fair does
– Lightness Races in Orbit
2 days ago
@aschepler: Examples?
– einpoklum
2 days ago
gcc 7.4, but not gcc 8.1: godbolt.org/z/udzwB2 (so I guess they improved that).
– aschepler
2 days ago
|
show 2 more comments
what is the right place to do it - in the public, private or protected section of the class?
I would put them in the public
section.
This is because deleting a constructor or an assignment operator is orthogonal to making them private
/ protected
; and when these aren't deleted, they are public
. Putting the deletions in one of those two sections seems to me like hinting "If I hadn't deleted them, I would have made them private/protected" - which is not a message you want to convey in your case.
Note, though, that the compiler doesn't care which section you put the deletion in.
7
Exactly this. Back in the day we made these things private specifically to deny folks access to them, but this was always a hack and was only because we couldn'tdelete
them. That consideration is no longer in play. I don't remember whether the "copy constructor is private" diagnostic tends to take precedence over the "copy constructor is deleted" diagnostic (I doubt it) but even if it doesn't changing the access level is not the right thing to do for the reasons you give.
– Lightness Races in Orbit
Mar 17 at 16:32
@LightnessRacesinOrbit I know I've seen some compilers give both errors when a function is private and deleted. The one about private access becomes just extra noise.
– aschepler
2 days ago
@aschepler Fair does
– Lightness Races in Orbit
2 days ago
@aschepler: Examples?
– einpoklum
2 days ago
gcc 7.4, but not gcc 8.1: godbolt.org/z/udzwB2 (so I guess they improved that).
– aschepler
2 days ago
|
show 2 more comments
what is the right place to do it - in the public, private or protected section of the class?
I would put them in the public
section.
This is because deleting a constructor or an assignment operator is orthogonal to making them private
/ protected
; and when these aren't deleted, they are public
. Putting the deletions in one of those two sections seems to me like hinting "If I hadn't deleted them, I would have made them private/protected" - which is not a message you want to convey in your case.
Note, though, that the compiler doesn't care which section you put the deletion in.
what is the right place to do it - in the public, private or protected section of the class?
I would put them in the public
section.
This is because deleting a constructor or an assignment operator is orthogonal to making them private
/ protected
; and when these aren't deleted, they are public
. Putting the deletions in one of those two sections seems to me like hinting "If I hadn't deleted them, I would have made them private/protected" - which is not a message you want to convey in your case.
Note, though, that the compiler doesn't care which section you put the deletion in.
edited 2 days ago
answered Mar 17 at 10:38
einpoklumeinpoklum
35.9k27132258
35.9k27132258
7
Exactly this. Back in the day we made these things private specifically to deny folks access to them, but this was always a hack and was only because we couldn'tdelete
them. That consideration is no longer in play. I don't remember whether the "copy constructor is private" diagnostic tends to take precedence over the "copy constructor is deleted" diagnostic (I doubt it) but even if it doesn't changing the access level is not the right thing to do for the reasons you give.
– Lightness Races in Orbit
Mar 17 at 16:32
@LightnessRacesinOrbit I know I've seen some compilers give both errors when a function is private and deleted. The one about private access becomes just extra noise.
– aschepler
2 days ago
@aschepler Fair does
– Lightness Races in Orbit
2 days ago
@aschepler: Examples?
– einpoklum
2 days ago
gcc 7.4, but not gcc 8.1: godbolt.org/z/udzwB2 (so I guess they improved that).
– aschepler
2 days ago
|
show 2 more comments
7
Exactly this. Back in the day we made these things private specifically to deny folks access to them, but this was always a hack and was only because we couldn'tdelete
them. That consideration is no longer in play. I don't remember whether the "copy constructor is private" diagnostic tends to take precedence over the "copy constructor is deleted" diagnostic (I doubt it) but even if it doesn't changing the access level is not the right thing to do for the reasons you give.
– Lightness Races in Orbit
Mar 17 at 16:32
@LightnessRacesinOrbit I know I've seen some compilers give both errors when a function is private and deleted. The one about private access becomes just extra noise.
– aschepler
2 days ago
@aschepler Fair does
– Lightness Races in Orbit
2 days ago
@aschepler: Examples?
– einpoklum
2 days ago
gcc 7.4, but not gcc 8.1: godbolt.org/z/udzwB2 (so I guess they improved that).
– aschepler
2 days ago
7
7
Exactly this. Back in the day we made these things private specifically to deny folks access to them, but this was always a hack and was only because we couldn't
delete
them. That consideration is no longer in play. I don't remember whether the "copy constructor is private" diagnostic tends to take precedence over the "copy constructor is deleted" diagnostic (I doubt it) but even if it doesn't changing the access level is not the right thing to do for the reasons you give.– Lightness Races in Orbit
Mar 17 at 16:32
Exactly this. Back in the day we made these things private specifically to deny folks access to them, but this was always a hack and was only because we couldn't
delete
them. That consideration is no longer in play. I don't remember whether the "copy constructor is private" diagnostic tends to take precedence over the "copy constructor is deleted" diagnostic (I doubt it) but even if it doesn't changing the access level is not the right thing to do for the reasons you give.– Lightness Races in Orbit
Mar 17 at 16:32
@LightnessRacesinOrbit I know I've seen some compilers give both errors when a function is private and deleted. The one about private access becomes just extra noise.
– aschepler
2 days ago
@LightnessRacesinOrbit I know I've seen some compilers give both errors when a function is private and deleted. The one about private access becomes just extra noise.
– aschepler
2 days ago
@aschepler Fair does
– Lightness Races in Orbit
2 days ago
@aschepler Fair does
– Lightness Races in Orbit
2 days ago
@aschepler: Examples?
– einpoklum
2 days ago
@aschepler: Examples?
– einpoklum
2 days ago
gcc 7.4, but not gcc 8.1: godbolt.org/z/udzwB2 (so I guess they improved that).
– aschepler
2 days ago
gcc 7.4, but not gcc 8.1: godbolt.org/z/udzwB2 (so I guess they improved that).
– aschepler
2 days ago
|
show 2 more comments
Does where we put the deleted definition make any difference?
From a pure language standpoint it makes absolutely zero difference. Name lookup and overload resolution happen before access checking. And attempting to refer to a deleted function at the end of overload resolution makes your program ill-formed, period. A compiler may or may not issue another diagnostic about the accessibility, but the program already has an error that must be reported.
So you can put that deleted definition with whatever accessibility you desire. I think most will keep it private, to be inline with the "old" practice of making a class non-copyable (put the declaration of those members in the private section of the class, and not define them), if only to help those who know the old ways "get it" sooner. A mixture of idioms, if you would.
Marking as private is also something you can't avoid if you need to support both C++03 and C++11 mode. With the help of a macro, a header can be made to conform to both standards easily:
#if __cplusplus >= 201103L
#define DELETED_DEFINITION = delete
#else
#define DELETED_DEFINITION
#endif
class noncopyable
private:
// This header can be compiled as both C++11 and C++03
noncopyable(noncopyable const&) DELETED_DEFINITION;
void operator=(noncopyable const&) DELETED_DEFINITION;
;
If you want backwards compatibility then this is a must.
– Lightness Races in Orbit
Mar 17 at 16:32
1
@LightnessRacesinOrbit If you want backward compability, you should NEVER use C++11 features. In the case of deleted functions, the workaround is proposed by StoryTeller. But what is the backward compability solution when using lambdas, stl, concurrency, etc. ?
– hsalimi
2 days ago
@hsalimi There's only so far you can go with C++11 features if you want a useful compatibility layer, true, but I did have an event framework lib in a previous project that could be compiled in either C++03 or C++11 mode (it was still used by legacy embedded projects), and in the latter case it had a bunch of optimisations (mostly relating to rvalue refs) that improved things greatly without bunging up the interface too badly. I did need it to switch between Boost.Thread andstd::thread
, but then in the latter case it alleviates a library (and link!) dependency so it's not for naught.
– Lightness Races in Orbit
2 days ago
@hsalimi But yeah it meant I couldn't use lambdas in the lib ... or, at least, doing so would have been more trouble than it were worth.
– Lightness Races in Orbit
2 days ago
add a comment |
Does where we put the deleted definition make any difference?
From a pure language standpoint it makes absolutely zero difference. Name lookup and overload resolution happen before access checking. And attempting to refer to a deleted function at the end of overload resolution makes your program ill-formed, period. A compiler may or may not issue another diagnostic about the accessibility, but the program already has an error that must be reported.
So you can put that deleted definition with whatever accessibility you desire. I think most will keep it private, to be inline with the "old" practice of making a class non-copyable (put the declaration of those members in the private section of the class, and not define them), if only to help those who know the old ways "get it" sooner. A mixture of idioms, if you would.
Marking as private is also something you can't avoid if you need to support both C++03 and C++11 mode. With the help of a macro, a header can be made to conform to both standards easily:
#if __cplusplus >= 201103L
#define DELETED_DEFINITION = delete
#else
#define DELETED_DEFINITION
#endif
class noncopyable
private:
// This header can be compiled as both C++11 and C++03
noncopyable(noncopyable const&) DELETED_DEFINITION;
void operator=(noncopyable const&) DELETED_DEFINITION;
;
If you want backwards compatibility then this is a must.
– Lightness Races in Orbit
Mar 17 at 16:32
1
@LightnessRacesinOrbit If you want backward compability, you should NEVER use C++11 features. In the case of deleted functions, the workaround is proposed by StoryTeller. But what is the backward compability solution when using lambdas, stl, concurrency, etc. ?
– hsalimi
2 days ago
@hsalimi There's only so far you can go with C++11 features if you want a useful compatibility layer, true, but I did have an event framework lib in a previous project that could be compiled in either C++03 or C++11 mode (it was still used by legacy embedded projects), and in the latter case it had a bunch of optimisations (mostly relating to rvalue refs) that improved things greatly without bunging up the interface too badly. I did need it to switch between Boost.Thread andstd::thread
, but then in the latter case it alleviates a library (and link!) dependency so it's not for naught.
– Lightness Races in Orbit
2 days ago
@hsalimi But yeah it meant I couldn't use lambdas in the lib ... or, at least, doing so would have been more trouble than it were worth.
– Lightness Races in Orbit
2 days ago
add a comment |
Does where we put the deleted definition make any difference?
From a pure language standpoint it makes absolutely zero difference. Name lookup and overload resolution happen before access checking. And attempting to refer to a deleted function at the end of overload resolution makes your program ill-formed, period. A compiler may or may not issue another diagnostic about the accessibility, but the program already has an error that must be reported.
So you can put that deleted definition with whatever accessibility you desire. I think most will keep it private, to be inline with the "old" practice of making a class non-copyable (put the declaration of those members in the private section of the class, and not define them), if only to help those who know the old ways "get it" sooner. A mixture of idioms, if you would.
Marking as private is also something you can't avoid if you need to support both C++03 and C++11 mode. With the help of a macro, a header can be made to conform to both standards easily:
#if __cplusplus >= 201103L
#define DELETED_DEFINITION = delete
#else
#define DELETED_DEFINITION
#endif
class noncopyable
private:
// This header can be compiled as both C++11 and C++03
noncopyable(noncopyable const&) DELETED_DEFINITION;
void operator=(noncopyable const&) DELETED_DEFINITION;
;
Does where we put the deleted definition make any difference?
From a pure language standpoint it makes absolutely zero difference. Name lookup and overload resolution happen before access checking. And attempting to refer to a deleted function at the end of overload resolution makes your program ill-formed, period. A compiler may or may not issue another diagnostic about the accessibility, but the program already has an error that must be reported.
So you can put that deleted definition with whatever accessibility you desire. I think most will keep it private, to be inline with the "old" practice of making a class non-copyable (put the declaration of those members in the private section of the class, and not define them), if only to help those who know the old ways "get it" sooner. A mixture of idioms, if you would.
Marking as private is also something you can't avoid if you need to support both C++03 and C++11 mode. With the help of a macro, a header can be made to conform to both standards easily:
#if __cplusplus >= 201103L
#define DELETED_DEFINITION = delete
#else
#define DELETED_DEFINITION
#endif
class noncopyable
private:
// This header can be compiled as both C++11 and C++03
noncopyable(noncopyable const&) DELETED_DEFINITION;
void operator=(noncopyable const&) DELETED_DEFINITION;
;
edited 2 days ago
answered Mar 17 at 10:26
StoryTellerStoryTeller
102k12215279
102k12215279
If you want backwards compatibility then this is a must.
– Lightness Races in Orbit
Mar 17 at 16:32
1
@LightnessRacesinOrbit If you want backward compability, you should NEVER use C++11 features. In the case of deleted functions, the workaround is proposed by StoryTeller. But what is the backward compability solution when using lambdas, stl, concurrency, etc. ?
– hsalimi
2 days ago
@hsalimi There's only so far you can go with C++11 features if you want a useful compatibility layer, true, but I did have an event framework lib in a previous project that could be compiled in either C++03 or C++11 mode (it was still used by legacy embedded projects), and in the latter case it had a bunch of optimisations (mostly relating to rvalue refs) that improved things greatly without bunging up the interface too badly. I did need it to switch between Boost.Thread andstd::thread
, but then in the latter case it alleviates a library (and link!) dependency so it's not for naught.
– Lightness Races in Orbit
2 days ago
@hsalimi But yeah it meant I couldn't use lambdas in the lib ... or, at least, doing so would have been more trouble than it were worth.
– Lightness Races in Orbit
2 days ago
add a comment |
If you want backwards compatibility then this is a must.
– Lightness Races in Orbit
Mar 17 at 16:32
1
@LightnessRacesinOrbit If you want backward compability, you should NEVER use C++11 features. In the case of deleted functions, the workaround is proposed by StoryTeller. But what is the backward compability solution when using lambdas, stl, concurrency, etc. ?
– hsalimi
2 days ago
@hsalimi There's only so far you can go with C++11 features if you want a useful compatibility layer, true, but I did have an event framework lib in a previous project that could be compiled in either C++03 or C++11 mode (it was still used by legacy embedded projects), and in the latter case it had a bunch of optimisations (mostly relating to rvalue refs) that improved things greatly without bunging up the interface too badly. I did need it to switch between Boost.Thread andstd::thread
, but then in the latter case it alleviates a library (and link!) dependency so it's not for naught.
– Lightness Races in Orbit
2 days ago
@hsalimi But yeah it meant I couldn't use lambdas in the lib ... or, at least, doing so would have been more trouble than it were worth.
– Lightness Races in Orbit
2 days ago
If you want backwards compatibility then this is a must.
– Lightness Races in Orbit
Mar 17 at 16:32
If you want backwards compatibility then this is a must.
– Lightness Races in Orbit
Mar 17 at 16:32
1
1
@LightnessRacesinOrbit If you want backward compability, you should NEVER use C++11 features. In the case of deleted functions, the workaround is proposed by StoryTeller. But what is the backward compability solution when using lambdas, stl, concurrency, etc. ?
– hsalimi
2 days ago
@LightnessRacesinOrbit If you want backward compability, you should NEVER use C++11 features. In the case of deleted functions, the workaround is proposed by StoryTeller. But what is the backward compability solution when using lambdas, stl, concurrency, etc. ?
– hsalimi
2 days ago
@hsalimi There's only so far you can go with C++11 features if you want a useful compatibility layer, true, but I did have an event framework lib in a previous project that could be compiled in either C++03 or C++11 mode (it was still used by legacy embedded projects), and in the latter case it had a bunch of optimisations (mostly relating to rvalue refs) that improved things greatly without bunging up the interface too badly. I did need it to switch between Boost.Thread and
std::thread
, but then in the latter case it alleviates a library (and link!) dependency so it's not for naught.– Lightness Races in Orbit
2 days ago
@hsalimi There's only so far you can go with C++11 features if you want a useful compatibility layer, true, but I did have an event framework lib in a previous project that could be compiled in either C++03 or C++11 mode (it was still used by legacy embedded projects), and in the latter case it had a bunch of optimisations (mostly relating to rvalue refs) that improved things greatly without bunging up the interface too badly. I did need it to switch between Boost.Thread and
std::thread
, but then in the latter case it alleviates a library (and link!) dependency so it's not for naught.– Lightness Races in Orbit
2 days ago
@hsalimi But yeah it meant I couldn't use lambdas in the lib ... or, at least, doing so would have been more trouble than it were worth.
– Lightness Races in Orbit
2 days ago
@hsalimi But yeah it meant I couldn't use lambdas in the lib ... or, at least, doing so would have been more trouble than it were worth.
– Lightness Races in Orbit
2 days ago
add a comment |
From Scott Meyers's book, Effective Modern C++ (Item 10), it seems that it is better to define them as public:
By convention, deleted functions are declared public, not private.
There’s a reason for that. When client code tries to use a member
function, C++ checks accessibility before deleted status. When client
code tries to use a deleted private function, some compilers complain
only about the function being private, even though the function’s
accessibility doesn’t really affect whether it can be used. It’s worth
bearing this in mind when revising legacy code to replace
private-and-not-defined member functions with deleted ones, because
making the new functions public will generally result in better error
messages.
In addition, I believe that a deleted copy constructor/assignment, should be part of the class interface to be shared with ALL of the class users. Such kind of information should not be kept as secret by making them private.
1
Meyers contradicts StoryTeller's answer, and my tests. It's still good advice, but I find einpoklum's reasoning to be superior.
– Lightness Races in Orbit
2 days ago
@LightnessRacesinOrbit I did the check on VS2013. The error message is different than g++ and it rightly shows the error. In addition, from a conceptual point of view, when a class deletes a ctor/cctor, etc., the class wants to say to ALL of its clients that: "Hey, this member is deleted, and you are not allowed to use it". There is no secret about this fact to keep it private.
– hsalimi
2 days ago
Agreed; that is einpoklum's reasoning that I find excellent ;)
– Lightness Races in Orbit
2 days ago
add a comment |
From Scott Meyers's book, Effective Modern C++ (Item 10), it seems that it is better to define them as public:
By convention, deleted functions are declared public, not private.
There’s a reason for that. When client code tries to use a member
function, C++ checks accessibility before deleted status. When client
code tries to use a deleted private function, some compilers complain
only about the function being private, even though the function’s
accessibility doesn’t really affect whether it can be used. It’s worth
bearing this in mind when revising legacy code to replace
private-and-not-defined member functions with deleted ones, because
making the new functions public will generally result in better error
messages.
In addition, I believe that a deleted copy constructor/assignment, should be part of the class interface to be shared with ALL of the class users. Such kind of information should not be kept as secret by making them private.
1
Meyers contradicts StoryTeller's answer, and my tests. It's still good advice, but I find einpoklum's reasoning to be superior.
– Lightness Races in Orbit
2 days ago
@LightnessRacesinOrbit I did the check on VS2013. The error message is different than g++ and it rightly shows the error. In addition, from a conceptual point of view, when a class deletes a ctor/cctor, etc., the class wants to say to ALL of its clients that: "Hey, this member is deleted, and you are not allowed to use it". There is no secret about this fact to keep it private.
– hsalimi
2 days ago
Agreed; that is einpoklum's reasoning that I find excellent ;)
– Lightness Races in Orbit
2 days ago
add a comment |
From Scott Meyers's book, Effective Modern C++ (Item 10), it seems that it is better to define them as public:
By convention, deleted functions are declared public, not private.
There’s a reason for that. When client code tries to use a member
function, C++ checks accessibility before deleted status. When client
code tries to use a deleted private function, some compilers complain
only about the function being private, even though the function’s
accessibility doesn’t really affect whether it can be used. It’s worth
bearing this in mind when revising legacy code to replace
private-and-not-defined member functions with deleted ones, because
making the new functions public will generally result in better error
messages.
In addition, I believe that a deleted copy constructor/assignment, should be part of the class interface to be shared with ALL of the class users. Such kind of information should not be kept as secret by making them private.
From Scott Meyers's book, Effective Modern C++ (Item 10), it seems that it is better to define them as public:
By convention, deleted functions are declared public, not private.
There’s a reason for that. When client code tries to use a member
function, C++ checks accessibility before deleted status. When client
code tries to use a deleted private function, some compilers complain
only about the function being private, even though the function’s
accessibility doesn’t really affect whether it can be used. It’s worth
bearing this in mind when revising legacy code to replace
private-and-not-defined member functions with deleted ones, because
making the new functions public will generally result in better error
messages.
In addition, I believe that a deleted copy constructor/assignment, should be part of the class interface to be shared with ALL of the class users. Such kind of information should not be kept as secret by making them private.
edited 2 days ago
answered 2 days ago
hsalimihsalimi
4,67022350
4,67022350
1
Meyers contradicts StoryTeller's answer, and my tests. It's still good advice, but I find einpoklum's reasoning to be superior.
– Lightness Races in Orbit
2 days ago
@LightnessRacesinOrbit I did the check on VS2013. The error message is different than g++ and it rightly shows the error. In addition, from a conceptual point of view, when a class deletes a ctor/cctor, etc., the class wants to say to ALL of its clients that: "Hey, this member is deleted, and you are not allowed to use it". There is no secret about this fact to keep it private.
– hsalimi
2 days ago
Agreed; that is einpoklum's reasoning that I find excellent ;)
– Lightness Races in Orbit
2 days ago
add a comment |
1
Meyers contradicts StoryTeller's answer, and my tests. It's still good advice, but I find einpoklum's reasoning to be superior.
– Lightness Races in Orbit
2 days ago
@LightnessRacesinOrbit I did the check on VS2013. The error message is different than g++ and it rightly shows the error. In addition, from a conceptual point of view, when a class deletes a ctor/cctor, etc., the class wants to say to ALL of its clients that: "Hey, this member is deleted, and you are not allowed to use it". There is no secret about this fact to keep it private.
– hsalimi
2 days ago
Agreed; that is einpoklum's reasoning that I find excellent ;)
– Lightness Races in Orbit
2 days ago
1
1
Meyers contradicts StoryTeller's answer, and my tests. It's still good advice, but I find einpoklum's reasoning to be superior.
– Lightness Races in Orbit
2 days ago
Meyers contradicts StoryTeller's answer, and my tests. It's still good advice, but I find einpoklum's reasoning to be superior.
– Lightness Races in Orbit
2 days ago
@LightnessRacesinOrbit I did the check on VS2013. The error message is different than g++ and it rightly shows the error. In addition, from a conceptual point of view, when a class deletes a ctor/cctor, etc., the class wants to say to ALL of its clients that: "Hey, this member is deleted, and you are not allowed to use it". There is no secret about this fact to keep it private.
– hsalimi
2 days ago
@LightnessRacesinOrbit I did the check on VS2013. The error message is different than g++ and it rightly shows the error. In addition, from a conceptual point of view, when a class deletes a ctor/cctor, etc., the class wants to say to ALL of its clients that: "Hey, this member is deleted, and you are not allowed to use it". There is no secret about this fact to keep it private.
– hsalimi
2 days ago
Agreed; that is einpoklum's reasoning that I find excellent ;)
– Lightness Races in Orbit
2 days ago
Agreed; that is einpoklum's reasoning that I find excellent ;)
– Lightness Races in Orbit
2 days ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55205874%2fdeletion-of-copy-ctor-copy-assignment-public-private-or-protected%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
If you throw your old shoes away, do you think about where to store them?
– Klaus
Mar 17 at 10:11
12
@Klaus: No, but you think about where to throw them...
– einpoklum
Mar 17 at 11:21