简体   繁体   English

Boost Hana type_c 模板参数扣除/替换失败

[英]Template argument deduction/substitution failed with Boost Hana type_c

I do not understand why the following simple example fails:我不明白为什么以下简单示例会失败:

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

I get the following error message:我收到以下错误消息:

[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]       |     ~~~^~~~~~~~~~~~~~~~~~~

The only way to make the above work is by making explicit the template argument of Foo by writing Foo<int>(boost::hana::type_c<int>) .完成上述工作的唯一方法是通过编写Foo<int>(boost::hana::type_c<int>)明确Foo的模板参数。 Why is the compiler unable to automatically deduce the template argument?为什么编译器无法自动推导出模板参数?

Notice that the above code works if I use boost::hana::basic_type in place of boost::hana::type in the declaration of Foo .请注意,如果我在Foo的声明中使用boost::hana::basic_type代替boost::hana::type ,则上述代码有效。 Is this alternative approach correct?这种替代方法是否正确?

type_c<int> is a variable template that creates a value of type type<int> . type_c<int>是一个变量模板,它创建一个type<int>的值。 It indeed seems like Foo should easily deduce parameter _T from type<_T> when passing type<int> .在传递type<int>时,似乎Foo应该很容易地从type<_T>推断出参数_T However it is not possible because type is an alias template that refers to member of some auxiliary class and its parameter is always in non-deduced context.但是这是不可能的,因为type是一个别名模板,它引用了一些辅助 class 的成员,并且它的参数总是在非推导上下文中。

template< typename x_Type >
struct FooImpl
{
    using Type = x_Type;
};

template< typename x_Type >
using Foo = typename FooImpl<x_Type>::Type;

template< typename x_Type > // x_Type can not be deduced
void Bar(Foo<x_Type>) {}

int main()
{
    Bar(Foo<int>{});
    return 0;
}

online compiler 在线编译器

basic_type works because it is an actual type of type_c . basic_type有效,因为它是type_c的实际类型。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM