【发布时间】:2022-01-12 19:39:37
【问题描述】:
我不明白为什么下面这个简单的例子会失败:
#include <boost/hana.hpp>
template <typename _T>
static constexpr void Foo(boost::hana::type<_T>) {
}
int main() {
Foo(boost::hana::type_c<int>);
return 0;
}
我收到以下错误消息:
[build] error: no matching function for call to ‘Foo(boost::hana::type<int>&)’
[build] 74 | Foo(hana::type_c<int>);
[build] | ~~~^~~~~~~~~~~~~~~~~~~
[build] note: candidate: ‘template<class _T> constexpr void Morphy::Foo(boost::hana::type<T>)’
[build] 61 | static constexpr void Foo(hana::type<_T>) {
[build] | ^~~
[build] note: template argument deduction/substitution failed:
[build] note: couldn’t deduce template parameter ‘_T’
[build] 74 | Foo(hana::type_c<int>);
[build] | ~~~^~~~~~~~~~~~~~~~~~~
实现上述工作的唯一方法是通过编写 Foo<int>(boost::hana::type_c<int>) 来明确 Foo 的模板参数。 为什么编译器无法自动推导出模板参数?
请注意,如果我在 Foo 的声明中使用 boost::hana::basic_type 代替 boost::hana::type,则上述代码有效。 这种替代方法是否正确?
【问题讨论】:
标签: c++ templates boost template-meta-programming template-argument-deduction