为什么 boost::hana 的集合不是默认可构造的?
Why is boost::hana's set not default constructable?
今天我发现boost::hana
的map
和set
不是默认可构造的,而tuple
是。这有什么特别的原因吗,因为它很烦人。
这个
#include <boost/hana/set.hpp>
// ^^^ or map
constexpr boost::hana::set<> a{};
// ^^^ or map
int main(){}
失败,并出现以下错误:
main.cpp:3:30: error: no matching constructor for initialization of 'const boost::hana::set<>'
constexpr boost::hana::set<> a{};
^~~
/home/russellg/Documents/boost/hana-0.6.0/include/boost/hana/set.hpp:65:28: note: candidate constructor not viable: requires single argument 'xs', but no arguments were provided
explicit constexpr set(tuple<Xs...> const& xs)
^
/home/russellg/Documents/boost/hana-0.6.0/include/boost/hana/set.hpp:69:28: note: candidate constructor not viable: requires single argument 'xs', but no arguments were provided
explicit constexpr set(tuple<Xs...>&& xs)
^
/home/russellg/Documents/boost/hana-0.6.0/include/boost/hana/set.hpp:57:12: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were
provided
struct set
^
/home/russellg/Documents/boost/hana-0.6.0/include/boost/hana/set.hpp:57:12: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were
provided
1 error generated.
即使有一个空的地图或集合是完全有效的:
#include <boost/hana/set.hpp>
// ^^^ or map
constexpr auto a = boost::hana::make_set();
// ^^^ or map
int main(){}
它完美地编译。
感谢您的帮助。
编辑:
实际上,它是否为空并不重要,默认构造map
s和set
s总是非法的。
hana::set
和hana::map
表示是实现定义的。文档警告不要直接使用它们,并提到创建它们的规范方法分别是通过hana::make_set
和hana::make_map
。文件说明:
hana::set
的实际表示是由实现定义的。特别是,不应想当然地认为模板参数的顺序和任何构造函数或赋值运算符的存在。创建hana::set
的规范方式是通过hana::make_set
。
hana::tuple
是一个更简单的容器,记录它的表示,并努力保持与std::tuple
的一些奇偶性。hana::basic_tuple
文档说明:
〔…〕
hana::tuple
旨在提供一个与std::tuple
〔…〕有点接近的接口
至于为什么hana::set
和hana::map
的表示是实现定义的,请考虑阅读常见问题解答,但简而言之:
- 允许更灵活地实现编译时和运行时优化
- 知道类型通常不是很有用
有一个github问题需要考虑为hana::map
添加一个默认构造函数。
相关文章:
- yum :在 CentOS 上使用 Boost 1.69 而不是默认 (1.53) 版本
- 传递 boost::p ython::numpy::ndarray 作为 boost::p ython 函数的(默认或非
- boost::p rogram_options 自定义验证和默认值
- Boost.Spirit.Qi 语法,用于 Boost.Fusion 自适应结构中的默认值
- Boost::msm 如何使用非默认构造函数初始化state_machine_def和 msm::front::sta
- 在调用 boost parseOptions 时保留预设(非默认)值
- Boost Karma:当boost ::可选时,生成默认文本
- 使用Boost Spirit将默认值分配给变量
- Boost HANA:从集合创建地图和默认值
- boost::p rogram_options:是否可以将一个选项设置为另一个选项的默认值
- Boost-Spirit(X3)解析,没有默认构造函数
- 向量值boost :: program_options的默认值
- Boost::序列化 - 如何序列化需要数据分配但没有默认构造函数的数据结构
- 默认模板函数参数中的 boost::shared_ptr 和 nullptr
- 在 boost::lockfree:queue 默认构造函数的情况下断言失败
- C++:为什么 boost::p tr_vector 调整大小需要对象具有默认构造函数
- 如何实例化没有默认构造函数的类型的 boost::fusion::vector 成员变量
- 如何在<string> boost::p rogram_options 中指定矢量的默认值
- boost::statechart:如何定义某个状态的两个子状态,而不指定哪个是默认状态
- 使用默认参数将特定参数传递给 Boost Python 函数