- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
当类具有默认构造函数时,我可以使用 std::make_shared
的实例化,就像使用指向函数的指针 一样。这可能是因为实例化的模板必须编译并存储在内存中,并且它的地址必须存在。
#include <memory>
#include <functional>
class DefaultConstructible
{
};
typedef std::function<std::shared_ptr<DefaultConstructible>()> Generator;
int main()
{
Generator generator(std::make_shared<DefaultConstructible>);
std::shared_ptr<DefaultConstructible> defConst = generator();
return 0;
}
但是当我添加一个非平凡的构造函数时,同样的事情失败了:
#include <memory>
#include <functional>
class FromInt
{
public:
FromInt(int a):a_(a){}
int a_;
};
typedef std::function<std::shared_ptr<FromInt>(int)> Generator;
int main()
{
Generator generator(std::make_shared<FromInt>);
std::shared_ptr<FromInt> p = generator(2);
return 0;
}
我得到一个编译器错误:
error: no matching function for call to
'std::function<std::shared_ptr<FromInt>(int)>::function(<unresolved overloaded function type>)'
Generator g(std::make_shared<FromInt>);
^
为什么会这样,我怎样才能编译我的代码?
最佳答案
您只需要明确说明您希望它使用哪个构造函数:
Generator generator(std::make_shared<FromInt, int>);
“额外的”模板参数对应于构造函数参数。
关于c++ - 为什么我不能使用 std::make_shared 的实例化作为指向函数的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21675068/
我在 boost::asio 中做了一个 echo-server 示例。但是使用 boost::make_shard 会导致“未知异常”,而 std::make_shared 则不会。请参阅注释行。
我一直在做一些现场性能测试 1>std::shared_ptr, std::make_shared based on 'gcc 4.7.2' & 'VC10 implementation' 2>boo
问题 1> Line 1 的使用率真的比 Line 2 好吗? boost::shared_ptr shpStr = boost::make_shared(); // Line 1 boost::sh
正如 Bjarne Stroustrup 的“C++ 之旅”中所述,作为一种已知的 C++14 实践,人们应该避免在代码中使用裸露的 new 和 delete。标准库提供 std::make_shar
这可能是重复的,但我无法在任何地方找到解决方案。 我有这样的源代码: struct Blob{ //... static void *operator new(size_t si
我有一个带有模板构造函数的类,并且想要一个 shared_pointer给它。如: class A; typedef std::shared_ptr A_ptr; class A { public:
将 VS 2015 与 v120 结合使用。 所以我得到了内存异常,因为当我在一个已经构造的对象上调用 make_shared() 时。已经构造的对象有一个指向另一个用 new 分配的对象的指针,所以
在this回答 T.C.州 boost::make_shared etc. support array types - either one of unknown size, or one of fi
虽然我有std::tr1::shared_ptr在我的编译器中可用,我不有make_shared . 谁能告诉我如何正确实现 make_shared ?我懂了我需要使用可变参数来为 T 的构造函数提供
我正在尝试自己实现 shared_ptr。 make_shared 有问题。 std::make_shared 的主要特点是它在连续的内存块中分配计数器 block 和对象。我怎样才能做同样的事情?
除非定义了 B0RKEN(就像命令行上的 -DB0RKEN 一样),否则编译以下内容: #include #include #include using boost::shared_ptr; u
有什么方法可以使用 make_shared 而不是 shared_ptr 作为抽象类型? 例子: #include #include class Foo { public: virtual
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我明白了: shared_ptr x = make_shared(); 效率高于: shared_ptr x(new X()); 而且我了解其中的优势。但是,我不明白为什么编译器不能有这样的规则 "i
使用 gcc 4.6.2,如果构造函数抛出异常,make_shared() 会给出无用的回溯(显然是由于某些重新抛出)。我正在使用 make_shared() 来节省一些输入,但这是显示停止器。我创建
从 C++11 开始,由于多种原因,开发人员倾向于将智能指针类用于动态生命周期对象。对于那些新的智能指针类、标准,甚至建议不要使用像 new 这样的运算符,而是建议使用 make_shared 或 m
从理论上讲,make_shared() 和shared_ptr 之间的一个区别是内存分配技术。 make_shared() 应该使用一个 block ,而 shared_ptr 应该使用两个 bloc
为了规避restriction on partially supplied explicit template arguments ,我将要从中推导出类模板参数 ( Internal ) 的结构嵌
为什么这个 make_shared 在两个单独的调用中分配相同的内存地址? typedef struct { int a; int b; int c; } test_t; vo
我最近正在处理 shared_ptr 的问题。我很好奇如果 make_shared 失败了,它会引发异常吗?是否存在 make_shared 返回 nullptr 但没有任何异常的情况? 最佳答案 来
我是一名优秀的程序员,十分优秀!