有关为模板提供默认类型的问题

Question about providing default type for a template

本文关键字:默认 类型 问题      更新时间:2023-10-16

例如,我写了以下一段代码:

struct default_type {};
template <typename U = default_type> auto func(int x, U u) {
if constexpr(is_same<U, default_type>::value) {
return x * 2;
}
else {
return u(x);
}
}
int main() {
cout << "5 + 3 = " << func(5, [](int x){ return x + 3; }) << endl;
cout << "5 * 2 = " << func(5) << endl;
}

但这给出了编译器错误"没有匹配函数来调用'func(int)'"。正确的方法是什么?

您提供了默认类型,但没有默认值。该函数仍然需要两个参数,并且default_type将被推导的参数覆盖,使其无用。

这样做:

template <typename U = default_type>
auto func(int x, U u = {}) { ... } // (1)

我不能用确切的标准来支持这一点,但简而言之:U是在模板参数推导阶段获得的(没有推导规则 - 不考虑默认函数参数,因此默认),这在重载解决阶段之前,此时默认函数参数被解释,已经知道U的类型,这使得列表初始化有效。


是的,你也可以写:

template <typename U = default_type>
auto func(int x, U u = U()) { ... } // (2)

template <typename U = default_type>
auto func(int x, U u = default_type()) { ... } // (3)

显式提供U类型时语义略有不同(我认为您不会):

func<not_the_default_type>(5);

在这里,(1) 和 (2) 是等价的,但在 (3) 中,U的构造是通过构造一个临时default_type来实现的,然后将其转换为not_the_default_type


最后,完全相反的错误是期望在这里推导出U

template <typename U>
auto func(int x, U u = default_type()) { ... }

事实并非如此。请参阅此问答。