2

today I found that boost::hana's map and set aren't default constructable, while the tuple is. Is there any particular reason for this because it is quite annoying.

This

#include <boost/hana/set.hpp> 
//                   ^^^ or map

constexpr boost::hana::set<> a{};
//                     ^^^ or map

int main(){}

fails with the following error:

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.

even though it is perfectly valid to have an empty map or set:

#include <boost/hana/set.hpp>
//                   ^^^ or map

constexpr auto a = boost::hana::make_set();
//                                   ^^^ or map

int main(){}

Which compiles flawlessly.

Any help is appreciated.

EDIT:

It doesn't actually matter if it is empty, it is always illegal to default construct maps and sets.

4

1 回答 1

3

hana::sethana::map表示是实现定义的。该文档警告不要直接使用它们,并提到创建它们的规范方法是分别通过hana::make_sethana::make_map。该文档指出:

a 的实际表示hana::set是实现定义的。特别是,不应理所当然地认为模板参数的顺序以及任何构造函数或赋值运算符的存在。创建 a 的规范方法hana::set是通过hana::make_set.

hana::tuple是一个更简单的容器,记录其表示,并努力与std::tuple. hana::basic_tuple 文档说明:

[...]hana::tuple旨在提供一个有点接近std::tuple[...]


至于为什么hana::sethana::map的表示是实现定义的,请考虑阅读常见问题解答,但简而言之:

  • 允许更灵活地实现编译时和运行时优化
  • 知道类型通常不是很有用

有一个 github问题需要考虑为hana::map.

于 2015-12-01T04:09:05.760 回答