12

指定的初始化程序 (C++20) 应该如何与 CTAD 一起使用?

此代码在 gcc9.2 中运行良好,但在 clang8 中失败

template <typename int_t=int, typename float_t=float>
struct my_pair {
    int_t   first;
    float_t second;
};

template<typename ... ts>
my_pair(ts...) -> my_pair<ts...>;

int main() {
    my_pair x{.first = 20, .second = 20.f};
    static_assert( std::is_same_v<decltype(x.first), int> );
    static_assert( std::is_same_v<decltype(x.second), float> );
}

这应该是有效的吗?

请参阅https://godbolt.org/z/KtNI43上的示例

4

1 回答 1

3

是的,这应该是有效的。

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-listspecified-initializer-list的每个元素,我们选择聚合的相应元素并将其类型用作新候选者。为了

my_pair x{.first = 20, .second = 20.f};

is的类型和firstisT的类型,因此:secondU

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

这当然有效。

于 2019-09-11T12:30:38.257 回答