他们中的哪一个是正确的,为什么?
我认为它是第一个,因为Ts已经有了与该类型相关联的&&或const &,但我希望确定,noexcept的这种特殊情况的例子真的不是很多。
template <typename T>
struct Test {
T test;
// A
template <typename... Ts>
Test(Ts &&... ts) noexcept(std::is_nothrow_constructible_v<T, Ts...>)
: test(std::forward<Ts>(ts)...) {
}
// B
template <typename... Ts>
Test(Ts &&... ts) noexcept(std::is_nothrow_constructible_v<T, Ts &&...>)
: test(std::forward<Ts>(ts)...) {
}
};发布于 2019-04-11 01:22:22
我更喜欢选项C。您可以在noexcept说明符中使用noexcept operator,并让它为您计算调用的值。我发现这更容易读懂,因为你使用了你想要为异常说明符做的表达式。在您的情况下,这将看起来像
template <typename T>
struct Test {
T test;
// C
template <typename... Ts>
Test(Ts &&... ts) noexcept(noexcept(T(std::forward<Ts>(ts)...)))
: test(std::forward<Ts>(ts)...) {
}
};现在代码显示,如果T(std::forward<Ts>(ts)...)为noexcept,则Test(Ts &&... ts)为noexcept。
https://stackoverflow.com/questions/55617890
复制相似问题