Reputation: 47
It is some kind of factory lambda. The class to be instantiated by the lambda receives a reference to another class in its constructor. If the latter declares a unique_ptr member, a really long error is thrown:
#include <memory>
#include <string>
struct A
{
std::unique_ptr<int> foo;
};
struct B
{
B(const std::string & str, A&a) {}
};
template<typename T, typename ... Args>
auto registerType(const std::string & type, Args&& ... args)
{
return [args = std::make_tuple(std::forward<Args>(args) ...)]() mutable -> auto
{
return std::apply([](auto&& ... args){
return std::make_unique<T>(args ...);
}, std::move(args));
};
}
int main()
{
A a;
registerType<B>("lala", "p1", a)();
}
Removing the unique_ptr member works fine. Passing a pointer to 'a' instead of a reference also works fine.
Error output is long, better to see it in action:
https://godbolt.org/z/n896qMz59
This happens at least with:
Any ideas?
Thanks
Upvotes: 1
Views: 167
Reputation: 117298
You can't copy a unique_ptr
.
But you can move it:
#include <utility>
// ...
registerType<B>("lala", "p1", std::move(a))();
or prevent the std::decay
when using make_tuple
from happening:
return [args = std::tuple<Args...>(std::forward<Args>(args) ...)]() mutable -> auto
Upvotes: 3