考虑:
#include <cstdlib>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
class Gizmo
{
public:
Gizmo() : foo_(shared_ptr<string>(new string("bar"))) {};
Gizmo(Gizmo&& rhs); // Implemented Below
private:
shared_ptr<string> foo_;
};
/*
// doesn't use std::move
Gizmo::Gizmo(Gizmo&& rhs)
: foo_(rhs.foo_)
{
}
*/
// Does use std::move
Gizmo::Gizmo(Gizmo&& rhs)
: foo_(std::move(rhs.foo_))
{
}
int main()
{
typedef vector<Gizmo> Gizmos;
Gizmos gizmos;
generate_n(back_inserter(gizmos), 10000, []() -> Gizmo
{
Gizmo ret;
return ret;
});
random_shuffle(gizmos.begin(), gizmos.end());
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,有两个版本Gizmo::Gizmo(Gizmo&&) …
我在shared_ptr继承类之间进行自动类型转换时遇到了一些问题.
我的类结构如下,一个基类Base和两个派生类Derived1和Derived2.
// Base class
class Base {
protected:
...
...
public:
Base() = default;
virtual ~Base() = default;
virtual void run() = 0;
...
...
};
// Derived class
class Derived1: Base {
protected:
...
...
public:
Derived1() = default;
virtual ~Derived1() = default;
void run() {...}
...
...
};
// Derived class
class Derived2: Base {
protected:
...
...
public:
Derived2() = default;
virtual ~Derived2() = default;
void run() {...} …Run Code Online (Sandbox Code Playgroud) 说我有struct这样的:
struct S
{
int i;
double d;
std::string s;
};
Run Code Online (Sandbox Code Playgroud)
我可以这样做吗?
std::make_shared<S>(1, 2.1, "Hello")
Run Code Online (Sandbox Code Playgroud) 我无法理解如何std::enable_shared_from_this::shared_from_this返回与现有指针共享所有权的共享pinter.换句话说,你做这个:
std::shared_ptr<Foo> getFoo() { return shared_from_this(); }
Run Code Online (Sandbox Code Playgroud)
因此,当你打电话时,getFoo它是如何得到另一个shared_ptr共享所有权的另一个,而不是创建一个shared_ptr拥有相同的独立this.
我需要理解这一点,以便能够理解如何从某个对象创建shared_ptr,这些对象都会增加相同的引用计数而不会初始化单独的shared_ptrs.
这个问题是关于拥有指针,使用指针,智能指针,向量和分配器的。
我对代码体系结构的想法有些迷茫。此外,如果这个问题在某个地方已经有答案,1.抱歉,但是到目前为止我还没有找到满意的答案,并且2.请指出。
我的问题如下:
我有一个存储在向量中的“事物”,以及这些“事物”的几个“消费者”。因此,我的第一次尝试如下所示:
std::vector<thing> i_am_the_owner_of_things;
thing* get_thing_for_consumer() {
// some thing-selection logic
return &i_am_the_owner_of_things[5]; // 5 is just an example
}
...
// somewhere else in the code:
class consumer {
consumer() {
m_thing = get_thing_for_consumer();
}
thing* m_thing;
};
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,这是安全的,因为在任何情况下,“事物”的寿命都超过了“消费者”。但是,可以在运行时添加更多的“事物”,这可能会成为问题,因为如果std::vector<thing> i_am_the_owner_of_things;重新分配了这些事物,则所有thing* m_thing指针都将变为无效。
一种解决方案是将唯一的指针存储到“事物”,而不是直接存储“事物”,即如下所示:
std::vector<std::unique_ptr<thing>> i_am_the_owner_of_things;
thing* get_thing_for_consumer() {
// some thing-selection logic
return i_am_the_owner_of_things[5].get(); // 5 is just an example
}
...
// somewhere else in the code:
class consumer {
consumer() {
m_thing …Run Code Online (Sandbox Code Playgroud) tr1::shared_ptr和之间有什么区别boost::shared_ptr吗?如果是这样,什么?
我试图比较原始指针,boost shared_ptr和boost weak_ptr之间的性能.在解除引用部分,我期望shared_ptr和raw_ptr相等,但结果显示shared_ptr大约慢两倍.对于测试,我创建一个数组,其中包含指针或指向int的共享指针,然后在循环中取消引用,如下所示:
int result;
for(int i = 0; i != 100; ++i)
{
for(int i = 0; i != SIZE; ++i)
result += *array[i];
}
Run Code Online (Sandbox Code Playgroud)
测试的完整代码可以在这里找到:https: //github.com/coolfluid/coolfluid3/blob/master/test/common/utest-ptr-benchmark.cpp
可以在此处找到没有断言的优化构建的测试时间:http://coolfluidsrv.vki.ac.be/cdash/testDetails.php? test = 145592&build = 7777
感兴趣的值是"DerefShared time"和"DerefRaw time"
我猜测试可能会有某种方面的缺陷,但我没有弄清楚差异来自哪里.分析显示来自shared_ptr的operator*内联,它似乎需要更多时间.我仔细检查了升压断言是否已关闭.
如果有人能解释可能产生的差异,我将非常感激.
其他独立测试:https: //gist.github.com/1335014
我在使用boost enable_shared_from_this和多重继承时遇到了一些麻烦.
该场景可以描述如下:
类A实现了一些功能,应该继承enable_shared_from_this
类B实现了另一个功能,应该继承enable_shared_from_this
类D从A和B(class D : public A, public B {})继承功能
当我B从类中使用一些类功能时,D我得到了一个异常(bad_weak_ptr)
继承enable_shared_from_this课程D对我来说不是一个选择
我不知道如何解决这个问题.
哦,我正在使用Visual C++ 2010.
我有以下代码:
void MyClass::onOpenModalBtnClicked() {
uiManager->load(L"data/ui/testmodal.json");
std::shared_ptr<UIElement> modal = uiManager->getElementById("loginModal");
if(modal) {
modal->getElementById("closeButton")->onClicked = [modal]() {
modal->hide();
};
}
}
Run Code Online (Sandbox Code Playgroud)
这个工作正常,点击按钮时模态关闭,onClicked是std::function.
我的应用程序开头也有这个:
#if defined(DEBUG) | defined (_DEBUG)
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
Run Code Online (Sandbox Code Playgroud)
这会在应用程序终止时打印出内存泄漏.
使用上面的代码我得到了很多内存泄漏,如果我将代码更改为以下代码,它们都会消失:
void MyClass::onOpenModalBtnClicked() {
uiManager->load(L"data/ui/testmodal.json");
std::shared_ptr<UIElement> modal = uiManager->getElementById("loginModal");
if(modal) {
modal->getElementById("closeButton")->onClicked = [this]() {
uiManager->getElementById("loginModal")->hide();
};
}
}
Run Code Online (Sandbox Code Playgroud)
我假设传递in shared_ptr值会使ref计数增加1,然后这个引用永远不会超出范围,或者在报告mem泄漏后它超出范围.所以我在使用shared_ptr之后尝试在lambda中调用reset但是后来我得到了这个编译错误:
Error 1 error C2662: 'void std::shared_ptr<_Ty>::reset(void) throw()' : cannot convert 'this' pointer from 'const std::shared_ptr<_Ty>' to 'std::shared_ptr<_Ty> &'
所以问题是如何使用捕获modal而不是那些内存泄漏?
编辑: 所以我通过添加 …
试试以下代码:
#include <functional>
#include <memory>
class C {
public:
void F(std::function<void(std::shared_ptr<void>)>){}
void F(std::function<void(std::shared_ptr<int>)>){}
};
int main(){
C c;
c.F([](std::shared_ptr<void>) {});
}
Run Code Online (Sandbox Code Playgroud)
您将看到编译错误:
prog.cc:12:7: error: call to member function 'F' is ambiguous
c.F([](std::shared_ptr<void>) {});
~~^
prog.cc:6:10: note: candidate function
void F(std::function<void(std::shared_ptr<void>)>){}
^
prog.cc:7:10: note: candidate function
void F(std::function<void(std::shared_ptr<int>)>){}
^
Run Code Online (Sandbox Code Playgroud)
有没有办法解决这种模棱两可的问题?也许与SFINAE?
c++ ×10
shared-ptr ×10
c++11 ×5
boost ×3
allocator ×1
inheritance ×1
lambda ×1
overloading ×1
std ×1
tr1 ×1
unique-ptr ×1