use_count becomes -1 when using shared_ptr in C++
GenericStack.h
#ifndef _GENERIC_STACK_TROFIMOV_H_
#define _GENERIC_STACK_TROFIMOV_H_
#include <memory>
class GenericStack {
struct StackNode {
std::shared_ptr<void> _data;
StackNode* _next;
StackNode(const std::shared_ptr<void>& p, StackNode* next)
: _data(p), _next(next) {
}
};
StackNode* _top;
GenericStack(const GenericStack&);
GenericStack& operator=(const GenericStack&);
protected:
GenericStack();
~GenericStack();
void push(const std::shared_ptr<void>&);
void pop();
std::shared_ptr<void>& top();
bool isEmpty() const;
public:
class EmptyError {
const char* _message;
public:
EmptyError(const char* message)
: _message(message) {
}
const char* getMessage() const {
return _message;
}
};
};
template <class T>
class TStack: private GenericStack {
public:
void push(const std::shared_ptr<T>& p) { GenericStack::push(p); }
void pop() { GenericStack::pop(); }
std::shared_ptr<T>& top() { return std::static_pointer_cast<T>(GenericStack::top()); }
bool isEmpty() const { return GenericStack::isEmpty(); }
};
#endif
GenerickStack.cpp
#include "GenericStack.h"
GenericStack::GenericStack()
: _top(0) {
};
GenericStack::~GenericStack() {
while(!isEmpty()) {
pop();
}
};
void GenericStack::push(const std::shared_ptr<void>& p) {
_top = new StackNode(p, _top);
}
std::shared_ptr<void>& GenericStack::top() {
if(isEmpty()) {
throw EmptyError("No more elements in stack.");
}
return _top->_data;
}
void GenericStack::pop() {
if(isEmpty()) {
throw EmptyError("No more elements in stack.");
}
StackNode* t = _top->_next;
delete _top;
_top = t;
}
bool GenericStack::isEmpty() const {
return !_top;
}
Main.cpp
#include <iostream>
#include "GenericStack.h"
//#define NDEBUG
#include <assert.h>
void ordinaryUsageVerification() {
TStack<int> intStack;
{
std::shared_ptr<int> sh(new int(7));
intStack.push(sh);
intStack.isEmpty();
assert(!intStack.isEmpty() && sh.use_count() == 2);
}
//assert(!intStack.isEmpty() && intStack.top().use_count() == 1);
std::cout << "intStack.top().use_count(): " << intStack.top().use_count() << std::endl;
std::cout << "*gs.top(): " << *intStack.top() << std::endl;
intStack.pop();
assert(intStack.isEmpty());
}
int main() {
ordinaryUsageVerification();
return 0;
}
After the following two line in Main.cpp:
std::shared_ptr<int> sh(new int(7));
intStack.push(sh);
I am expecting intStack.top().use_count()
to be equal 2, but it is equal -1.
I am expecting such a behavior because when calling a push
method I am passing the shared_ptr
by reference, so the use_count
should not change. And only in one place in GenericaStack.h here:
StackNode(const std::shared_ptr<void>& p, StackNode* next)
: _data(p), _next(next) {
The use_count
increments by one for p
.
So, given that before push
I had sh.use_count() == 1
and after intStack.push(sh);
I had sh.use_count() == 2
I should get intStack.top().use_count() == 2
, but what I am getting is intStack.top().use_count() == -1
. Why?
Thank you.
After modifying GenericStack.h in this way:
#ifndef _GENERIC_STACK_TROFIMOV_H_
#define _GENERIC_STACK_TROFIMOV_H_
#include <memory>
class GenericStack {
struct StackNode {
std::shared_ptr<void> _data;
StackNode* _next;
StackNode(std::shared_ptr<void>& p, StackNode* next)
: _data(p), _next(next) {
}
};
StackNode* _top;
GenericStack(const GenericStack&);
GenericStack& operator=(const GenericStack&);
protected:
GenericStack();
~GenericStack();
void push(std::shared_ptr<void>&);
void pop();
std::shared_ptr<void>& top();
bool isEmpty() const;
public:
class EmptyError {
const char* _message;
public:
EmptyError(const char* message)
: _message(message) {
}
const char* getMessage() const {
return _message;
}
};
};
template <class T>
class TStack: private GenericStack {
public:
void push(std::shared_ptr<T>& p) {
GenericStack::push(p);
}
void pop() { GenericStack::pop(); }
std::shared_ptr<T> top() { return std::static_pointer_cast<T>(GenericStack::top()); }
bool isEmpty() const { return GenericStack::isEmpty(); }
};
#endif
I am getting an error:
Error 1 error C2664: 'GenericStack::push' : cannot convert parameter 1 from 'std::shared_ptr<_Ty>' to 'std::shared_ptr<_Ty> &' ...stackgenericstack.h 47 Stack
It is about this part:
void push(std::shared_ptr<T>& p) {
GenericStack::push(p);
}
c++ pointers memory shared-ptr
add a comment |
GenericStack.h
#ifndef _GENERIC_STACK_TROFIMOV_H_
#define _GENERIC_STACK_TROFIMOV_H_
#include <memory>
class GenericStack {
struct StackNode {
std::shared_ptr<void> _data;
StackNode* _next;
StackNode(const std::shared_ptr<void>& p, StackNode* next)
: _data(p), _next(next) {
}
};
StackNode* _top;
GenericStack(const GenericStack&);
GenericStack& operator=(const GenericStack&);
protected:
GenericStack();
~GenericStack();
void push(const std::shared_ptr<void>&);
void pop();
std::shared_ptr<void>& top();
bool isEmpty() const;
public:
class EmptyError {
const char* _message;
public:
EmptyError(const char* message)
: _message(message) {
}
const char* getMessage() const {
return _message;
}
};
};
template <class T>
class TStack: private GenericStack {
public:
void push(const std::shared_ptr<T>& p) { GenericStack::push(p); }
void pop() { GenericStack::pop(); }
std::shared_ptr<T>& top() { return std::static_pointer_cast<T>(GenericStack::top()); }
bool isEmpty() const { return GenericStack::isEmpty(); }
};
#endif
GenerickStack.cpp
#include "GenericStack.h"
GenericStack::GenericStack()
: _top(0) {
};
GenericStack::~GenericStack() {
while(!isEmpty()) {
pop();
}
};
void GenericStack::push(const std::shared_ptr<void>& p) {
_top = new StackNode(p, _top);
}
std::shared_ptr<void>& GenericStack::top() {
if(isEmpty()) {
throw EmptyError("No more elements in stack.");
}
return _top->_data;
}
void GenericStack::pop() {
if(isEmpty()) {
throw EmptyError("No more elements in stack.");
}
StackNode* t = _top->_next;
delete _top;
_top = t;
}
bool GenericStack::isEmpty() const {
return !_top;
}
Main.cpp
#include <iostream>
#include "GenericStack.h"
//#define NDEBUG
#include <assert.h>
void ordinaryUsageVerification() {
TStack<int> intStack;
{
std::shared_ptr<int> sh(new int(7));
intStack.push(sh);
intStack.isEmpty();
assert(!intStack.isEmpty() && sh.use_count() == 2);
}
//assert(!intStack.isEmpty() && intStack.top().use_count() == 1);
std::cout << "intStack.top().use_count(): " << intStack.top().use_count() << std::endl;
std::cout << "*gs.top(): " << *intStack.top() << std::endl;
intStack.pop();
assert(intStack.isEmpty());
}
int main() {
ordinaryUsageVerification();
return 0;
}
After the following two line in Main.cpp:
std::shared_ptr<int> sh(new int(7));
intStack.push(sh);
I am expecting intStack.top().use_count()
to be equal 2, but it is equal -1.
I am expecting such a behavior because when calling a push
method I am passing the shared_ptr
by reference, so the use_count
should not change. And only in one place in GenericaStack.h here:
StackNode(const std::shared_ptr<void>& p, StackNode* next)
: _data(p), _next(next) {
The use_count
increments by one for p
.
So, given that before push
I had sh.use_count() == 1
and after intStack.push(sh);
I had sh.use_count() == 2
I should get intStack.top().use_count() == 2
, but what I am getting is intStack.top().use_count() == -1
. Why?
Thank you.
After modifying GenericStack.h in this way:
#ifndef _GENERIC_STACK_TROFIMOV_H_
#define _GENERIC_STACK_TROFIMOV_H_
#include <memory>
class GenericStack {
struct StackNode {
std::shared_ptr<void> _data;
StackNode* _next;
StackNode(std::shared_ptr<void>& p, StackNode* next)
: _data(p), _next(next) {
}
};
StackNode* _top;
GenericStack(const GenericStack&);
GenericStack& operator=(const GenericStack&);
protected:
GenericStack();
~GenericStack();
void push(std::shared_ptr<void>&);
void pop();
std::shared_ptr<void>& top();
bool isEmpty() const;
public:
class EmptyError {
const char* _message;
public:
EmptyError(const char* message)
: _message(message) {
}
const char* getMessage() const {
return _message;
}
};
};
template <class T>
class TStack: private GenericStack {
public:
void push(std::shared_ptr<T>& p) {
GenericStack::push(p);
}
void pop() { GenericStack::pop(); }
std::shared_ptr<T> top() { return std::static_pointer_cast<T>(GenericStack::top()); }
bool isEmpty() const { return GenericStack::isEmpty(); }
};
#endif
I am getting an error:
Error 1 error C2664: 'GenericStack::push' : cannot convert parameter 1 from 'std::shared_ptr<_Ty>' to 'std::shared_ptr<_Ty> &' ...stackgenericstack.h 47 Stack
It is about this part:
void push(std::shared_ptr<T>& p) {
GenericStack::push(p);
}
c++ pointers memory shared-ptr
What possessed you to return a reference here?
– StoryTeller
Nov 25 '18 at 13:09
1
Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please first extract a Minimal, Complete, and Verifiable example. In particular, there shouldn't be any need to split this into three files!
– Ulrich Eckhardt
Nov 25 '18 at 14:41
Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please first extract a Minimal, Complete, and Verifiable example. In particular, there shouldn't be any need to split this into three files!
– Ulrich Eckhardt
Nov 25 '18 at 14:41
add a comment |
GenericStack.h
#ifndef _GENERIC_STACK_TROFIMOV_H_
#define _GENERIC_STACK_TROFIMOV_H_
#include <memory>
class GenericStack {
struct StackNode {
std::shared_ptr<void> _data;
StackNode* _next;
StackNode(const std::shared_ptr<void>& p, StackNode* next)
: _data(p), _next(next) {
}
};
StackNode* _top;
GenericStack(const GenericStack&);
GenericStack& operator=(const GenericStack&);
protected:
GenericStack();
~GenericStack();
void push(const std::shared_ptr<void>&);
void pop();
std::shared_ptr<void>& top();
bool isEmpty() const;
public:
class EmptyError {
const char* _message;
public:
EmptyError(const char* message)
: _message(message) {
}
const char* getMessage() const {
return _message;
}
};
};
template <class T>
class TStack: private GenericStack {
public:
void push(const std::shared_ptr<T>& p) { GenericStack::push(p); }
void pop() { GenericStack::pop(); }
std::shared_ptr<T>& top() { return std::static_pointer_cast<T>(GenericStack::top()); }
bool isEmpty() const { return GenericStack::isEmpty(); }
};
#endif
GenerickStack.cpp
#include "GenericStack.h"
GenericStack::GenericStack()
: _top(0) {
};
GenericStack::~GenericStack() {
while(!isEmpty()) {
pop();
}
};
void GenericStack::push(const std::shared_ptr<void>& p) {
_top = new StackNode(p, _top);
}
std::shared_ptr<void>& GenericStack::top() {
if(isEmpty()) {
throw EmptyError("No more elements in stack.");
}
return _top->_data;
}
void GenericStack::pop() {
if(isEmpty()) {
throw EmptyError("No more elements in stack.");
}
StackNode* t = _top->_next;
delete _top;
_top = t;
}
bool GenericStack::isEmpty() const {
return !_top;
}
Main.cpp
#include <iostream>
#include "GenericStack.h"
//#define NDEBUG
#include <assert.h>
void ordinaryUsageVerification() {
TStack<int> intStack;
{
std::shared_ptr<int> sh(new int(7));
intStack.push(sh);
intStack.isEmpty();
assert(!intStack.isEmpty() && sh.use_count() == 2);
}
//assert(!intStack.isEmpty() && intStack.top().use_count() == 1);
std::cout << "intStack.top().use_count(): " << intStack.top().use_count() << std::endl;
std::cout << "*gs.top(): " << *intStack.top() << std::endl;
intStack.pop();
assert(intStack.isEmpty());
}
int main() {
ordinaryUsageVerification();
return 0;
}
After the following two line in Main.cpp:
std::shared_ptr<int> sh(new int(7));
intStack.push(sh);
I am expecting intStack.top().use_count()
to be equal 2, but it is equal -1.
I am expecting such a behavior because when calling a push
method I am passing the shared_ptr
by reference, so the use_count
should not change. And only in one place in GenericaStack.h here:
StackNode(const std::shared_ptr<void>& p, StackNode* next)
: _data(p), _next(next) {
The use_count
increments by one for p
.
So, given that before push
I had sh.use_count() == 1
and after intStack.push(sh);
I had sh.use_count() == 2
I should get intStack.top().use_count() == 2
, but what I am getting is intStack.top().use_count() == -1
. Why?
Thank you.
After modifying GenericStack.h in this way:
#ifndef _GENERIC_STACK_TROFIMOV_H_
#define _GENERIC_STACK_TROFIMOV_H_
#include <memory>
class GenericStack {
struct StackNode {
std::shared_ptr<void> _data;
StackNode* _next;
StackNode(std::shared_ptr<void>& p, StackNode* next)
: _data(p), _next(next) {
}
};
StackNode* _top;
GenericStack(const GenericStack&);
GenericStack& operator=(const GenericStack&);
protected:
GenericStack();
~GenericStack();
void push(std::shared_ptr<void>&);
void pop();
std::shared_ptr<void>& top();
bool isEmpty() const;
public:
class EmptyError {
const char* _message;
public:
EmptyError(const char* message)
: _message(message) {
}
const char* getMessage() const {
return _message;
}
};
};
template <class T>
class TStack: private GenericStack {
public:
void push(std::shared_ptr<T>& p) {
GenericStack::push(p);
}
void pop() { GenericStack::pop(); }
std::shared_ptr<T> top() { return std::static_pointer_cast<T>(GenericStack::top()); }
bool isEmpty() const { return GenericStack::isEmpty(); }
};
#endif
I am getting an error:
Error 1 error C2664: 'GenericStack::push' : cannot convert parameter 1 from 'std::shared_ptr<_Ty>' to 'std::shared_ptr<_Ty> &' ...stackgenericstack.h 47 Stack
It is about this part:
void push(std::shared_ptr<T>& p) {
GenericStack::push(p);
}
c++ pointers memory shared-ptr
GenericStack.h
#ifndef _GENERIC_STACK_TROFIMOV_H_
#define _GENERIC_STACK_TROFIMOV_H_
#include <memory>
class GenericStack {
struct StackNode {
std::shared_ptr<void> _data;
StackNode* _next;
StackNode(const std::shared_ptr<void>& p, StackNode* next)
: _data(p), _next(next) {
}
};
StackNode* _top;
GenericStack(const GenericStack&);
GenericStack& operator=(const GenericStack&);
protected:
GenericStack();
~GenericStack();
void push(const std::shared_ptr<void>&);
void pop();
std::shared_ptr<void>& top();
bool isEmpty() const;
public:
class EmptyError {
const char* _message;
public:
EmptyError(const char* message)
: _message(message) {
}
const char* getMessage() const {
return _message;
}
};
};
template <class T>
class TStack: private GenericStack {
public:
void push(const std::shared_ptr<T>& p) { GenericStack::push(p); }
void pop() { GenericStack::pop(); }
std::shared_ptr<T>& top() { return std::static_pointer_cast<T>(GenericStack::top()); }
bool isEmpty() const { return GenericStack::isEmpty(); }
};
#endif
GenerickStack.cpp
#include "GenericStack.h"
GenericStack::GenericStack()
: _top(0) {
};
GenericStack::~GenericStack() {
while(!isEmpty()) {
pop();
}
};
void GenericStack::push(const std::shared_ptr<void>& p) {
_top = new StackNode(p, _top);
}
std::shared_ptr<void>& GenericStack::top() {
if(isEmpty()) {
throw EmptyError("No more elements in stack.");
}
return _top->_data;
}
void GenericStack::pop() {
if(isEmpty()) {
throw EmptyError("No more elements in stack.");
}
StackNode* t = _top->_next;
delete _top;
_top = t;
}
bool GenericStack::isEmpty() const {
return !_top;
}
Main.cpp
#include <iostream>
#include "GenericStack.h"
//#define NDEBUG
#include <assert.h>
void ordinaryUsageVerification() {
TStack<int> intStack;
{
std::shared_ptr<int> sh(new int(7));
intStack.push(sh);
intStack.isEmpty();
assert(!intStack.isEmpty() && sh.use_count() == 2);
}
//assert(!intStack.isEmpty() && intStack.top().use_count() == 1);
std::cout << "intStack.top().use_count(): " << intStack.top().use_count() << std::endl;
std::cout << "*gs.top(): " << *intStack.top() << std::endl;
intStack.pop();
assert(intStack.isEmpty());
}
int main() {
ordinaryUsageVerification();
return 0;
}
After the following two line in Main.cpp:
std::shared_ptr<int> sh(new int(7));
intStack.push(sh);
I am expecting intStack.top().use_count()
to be equal 2, but it is equal -1.
I am expecting such a behavior because when calling a push
method I am passing the shared_ptr
by reference, so the use_count
should not change. And only in one place in GenericaStack.h here:
StackNode(const std::shared_ptr<void>& p, StackNode* next)
: _data(p), _next(next) {
The use_count
increments by one for p
.
So, given that before push
I had sh.use_count() == 1
and after intStack.push(sh);
I had sh.use_count() == 2
I should get intStack.top().use_count() == 2
, but what I am getting is intStack.top().use_count() == -1
. Why?
Thank you.
After modifying GenericStack.h in this way:
#ifndef _GENERIC_STACK_TROFIMOV_H_
#define _GENERIC_STACK_TROFIMOV_H_
#include <memory>
class GenericStack {
struct StackNode {
std::shared_ptr<void> _data;
StackNode* _next;
StackNode(std::shared_ptr<void>& p, StackNode* next)
: _data(p), _next(next) {
}
};
StackNode* _top;
GenericStack(const GenericStack&);
GenericStack& operator=(const GenericStack&);
protected:
GenericStack();
~GenericStack();
void push(std::shared_ptr<void>&);
void pop();
std::shared_ptr<void>& top();
bool isEmpty() const;
public:
class EmptyError {
const char* _message;
public:
EmptyError(const char* message)
: _message(message) {
}
const char* getMessage() const {
return _message;
}
};
};
template <class T>
class TStack: private GenericStack {
public:
void push(std::shared_ptr<T>& p) {
GenericStack::push(p);
}
void pop() { GenericStack::pop(); }
std::shared_ptr<T> top() { return std::static_pointer_cast<T>(GenericStack::top()); }
bool isEmpty() const { return GenericStack::isEmpty(); }
};
#endif
I am getting an error:
Error 1 error C2664: 'GenericStack::push' : cannot convert parameter 1 from 'std::shared_ptr<_Ty>' to 'std::shared_ptr<_Ty> &' ...stackgenericstack.h 47 Stack
It is about this part:
void push(std::shared_ptr<T>& p) {
GenericStack::push(p);
}
c++ pointers memory shared-ptr
c++ pointers memory shared-ptr
edited Nov 25 '18 at 14:37
yourbuddy
asked Nov 25 '18 at 12:31
yourbuddyyourbuddy
334
334
What possessed you to return a reference here?
– StoryTeller
Nov 25 '18 at 13:09
1
Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please first extract a Minimal, Complete, and Verifiable example. In particular, there shouldn't be any need to split this into three files!
– Ulrich Eckhardt
Nov 25 '18 at 14:41
Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please first extract a Minimal, Complete, and Verifiable example. In particular, there shouldn't be any need to split this into three files!
– Ulrich Eckhardt
Nov 25 '18 at 14:41
add a comment |
What possessed you to return a reference here?
– StoryTeller
Nov 25 '18 at 13:09
1
Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please first extract a Minimal, Complete, and Verifiable example. In particular, there shouldn't be any need to split this into three files!
– Ulrich Eckhardt
Nov 25 '18 at 14:41
Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please first extract a Minimal, Complete, and Verifiable example. In particular, there shouldn't be any need to split this into three files!
– Ulrich Eckhardt
Nov 25 '18 at 14:41
What possessed you to return a reference here?
– StoryTeller
Nov 25 '18 at 13:09
What possessed you to return a reference here?
– StoryTeller
Nov 25 '18 at 13:09
1
1
Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please first extract a Minimal, Complete, and Verifiable example. In particular, there shouldn't be any need to split this into three files!
– Ulrich Eckhardt
Nov 25 '18 at 14:41
Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please first extract a Minimal, Complete, and Verifiable example. In particular, there shouldn't be any need to split this into three files!
– Ulrich Eckhardt
Nov 25 '18 at 14:41
Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please first extract a Minimal, Complete, and Verifiable example. In particular, there shouldn't be any need to split this into three files!
– Ulrich Eckhardt
Nov 25 '18 at 14:41
Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please first extract a Minimal, Complete, and Verifiable example. In particular, there shouldn't be any need to split this into three files!
– Ulrich Eckhardt
Nov 25 '18 at 14:41
add a comment |
1 Answer
1
active
oldest
votes
Let's review the specification of std::static_pointer_cast
: std::static_pointer_cast
() returns an rvalue, a.k.a. a temporary object.
std::shared_ptr<T>& top() { return std::static_pointer_cast<T>(GenericStack::top()); }
This returns a reference to a temporary object that gets destroyed by the time this top()
returns. Undefined behavior. Most modern C++ compilers are generally capable of detecting this common instance of undefined behavior, and your compiler should be barking at you, on this line.
In general, attempts to defeat C++'s type-safety, by casting things back and forth from a void *
(the apparent underlying purpose of this set of templates) -- that never ends well.
It's not "defeating type safety". Code that uses this stack will always get back the type they put into it. It doesn't mean the actual stack logic needs to be templated on the type. We use type erasure all the time to make sure only the truly mandatory type information is generic.
– StoryTeller
Nov 25 '18 at 13:04
1
And as for the return type, the only way that lvalue reference can ever bind to an rvalue is in MSVC and its damn extensions. It's worth mentioning that.
– StoryTeller
Nov 25 '18 at 13:05
I updated my question. Thank you.
– yourbuddy
Nov 25 '18 at 14:37
Temporaries do not bind to mutable references, only toconst
references, that's the reason for the push error.
– Sam Varshavchik
Nov 25 '18 at 17:20
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%2f53467462%2fuse-count-becomes-1-when-using-shared-ptr-in-c%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
Let's review the specification of std::static_pointer_cast
: std::static_pointer_cast
() returns an rvalue, a.k.a. a temporary object.
std::shared_ptr<T>& top() { return std::static_pointer_cast<T>(GenericStack::top()); }
This returns a reference to a temporary object that gets destroyed by the time this top()
returns. Undefined behavior. Most modern C++ compilers are generally capable of detecting this common instance of undefined behavior, and your compiler should be barking at you, on this line.
In general, attempts to defeat C++'s type-safety, by casting things back and forth from a void *
(the apparent underlying purpose of this set of templates) -- that never ends well.
It's not "defeating type safety". Code that uses this stack will always get back the type they put into it. It doesn't mean the actual stack logic needs to be templated on the type. We use type erasure all the time to make sure only the truly mandatory type information is generic.
– StoryTeller
Nov 25 '18 at 13:04
1
And as for the return type, the only way that lvalue reference can ever bind to an rvalue is in MSVC and its damn extensions. It's worth mentioning that.
– StoryTeller
Nov 25 '18 at 13:05
I updated my question. Thank you.
– yourbuddy
Nov 25 '18 at 14:37
Temporaries do not bind to mutable references, only toconst
references, that's the reason for the push error.
– Sam Varshavchik
Nov 25 '18 at 17:20
add a comment |
Let's review the specification of std::static_pointer_cast
: std::static_pointer_cast
() returns an rvalue, a.k.a. a temporary object.
std::shared_ptr<T>& top() { return std::static_pointer_cast<T>(GenericStack::top()); }
This returns a reference to a temporary object that gets destroyed by the time this top()
returns. Undefined behavior. Most modern C++ compilers are generally capable of detecting this common instance of undefined behavior, and your compiler should be barking at you, on this line.
In general, attempts to defeat C++'s type-safety, by casting things back and forth from a void *
(the apparent underlying purpose of this set of templates) -- that never ends well.
It's not "defeating type safety". Code that uses this stack will always get back the type they put into it. It doesn't mean the actual stack logic needs to be templated on the type. We use type erasure all the time to make sure only the truly mandatory type information is generic.
– StoryTeller
Nov 25 '18 at 13:04
1
And as for the return type, the only way that lvalue reference can ever bind to an rvalue is in MSVC and its damn extensions. It's worth mentioning that.
– StoryTeller
Nov 25 '18 at 13:05
I updated my question. Thank you.
– yourbuddy
Nov 25 '18 at 14:37
Temporaries do not bind to mutable references, only toconst
references, that's the reason for the push error.
– Sam Varshavchik
Nov 25 '18 at 17:20
add a comment |
Let's review the specification of std::static_pointer_cast
: std::static_pointer_cast
() returns an rvalue, a.k.a. a temporary object.
std::shared_ptr<T>& top() { return std::static_pointer_cast<T>(GenericStack::top()); }
This returns a reference to a temporary object that gets destroyed by the time this top()
returns. Undefined behavior. Most modern C++ compilers are generally capable of detecting this common instance of undefined behavior, and your compiler should be barking at you, on this line.
In general, attempts to defeat C++'s type-safety, by casting things back and forth from a void *
(the apparent underlying purpose of this set of templates) -- that never ends well.
Let's review the specification of std::static_pointer_cast
: std::static_pointer_cast
() returns an rvalue, a.k.a. a temporary object.
std::shared_ptr<T>& top() { return std::static_pointer_cast<T>(GenericStack::top()); }
This returns a reference to a temporary object that gets destroyed by the time this top()
returns. Undefined behavior. Most modern C++ compilers are generally capable of detecting this common instance of undefined behavior, and your compiler should be barking at you, on this line.
In general, attempts to defeat C++'s type-safety, by casting things back and forth from a void *
(the apparent underlying purpose of this set of templates) -- that never ends well.
answered Nov 25 '18 at 12:40
Sam VarshavchikSam Varshavchik
63.3k53581
63.3k53581
It's not "defeating type safety". Code that uses this stack will always get back the type they put into it. It doesn't mean the actual stack logic needs to be templated on the type. We use type erasure all the time to make sure only the truly mandatory type information is generic.
– StoryTeller
Nov 25 '18 at 13:04
1
And as for the return type, the only way that lvalue reference can ever bind to an rvalue is in MSVC and its damn extensions. It's worth mentioning that.
– StoryTeller
Nov 25 '18 at 13:05
I updated my question. Thank you.
– yourbuddy
Nov 25 '18 at 14:37
Temporaries do not bind to mutable references, only toconst
references, that's the reason for the push error.
– Sam Varshavchik
Nov 25 '18 at 17:20
add a comment |
It's not "defeating type safety". Code that uses this stack will always get back the type they put into it. It doesn't mean the actual stack logic needs to be templated on the type. We use type erasure all the time to make sure only the truly mandatory type information is generic.
– StoryTeller
Nov 25 '18 at 13:04
1
And as for the return type, the only way that lvalue reference can ever bind to an rvalue is in MSVC and its damn extensions. It's worth mentioning that.
– StoryTeller
Nov 25 '18 at 13:05
I updated my question. Thank you.
– yourbuddy
Nov 25 '18 at 14:37
Temporaries do not bind to mutable references, only toconst
references, that's the reason for the push error.
– Sam Varshavchik
Nov 25 '18 at 17:20
It's not "defeating type safety". Code that uses this stack will always get back the type they put into it. It doesn't mean the actual stack logic needs to be templated on the type. We use type erasure all the time to make sure only the truly mandatory type information is generic.
– StoryTeller
Nov 25 '18 at 13:04
It's not "defeating type safety". Code that uses this stack will always get back the type they put into it. It doesn't mean the actual stack logic needs to be templated on the type. We use type erasure all the time to make sure only the truly mandatory type information is generic.
– StoryTeller
Nov 25 '18 at 13:04
1
1
And as for the return type, the only way that lvalue reference can ever bind to an rvalue is in MSVC and its damn extensions. It's worth mentioning that.
– StoryTeller
Nov 25 '18 at 13:05
And as for the return type, the only way that lvalue reference can ever bind to an rvalue is in MSVC and its damn extensions. It's worth mentioning that.
– StoryTeller
Nov 25 '18 at 13:05
I updated my question. Thank you.
– yourbuddy
Nov 25 '18 at 14:37
I updated my question. Thank you.
– yourbuddy
Nov 25 '18 at 14:37
Temporaries do not bind to mutable references, only to
const
references, that's the reason for the push error.– Sam Varshavchik
Nov 25 '18 at 17:20
Temporaries do not bind to mutable references, only to
const
references, that's the reason for the push error.– Sam Varshavchik
Nov 25 '18 at 17:20
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%2f53467462%2fuse-count-becomes-1-when-using-shared-ptr-in-c%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
What possessed you to return a reference here?
– StoryTeller
Nov 25 '18 at 13:09
1
Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please first extract a Minimal, Complete, and Verifiable example. In particular, there shouldn't be any need to split this into three files!
– Ulrich Eckhardt
Nov 25 '18 at 14:41
Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, please first extract a Minimal, Complete, and Verifiable example. In particular, there shouldn't be any need to split this into three files!
– Ulrich Eckhardt
Nov 25 '18 at 14:41