gpt4 book ai didi

c++ - 使用聚合初始化器初始化类的模板(聚合类型)成员但没有额外的括号

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:33:47 26 4
gpt4 key购买 nike

有这段代码:

struct Vec3 {
int x;
int y;
int z;
};

template <typename T>
class myProperty {
public:
myProperty(const T& initValue) : m_value{initValue} {}
private:
T m_value;
};

创建 myProperty 类型对象时:

myProperty<int> ip{1};
myProperty<Vec3> vp1{{1, 2, 3}};
// myProperty<Vec3> vp2{1, 2, 3}; ERROR: myProperty doesn't have a matching constructor.

是否有一种优雅的方式使 vp2 初始化工作?为 Vec3 专门化 myProperty 是一种矫枉过正。

最佳答案

一个简单的解决方案是使用可变模板构造函数:

template <typename ...P> myProperty(P &&... p) : m_value{std::forward<P>(p)...} {}

它使 myProperty<Vec3> vp2{1, 2, 3};编译。

它也停止了 myProperty<Vec3> vp1{{1, 2, 3}};从编译(这似乎符合您的意图)。

此选项的问题在于它会阻止复制构造正常工作。
(如果参数是非常量 myProperty<T> 左值,那么这个可变参数构造函数比 myProperty(const myProperty &) 更匹配。)

这可以用 SFINAE 解决:

C++17 <experimental/type_traits> :

#include <experimental/type_traits>
#include <utility>

template <typename T, typename ...P> using list_constructible = decltype(T{std::declval<P>()...});

// ...

template
<
typename ...P,
typename = std::enable_if_t<std::experimental::is_detected_v<list_constructible, T, P...>>
>
myProperty(P &&... p) : m_value{std::forward<P>(p)...} {}

C++14:

#include <type_traits>
#include <utility>

template <typename...> using void_t = void;
template <typename DummyVoid, template <typename...> class A, typename ...B> struct is_detected : std::false_type {};
template <template <typename...> class A, typename ...B> struct is_detected<void_t<A<B...>>, A, B...> : std::true_type {};
template <typename T, typename ...P> using list_constructible = decltype(T{std::declval<P>()...});

// ...

template
<
typename ...P,
typename = std::enable_if_t<is_detected<void, list_constructible, T, P...>::value>
>
myProperty(P &&... p) : m_value{std::forward<P>(p)...} {}

关于c++ - 使用聚合初始化器初始化类的模板(聚合类型)成员但没有额外的括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53238233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com