简体   繁体   English

没有与可变参数调用std :: forward(const std :: string&)的匹配函数

[英]No matching function for call std::forward(const std::string &) with variadic arguments

I'm trying to make a movable wrapper to non-copyable, non-movable class, however I have a problem passing a const std::string variable to the constructor. 我试图将一个可移动的包装器设置为不可复制的,不可移动的类,但是我将const std::string变量传递给构造const std::string遇到了问题。 The minimal example below produces following error: 下面的最小示例产生以下错误:

#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;
}

Error message here: 错误信息:

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.

You need to explicitly pass template parameters to std::forward : 您需要将模板参数显式传递给std::forward

std::forward<Args>(args)...

This is because std::forward needs some way of knowing the "original value category" of args... , which is impossible through template argument deduction alone as args is always an lvalue. 这是因为std::forward需要某种方式来了解args...的“原始值类别” args...这是不可能通过模板参数推导单独进行的,因为args总是一个左值。

Lvalues will deduced as lvalue references in the context of template argument deduction for forwarding references (as a special rule), so std::forward can do its job by looking at the types inside Args... . Lvalues将在转发引用的模板参数推导的上下文中推断为左值引用 (作为特殊规则),因此std::forward可以通过查看Args...内部的类型来完成它的工作Args...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在可变参数函数中的所有参数上调用std :: forward? - How would one call std::forward on all arguments in a variadic function? 可变参数模板麻烦匹配const和非const std :: string - Variadic template trouble matching const and non-const std::string std::function &amp; std::forward 带有可变参数模板 - std::function & std::forward with variadic templates 使用来自 std::vector 的参数调用可变参数模板化函数 - Call variadic templated function with arguments from a std::vector 可变参数模板函数:没有匹配的函数,用于调用std :: endl - Variadic Template Functions: No matching function for call, std::endl 如何使用 std:string 参数迭代可变参数函数? - How to iterate over variadic function with std:string arguments? 没有匹配函数std :: forward with lambdas - No matching function std::forward with lambdas 错误:没有匹配的 function 调用 'recherche(std::vector &gt;&amp;, std::vector &gt;::iterator, std::vector &gt;::iterator, const char [10])' - error: no matching function for call to ‘recherche(std::vector >&, std::vector >::iterator, std::vector >::iterator, const char [10])’ 没有匹配的函数可以调用&#39;const std :: __ 1 :: packaged_task类型的对象 <void ()> &#39;,但`std :: is_const`返回false - No matching function for call to object of type 'const std::__1::packaged_task<void ()>', but `std::is_const` returns false 使用 getline 给出错误:没有匹配的 function 调用 'getline(std::istream&amp;, const string&amp;)' - using getline gives error: no matching function for call to ‘getline(std::istream&, const string&)’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM