是的,这应该是有效的。
CTAD 的工作方式是我们在一组合成的构造函数上执行重载决议,以确定类模板参数是什么。从 C++17 开始,合成的构造函数集仅基于主模板的构造函数和推导指南(我正在更改模板参数名称,因为我发现它们非常混乱):
template <class T=int, class U=float>
struct my_pair {
T first;
U second;
};
// default constructor
template <class T=int, class U=float>
auto __f() -> my_pair<T, U>;
// copy candidate
template <class T=int, class U=float>
auto __f(my_pair<T, U>) -> my_pair<T, U>;
// deduction guide
template <class... T>
auto __f(T...) -> my_pair<T...>;
C++20 添加了一个新的聚合扣除候选。对于initializer-list或specified-initializer-list的每个元素,我们选择聚合的相应元素并将其类型用作新候选者。为了
my_pair x{.first = 20, .second = 20.f};
is的类型和first
isT
的类型,因此:second
U
// aggregate deduction candidate
template <class T=int, class U=float>
auto __f(T, U) -> my_pair<T, U>;
现在,我将这四个候选项写为函数(因为我发现将它们视为函数更容易),但措辞将它们定义为假设类类型的构造函数。因此,当我们使用 执行重载解决方案时{.first = 20, .second = 20.f}
,如果你眯着眼睛看它是可行的。
最后一个候选是最好的候选(只有聚合推导候选和推导指南可行,聚合推导更专业),所以我们最终得到my_pair<int, float>
。
完成 CTAD 后,我们现在重新开始并有效地做
my_pair<int, float> x{.first = 20, .second = 20.f};
这当然有效。