- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下代码示例。
它创建类 A(使用模板参数 T
和 Args...
)并将其构造函数参数保存到元组 args_
中。稍后它使用 args_
在 get
方法中创建 T
的实例。
一切正常,除了,如您所见,我确实在 std::apply
中使用了 my_own_make_shared
函数,只是因为如果我将其替换为 std::make_shared
我无法编译这段代码。有人知道问题出在哪里吗?
#include <iostream>
#include <charconv>
#include <array>
#include <cstring>
#include <iostream>
#include <tuple>
#include <utility>
#include <memory>
#include <algorithm>
struct Base
{
virtual void foo() = 0;
};
struct Test : Base
{
Test(int, int) {}
void foo() override {std::cout << "Test\n";}
};
struct Test2 : Base
{
Test2(int, int, const std::string&) {}
void foo() override {std::cout << "Test2\n";}
};
template<typename T, typename... Args>
std::shared_ptr<T> my_own_make_shared(Args... args)
{
return std::make_shared<T>(args...);
}
template <typename T, typename... Args>
struct A
{
A(Args... args) : args_(std::make_tuple(args...)) {}
std::shared_ptr<Base> get()
{
return std::apply(my_own_make_shared<T, Args...>, args_);
}
std::tuple<Args...> args_;
};
template <typename T, typename... Args>
auto make_A(Args... args)
{
return A<T, Args...>(args...);
}
int main(int argc, char** argv)
{
make_A<Test>(1, 2).get()->foo();
make_A<Test2>(1, 2, "").get()->foo();
}
编译器错误 (GCC-9)。假设我们将 my_own_make_shared
替换为 std::make_shared
In file included from test.cpp:6:
/usr/include/c++/9/tuple: In instantiation of ‘constexpr decltype(auto) std::__apply_impl(_Fn&&, _Tuple&&, std::index_sequence<_Idx ...>) [with _Fn = std::shared_ptr<Test> (&)(int&&, int&&); _Tuple = std::tuple<int, int>&; long unsigned int ..._Idx = {0, 1}; std::index_sequence<_Idx ...> = std::integer_sequence<long unsigned int, 0, 1>]’:
/usr/include/c++/9/tuple:1694:31: required from ‘constexpr decltype(auto) std::apply(_Fn&&, _Tuple&&) [with _Fn = std::shared_ptr<Test> (&)(int&&, int&&); _Tuple = std::tuple<int, int>&]’
test.cpp:41:26: required from ‘std::shared_ptr<Base> A<T, Args>::get() [with T = Test; Args = {int, int}]’
test.cpp:55:28: required from here
/usr/include/c++/9/tuple:1684:27: error: no matching function for call to ‘__invoke(std::shared_ptr<Test> (&)(int&&, int&&), std::__tuple_element_t<0, std::tuple<int, int> >&, std::__tuple_element_t<1, std::tuple<int, int> >&)’
1684 | return std::__invoke(std::forward<_Fn>(__f),
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
1685 | std::get<_Idx>(std::forward<_Tuple>(__t))...);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/tuple:41,
from test.cpp:6:
/usr/include/c++/9/bits/invoke.h:89:5: note: candidate: ‘template<class _Callable, class ... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...)’
89 | __invoke(_Callable&& __fn, _Args&&... __args)
| ^~~~~~~~
/usr/include/c++/9/bits/invoke.h:89:5: note: template argument deduction/substitution failed:
/usr/include/c++/9/bits/invoke.h: In substitution of ‘template<class _Callable, class ... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...) [with _Callable = std::shared_ptr<Test> (&)(int&&, int&&); _Args = {int&, int&}]’:
/usr/include/c++/9/tuple:1684:27: required from ‘constexpr decltype(auto) std::__apply_impl(_Fn&&, _Tuple&&, std::index_sequence<_Idx ...>) [with _Fn = std::shared_ptr<Test> (&)(int&&, int&&); _Tuple = std::tuple<int, int>&; long unsigned int ..._Idx = {0, 1}; std::index_sequence<_Idx ...> = std::integer_sequence<long unsigned int, 0, 1>]’
/usr/include/c++/9/tuple:1694:31: required from ‘constexpr decltype(auto) std::apply(_Fn&&, _Tuple&&) [with _Fn = std::shared_ptr<Test> (&)(int&&, int&&); _Tuple = std::tuple<int, int>&]’
test.cpp:41:26: required from ‘std::shared_ptr<Base> A<T, Args>::get() [with T = Test; Args = {int, int}]’
test.cpp:55:28: required from here
/usr/include/c++/9/bits/invoke.h:89:5: error: no type named ‘type’ in ‘struct std::__invoke_result<std::shared_ptr<Test> (&)(int&&, int&&), int&, int&>’
test.cpp: In instantiation of ‘std::shared_ptr<Base> A<T, Args>::get() [with T = Test; Args = {int, int}]’:
test.cpp:55:28: required from here
test.cpp:41:26: error: could not convert ‘std::apply<std::shared_ptr<Test> (&)(int&&, int&&), std::tuple<int, int>&>(std::make_shared<Test, int, int>, ((A<Test, int, int>*)this)->A<Test, int, int>::args_)’ from ‘void’ to ‘std::shared_ptr<Base>’
41 | return std::apply(std::make_shared<T, Args...>, args_);
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| void
In file included from test.cpp:6:
/usr/include/c++/9/tuple: In instantiation of ‘constexpr decltype(auto) std::__apply_impl(_Fn&&, _Tuple&&, std::index_sequence<_Idx ...>) [with _Fn = std::shared_ptr<Test2> (&)(int&&, int&&, const char*&&); _Tuple = std::tuple<int, int, const char*>&; long unsigned int ..._Idx = {0, 1, 2}; std::index_sequence<_Idx ...> = std::integer_sequence<long unsigned int, 0, 1, 2>]’:
/usr/include/c++/9/tuple:1694:31: required from ‘constexpr decltype(auto) std::apply(_Fn&&, _Tuple&&) [with _Fn = std::shared_ptr<Test2> (&)(int&&, int&&, const char*&&); _Tuple = std::tuple<int, int, const char*>&]’
test.cpp:41:26: required from ‘std::shared_ptr<Base> A<T, Args>::get() [with T = Test2; Args = {int, int, const char*}]’
test.cpp:56:33: required from here
/usr/include/c++/9/tuple:1684:27: error: no matching function for call to ‘__invoke(std::shared_ptr<Test2> (&)(int&&, int&&, const char*&&), std::__tuple_element_t<0, std::tuple<int, int, const char*> >&, std::__tuple_element_t<1, std::tuple<int, int, const char*> >&, const char*&)’
1684 | return std::__invoke(std::forward<_Fn>(__f),
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
1685 | std::get<_Idx>(std::forward<_Tuple>(__t))...);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/tuple:41,
from test.cpp:6:
/usr/include/c++/9/bits/invoke.h:89:5: note: candidate: ‘template<class _Callable, class ... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...)’
89 | __invoke(_Callable&& __fn, _Args&&... __args)
| ^~~~~~~~
/usr/include/c++/9/bits/invoke.h:89:5: note: template argument deduction/substitution failed:
/usr/include/c++/9/bits/invoke.h: In substitution of ‘template<class _Callable, class ... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...) [with _Callable = std::shared_ptr<Test2> (&)(int&&, int&&, const char*&&); _Args = {int&, int&, const char*&}]’:
/usr/include/c++/9/tuple:1684:27: required from ‘constexpr decltype(auto) std::__apply_impl(_Fn&&, _Tuple&&, std::index_sequence<_Idx ...>) [with _Fn = std::shared_ptr<Test2> (&)(int&&, int&&, const char*&&); _Tuple = std::tuple<int, int, const char*>&; long unsigned int ..._Idx = {0, 1, 2}; std::index_sequence<_Idx ...> = std::integer_sequence<long unsigned int, 0, 1, 2>]’
/usr/include/c++/9/tuple:1694:31: required from ‘constexpr decltype(auto) std::apply(_Fn&&, _Tuple&&) [with _Fn = std::shared_ptr<Test2> (&)(int&&, int&&, const char*&&); _Tuple = std::tuple<int, int, const char*>&]’
test.cpp:41:26: required from ‘std::shared_ptr<Base> A<T, Args>::get() [with T = Test2; Args = {int, int, const char*}]’
test.cpp:56:33: required from here
/usr/include/c++/9/bits/invoke.h:89:5: error: no type named ‘type’ in ‘struct std::__invoke_result<std::shared_ptr<Test2> (&)(int&&, int&&, const char*&&), int&, int&, const char*&>’
test.cpp: In instantiation of ‘std::shared_ptr<Base> A<T, Args>::get() [with T = Test2; Args = {int, int, const char*}]’:
test.cpp:56:33: required from here
test.cpp:41:26: error: could not convert ‘std::apply<std::shared_ptr<Test2> (&)(int&&, int&&, const char*&&), std::tuple<int, int, const char*>&>(std::make_shared<Test2, int, int, const char*>, ((A<Test2, int, int, const char*>*)this)->A<Test2, int, int, const char*>::args_)’ from ‘void’ to ‘std::shared_ptr<Base>’
41 | return std::apply(std::make_shared<T, Args...>, args_);
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| void
最佳答案
下面是 std::apply
的实现取自 reference
namespace detail {
template <class F, class Tuple, std::size_t... I>
constexpr decltype(auto) apply_impl(F&& f, Tuple&& t, std::index_sequence<I...>)
{
// This implementation is valid since C++20 (via P1065R2)
// In C++17, a constexpr counterpart of std::invoke is actually needed here
return std::invoke(std::forward<F>(f), std::get<I>(std::forward<Tuple>(t))...);
}
} // namespace detail
我们可以看到仿函数及其参数作为元组元素被转发。
在你的情况下 args_
是Lvaue。所以通过 std::get<I>(std::forward<Tuple>(t))...
您正在获取对特定元组元素的左值引用。
为了
make_A<Test>(1, 2).get()->foo();
参数包Args...
推导为:{int,int}
以及以下内容
return std::apply(std::make_shared<T, Args...>, args_);
无法编译,因为R 值引用无法绑定(bind)到 Lvalues。 R 值引用出现在 make_shared
上参数列表。你用这个overload :
template< class T, class... Args >
shared_ptr<T> make_shared( Args&&... args );
哪里Args...
是{int,int}
, 所以参数列表是 int&&, int&&
.
如果你想使用元组的元素作为 apply as Lvalue 的参数,只需将 Args 转换为 Lvalues:
return std::apply(std::make_shared<T, std::add_lvalue_reference_t<Args>...>, args_);
关于c++ - std::make_shared 在 std::apply 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60135461/
我在 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 但没有任何异常的情况? 最佳答案 来
我是一名优秀的程序员,十分优秀!