我正在尝试为不可复制、不可移动的类制作一个可移动的包装器,但是我在将 const std::string
变量传递给构造函数时遇到问题。下面的最小示例会产生以下错误:
#include <iostream>
#include <memory>
#include <string>
#include <utility>
struct X {
std::string x;
X(const std::string &x) : x(x) {}
X(const X &x) = delete;
X(X &&x) = delete;
};
struct Wrapper {
std::unique_ptr<X> x;
Wrapper(const Wrapper & wrapper) = delete;
Wrapper(Wrapper && wrapper) = default;
template<typename... Args>
Wrapper(Args&&... args) : x(std::make_unique<X>(std::forward(args)...)) {}
};
int main() {
const std::string XXX = "XXX";
Wrapper w{XXX};
std::cout << w.x->x << std::endl;
}
这里的错误信息:
forwarding.cc:21:53: error: no matching function for call to 'forward'
Wrapper(Args&&... args) : x(std::make_unique<X>(std::forward(args)...)) {}
^~~~~~~~~~~~
forwarding.cc:26:13: note: in instantiation of function template specialization 'Wrapper::Wrapper<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > &>' requested here
Wrapper w{XXX};
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/bits/move.h:73:5: note: candidate template ignored: couldn't infer template argument '_Tp'
forward(typename std::remove_reference<_Tp>::type& __t) noexcept
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/bits/move.h:84:5: note: candidate template ignored: couldn't infer template argument '_Tp'
forward(typename std::remove_reference<_Tp>::type&& __t) noexcept
^
1 error generated.
最佳答案
您需要明确地将模板参数传递给std::forward
:
std::forward<Args>(args)...
这是因为 std::forward
需要某种方式来了解 args...
的“原始值类别”,而通过 template 参数这是不可能的单独扣除,因为args
始终是左值。
在转发引用的模板参数推导上下文中,左值将推导为左值引用(作为特殊规则),因此std::forward
可以通过查看 Args...
中的类型来完成它的工作。
关于c++ - 没有用于使用可变参数调用 std::forward(const std::string &) 的匹配函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48501797/