What is going on: C++ std::move on std::shared_ptr increases use_count?What are the differences between a...
Is there a basic list of ways in which a low-level Rogue can get advantage for sneak attack?
Why is differential privacy defined over the exponential function?
Stack class in Java 8
How can I fix a framing mistake so I can drywall?
Determining if file in projected or geographic coordinates using ArcGIS Desktop?
How does Vivi differ from other Black Mages?
Why would thermal imaging be used to locate the Chandrayaan-2 lander?
How to split a string by the third .(dot) delimiter
SCOTUS - Can Congress overrule Marbury v. Madison by statute?
Why is the the worst case for this function O(n^2)?
Seized engine due to being run without oil
Is there a star over my head?
Procedure for traffic not in sight
How to create a list of dictionaries from a dictionary with lists of different lengths
Are there any instances of members of different Hogwarts houses coupling up and marrying each other?
Are there take-over requests from autopilots?
Dividing Divisive Divisors
Job offer without any details but asking me to withdraw other applications - is it normal?
RP Automatic Updates
Is there a sentence that begins with “them”?
Why are some Mac apps not available on AppStore?
How do I preserve the line ordering for two "equal" strings while sorting and ignoring the case?
Which ping implementation is cygwin using?
Is BitLocker useful in the case of stolen laptop?
What is going on: C++ std::move on std::shared_ptr increases use_count?
What are the differences between a pointer variable and a reference variable in C++?What are POD types in C++?What are the rules about using an underscore in a C++ identifier?What are C++ functors and their uses?What is the effect of extern “C” in C++?What is the “-->” operator in C++?What is move semantics?What is the copy-and-swap idiom?What is std::move(), and when should it be used?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I was always assuming that std::move() on a std::shared_ptr steals the pointer and sets the pointer of the original to nullptr-thus not increasing the reference count. That does not seem to be true in my world.
SETUP:
MacOS, g++ -version => "Apple LLVM version 10.0.1 (clang-1001.0.46.3)"
CODE:
#include <cstdio>
#include <memory>
class Thing { public: Thing(int N) : value(N) {} int value; };
void print(const char* name, std::shared_ptr<Thing>& sp)
{ printf("%s: { use_count=%i; }n", name, (int)sp.use_count()); }
int main(int argc, char** argv) {
std::shared_ptr<Thing> x(new Thing(4711));
print("BEFORE x", x);
std::shared_ptr<Thing> y = std::move(x);
y->value = 4712;
print(" AFTER x", x);
print(" AFTER y", y);
return 0;
}
OUTPUT:
Compiling (g++ tmp.cpp -o test), and running (./test), delivers
BEFORE x: { use_count=1; }
AFTER x: { use_count=2; }
AFTER y: { use_count=2; }
So, the reference count is increased while using std::move().
QUESTION:
What is going on, here?
c++ macos shared-ptr c++98
add a comment |
I was always assuming that std::move() on a std::shared_ptr steals the pointer and sets the pointer of the original to nullptr-thus not increasing the reference count. That does not seem to be true in my world.
SETUP:
MacOS, g++ -version => "Apple LLVM version 10.0.1 (clang-1001.0.46.3)"
CODE:
#include <cstdio>
#include <memory>
class Thing { public: Thing(int N) : value(N) {} int value; };
void print(const char* name, std::shared_ptr<Thing>& sp)
{ printf("%s: { use_count=%i; }n", name, (int)sp.use_count()); }
int main(int argc, char** argv) {
std::shared_ptr<Thing> x(new Thing(4711));
print("BEFORE x", x);
std::shared_ptr<Thing> y = std::move(x);
y->value = 4712;
print(" AFTER x", x);
print(" AFTER y", y);
return 0;
}
OUTPUT:
Compiling (g++ tmp.cpp -o test), and running (./test), delivers
BEFORE x: { use_count=1; }
AFTER x: { use_count=2; }
AFTER y: { use_count=2; }
So, the reference count is increased while using std::move().
QUESTION:
What is going on, here?
c++ macos shared-ptr c++98
After fixing UB can't reproduce.
– Marek R
14 hours ago
FWIW I'm on (almost) the same version and the library code doesn't match the report iff_LIBCPP_HAS_NO_RVALUE_REFERENCESisn't defined
– Lightness Races in Orbit
14 hours ago
add a comment |
I was always assuming that std::move() on a std::shared_ptr steals the pointer and sets the pointer of the original to nullptr-thus not increasing the reference count. That does not seem to be true in my world.
SETUP:
MacOS, g++ -version => "Apple LLVM version 10.0.1 (clang-1001.0.46.3)"
CODE:
#include <cstdio>
#include <memory>
class Thing { public: Thing(int N) : value(N) {} int value; };
void print(const char* name, std::shared_ptr<Thing>& sp)
{ printf("%s: { use_count=%i; }n", name, (int)sp.use_count()); }
int main(int argc, char** argv) {
std::shared_ptr<Thing> x(new Thing(4711));
print("BEFORE x", x);
std::shared_ptr<Thing> y = std::move(x);
y->value = 4712;
print(" AFTER x", x);
print(" AFTER y", y);
return 0;
}
OUTPUT:
Compiling (g++ tmp.cpp -o test), and running (./test), delivers
BEFORE x: { use_count=1; }
AFTER x: { use_count=2; }
AFTER y: { use_count=2; }
So, the reference count is increased while using std::move().
QUESTION:
What is going on, here?
c++ macos shared-ptr c++98
I was always assuming that std::move() on a std::shared_ptr steals the pointer and sets the pointer of the original to nullptr-thus not increasing the reference count. That does not seem to be true in my world.
SETUP:
MacOS, g++ -version => "Apple LLVM version 10.0.1 (clang-1001.0.46.3)"
CODE:
#include <cstdio>
#include <memory>
class Thing { public: Thing(int N) : value(N) {} int value; };
void print(const char* name, std::shared_ptr<Thing>& sp)
{ printf("%s: { use_count=%i; }n", name, (int)sp.use_count()); }
int main(int argc, char** argv) {
std::shared_ptr<Thing> x(new Thing(4711));
print("BEFORE x", x);
std::shared_ptr<Thing> y = std::move(x);
y->value = 4712;
print(" AFTER x", x);
print(" AFTER y", y);
return 0;
}
OUTPUT:
Compiling (g++ tmp.cpp -o test), and running (./test), delivers
BEFORE x: { use_count=1; }
AFTER x: { use_count=2; }
AFTER y: { use_count=2; }
So, the reference count is increased while using std::move().
QUESTION:
What is going on, here?
c++ macos shared-ptr c++98
c++ macos shared-ptr c++98
edited 13 hours ago
Deduplicator
37.4k6 gold badges52 silver badges92 bronze badges
37.4k6 gold badges52 silver badges92 bronze badges
asked 15 hours ago
Frank-Rene SchäferFrank-Rene Schäfer
1,3748 silver badges26 bronze badges
1,3748 silver badges26 bronze badges
After fixing UB can't reproduce.
– Marek R
14 hours ago
FWIW I'm on (almost) the same version and the library code doesn't match the report iff_LIBCPP_HAS_NO_RVALUE_REFERENCESisn't defined
– Lightness Races in Orbit
14 hours ago
add a comment |
After fixing UB can't reproduce.
– Marek R
14 hours ago
FWIW I'm on (almost) the same version and the library code doesn't match the report iff_LIBCPP_HAS_NO_RVALUE_REFERENCESisn't defined
– Lightness Races in Orbit
14 hours ago
After fixing UB can't reproduce.
– Marek R
14 hours ago
After fixing UB can't reproduce.
– Marek R
14 hours ago
FWIW I'm on (almost) the same version and the library code doesn't match the report iff
_LIBCPP_HAS_NO_RVALUE_REFERENCES isn't defined– Lightness Races in Orbit
14 hours ago
FWIW I'm on (almost) the same version and the library code doesn't match the report iff
_LIBCPP_HAS_NO_RVALUE_REFERENCES isn't defined– Lightness Races in Orbit
14 hours ago
add a comment |
1 Answer
1
active
oldest
votes
What is going on, here?
On MacOS, it seems that you must explicitly enable move-sematics with -std=c++11 (or later standards)¹. Otherwise, the example happens to compile (i.e., std::shared_ptr from the related library implementation is usable) but doesn't work correctly as the required language features aren't enabled. This results in actual copies being made instead of move constructions. It would have been better if the AppleClang package didn't even allow an instantiation of std::shared_ptr when the required language features isn't enabled.
¹) Thanks to @t.niese for testing the given compiler/platform.
1
@lubgr I can confirm that your code will result onuse_count=2;when compiled and run with the given compiler on mac, but will result inuse_count=1;on e.g. wandbox.
– t.niese
14 hours ago
2
@lubgr but only when compiling withg++ tmp.cpp -o test, withg++ -std=c++11 tmp.cpp -o testorg++ -std=c++17 tmp.cpp -o testit showsuse_count=1;
– t.niese
14 hours ago
on godbolt it works fine.
– Marek R
14 hours ago
2
Sounds like the standard library in use allows for using C++11 features (std::shared_ptr) without enabling the language support (move semantics), which results in actual copies being made.
– lubgr
14 hours ago
@lubgr; your last comment seems to pinpoint into the right direction. Can you, or someone, make an answer out of it?
– Frank-Rene Schäfer
14 hours ago
|
show 3 more comments
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/4.0/"u003ecc by-sa 4.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%2f57852209%2fwhat-is-going-on-c-stdmove-on-stdshared-ptr-increases-use-count%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
What is going on, here?
On MacOS, it seems that you must explicitly enable move-sematics with -std=c++11 (or later standards)¹. Otherwise, the example happens to compile (i.e., std::shared_ptr from the related library implementation is usable) but doesn't work correctly as the required language features aren't enabled. This results in actual copies being made instead of move constructions. It would have been better if the AppleClang package didn't even allow an instantiation of std::shared_ptr when the required language features isn't enabled.
¹) Thanks to @t.niese for testing the given compiler/platform.
1
@lubgr I can confirm that your code will result onuse_count=2;when compiled and run with the given compiler on mac, but will result inuse_count=1;on e.g. wandbox.
– t.niese
14 hours ago
2
@lubgr but only when compiling withg++ tmp.cpp -o test, withg++ -std=c++11 tmp.cpp -o testorg++ -std=c++17 tmp.cpp -o testit showsuse_count=1;
– t.niese
14 hours ago
on godbolt it works fine.
– Marek R
14 hours ago
2
Sounds like the standard library in use allows for using C++11 features (std::shared_ptr) without enabling the language support (move semantics), which results in actual copies being made.
– lubgr
14 hours ago
@lubgr; your last comment seems to pinpoint into the right direction. Can you, or someone, make an answer out of it?
– Frank-Rene Schäfer
14 hours ago
|
show 3 more comments
What is going on, here?
On MacOS, it seems that you must explicitly enable move-sematics with -std=c++11 (or later standards)¹. Otherwise, the example happens to compile (i.e., std::shared_ptr from the related library implementation is usable) but doesn't work correctly as the required language features aren't enabled. This results in actual copies being made instead of move constructions. It would have been better if the AppleClang package didn't even allow an instantiation of std::shared_ptr when the required language features isn't enabled.
¹) Thanks to @t.niese for testing the given compiler/platform.
1
@lubgr I can confirm that your code will result onuse_count=2;when compiled and run with the given compiler on mac, but will result inuse_count=1;on e.g. wandbox.
– t.niese
14 hours ago
2
@lubgr but only when compiling withg++ tmp.cpp -o test, withg++ -std=c++11 tmp.cpp -o testorg++ -std=c++17 tmp.cpp -o testit showsuse_count=1;
– t.niese
14 hours ago
on godbolt it works fine.
– Marek R
14 hours ago
2
Sounds like the standard library in use allows for using C++11 features (std::shared_ptr) without enabling the language support (move semantics), which results in actual copies being made.
– lubgr
14 hours ago
@lubgr; your last comment seems to pinpoint into the right direction. Can you, or someone, make an answer out of it?
– Frank-Rene Schäfer
14 hours ago
|
show 3 more comments
What is going on, here?
On MacOS, it seems that you must explicitly enable move-sematics with -std=c++11 (or later standards)¹. Otherwise, the example happens to compile (i.e., std::shared_ptr from the related library implementation is usable) but doesn't work correctly as the required language features aren't enabled. This results in actual copies being made instead of move constructions. It would have been better if the AppleClang package didn't even allow an instantiation of std::shared_ptr when the required language features isn't enabled.
¹) Thanks to @t.niese for testing the given compiler/platform.
What is going on, here?
On MacOS, it seems that you must explicitly enable move-sematics with -std=c++11 (or later standards)¹. Otherwise, the example happens to compile (i.e., std::shared_ptr from the related library implementation is usable) but doesn't work correctly as the required language features aren't enabled. This results in actual copies being made instead of move constructions. It would have been better if the AppleClang package didn't even allow an instantiation of std::shared_ptr when the required language features isn't enabled.
¹) Thanks to @t.niese for testing the given compiler/platform.
edited 14 hours ago
answered 15 hours ago
lubgrlubgr
25.8k3 gold badges36 silver badges79 bronze badges
25.8k3 gold badges36 silver badges79 bronze badges
1
@lubgr I can confirm that your code will result onuse_count=2;when compiled and run with the given compiler on mac, but will result inuse_count=1;on e.g. wandbox.
– t.niese
14 hours ago
2
@lubgr but only when compiling withg++ tmp.cpp -o test, withg++ -std=c++11 tmp.cpp -o testorg++ -std=c++17 tmp.cpp -o testit showsuse_count=1;
– t.niese
14 hours ago
on godbolt it works fine.
– Marek R
14 hours ago
2
Sounds like the standard library in use allows for using C++11 features (std::shared_ptr) without enabling the language support (move semantics), which results in actual copies being made.
– lubgr
14 hours ago
@lubgr; your last comment seems to pinpoint into the right direction. Can you, or someone, make an answer out of it?
– Frank-Rene Schäfer
14 hours ago
|
show 3 more comments
1
@lubgr I can confirm that your code will result onuse_count=2;when compiled and run with the given compiler on mac, but will result inuse_count=1;on e.g. wandbox.
– t.niese
14 hours ago
2
@lubgr but only when compiling withg++ tmp.cpp -o test, withg++ -std=c++11 tmp.cpp -o testorg++ -std=c++17 tmp.cpp -o testit showsuse_count=1;
– t.niese
14 hours ago
on godbolt it works fine.
– Marek R
14 hours ago
2
Sounds like the standard library in use allows for using C++11 features (std::shared_ptr) without enabling the language support (move semantics), which results in actual copies being made.
– lubgr
14 hours ago
@lubgr; your last comment seems to pinpoint into the right direction. Can you, or someone, make an answer out of it?
– Frank-Rene Schäfer
14 hours ago
1
1
@lubgr I can confirm that your code will result on
use_count=2; when compiled and run with the given compiler on mac, but will result in use_count=1; on e.g. wandbox.– t.niese
14 hours ago
@lubgr I can confirm that your code will result on
use_count=2; when compiled and run with the given compiler on mac, but will result in use_count=1; on e.g. wandbox.– t.niese
14 hours ago
2
2
@lubgr but only when compiling with
g++ tmp.cpp -o test, with g++ -std=c++11 tmp.cpp -o test or g++ -std=c++17 tmp.cpp -o test it shows use_count=1;– t.niese
14 hours ago
@lubgr but only when compiling with
g++ tmp.cpp -o test, with g++ -std=c++11 tmp.cpp -o test or g++ -std=c++17 tmp.cpp -o test it shows use_count=1;– t.niese
14 hours ago
on godbolt it works fine.
– Marek R
14 hours ago
on godbolt it works fine.
– Marek R
14 hours ago
2
2
Sounds like the standard library in use allows for using C++11 features (
std::shared_ptr) without enabling the language support (move semantics), which results in actual copies being made.– lubgr
14 hours ago
Sounds like the standard library in use allows for using C++11 features (
std::shared_ptr) without enabling the language support (move semantics), which results in actual copies being made.– lubgr
14 hours ago
@lubgr; your last comment seems to pinpoint into the right direction. Can you, or someone, make an answer out of it?
– Frank-Rene Schäfer
14 hours ago
@lubgr; your last comment seems to pinpoint into the right direction. Can you, or someone, make an answer out of it?
– Frank-Rene Schäfer
14 hours ago
|
show 3 more comments
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f57852209%2fwhat-is-going-on-c-stdmove-on-stdshared-ptr-increases-use-count%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
After fixing UB can't reproduce.
– Marek R
14 hours ago
FWIW I'm on (almost) the same version and the library code doesn't match the report iff
_LIBCPP_HAS_NO_RVALUE_REFERENCESisn't defined– Lightness Races in Orbit
14 hours ago