2

我有一段有点做作的代码:

#include <functional>

template <typename... ARGs>
auto construct1(std::function<void(ARGs...)> p, const ARGs &...args) {}

template < typename... ARGs>
auto construct2(std::function<void(int)>     p, const ARGs &...args) {}

int main() {
    auto p = [](int) {};
    construct1<int>(p, 0);
    construct2<int>(p, 0);
    return 0;
}

ARGs... = { int }为什么编译器在第一种情况下努力解决这个问题?std::function<void(int)>如果我通过在签名中使用来帮助编译器(第二种情况),则代码可以编译。在这两种情况下,编译器都可以毫不费力地推断出const ARGs&...应该是const int&. 使用 C++17。

海合会:

main.cpp: In function ‘int main()’:
main.cpp:11:25: error: no matching function for call to ‘construct1<int>(main()::<lambda(int)>&, int)’
   11 |     construct1<int>(p, 0);
      |                         ^
main.cpp:4:6: note: candidate: ‘template<class ... ARGs> auto construct1(std::function<void(ARGs ...)>, const ARGs& ...)’
    4 | auto construct1(std::function<void(ARGs...)> p, const ARGs &...args) {}
      |      ^~~~~~~~~~
main.cpp:4:6: note:   template argument deduction/substitution failed:
main.cpp:11:25: note:   ‘main()::<lambda(int)>’ is not derived from ‘std::function<void(ARGs ...)>’
   11 |     construct1<int>(p, 0);
      |  

铛:

main.cpp:11:5: error: no matching function for call to 'construct1'
    construct1<int>(p, 0);
    ^~~~~~~~~~~~~~~
main.cpp:4:6: note: candidate template ignored: could not match 'function<void (int, type-parameter-0-0...)>' against '(lambda at main.cpp:10:14)'
auto construct1(std::function<void(ARGs...)> p, const ARGs &...args) {}
     ^
1 error generated.
4

1 回答 1

4

问题是,construct1正在服用,std::function但您正在通过 lambda。当你将参数设为 typestd::function<void(ARGs...)>时,会执行模板参数推导ARGs以对函数参数进行推导p(即使模板参数包是用模板参数显式指定的,如果有额外的参数,它也可以通过模板参数推导来扩展),这会失败,因为隐式转换获胜' 不被考虑在扣除。

类型推导不考虑隐式转换(除了上面列出的类型调整):这是重载解析的工作,稍后会发生。

您可以使用std::type_identity(C++20 起)p从演绎中排除(参见non-deduced contexts),例如

template <typename... ARGs>
auto construct1(std::function<void(std::type_identity_t<ARGs>...)> p, const ARGs &...args) {}
//                                 ^^^^^^^^^^^^^^^^^^^^^    ^

居住


PS:在 C++20 之前,您可以通过以下方式type_identity轻松获胜:

template< class T >
struct type_identity {
    using type = T;
};
template< class T >
using type_identity_t = typename type_identity<T>::type;
于 2021-05-07T03:17:14.677 回答