Boost.Hana在visual studio 2017 rc中的残缺使用

时间:2023-12-20 19:10:08

最新的visual studio还不支持hana,不知道vs2017正式版本出后会不会支持。等不及了,先用rc版试试吧。

1、从https://github.com/boostorg/hana下载或拉取最新版本,我们只需要'include/boost/'目录中的所有文件。

Boost.Hana在visual studio 2017 rc中的残缺使用

2、新建一个控制台应用程序:

Boost.Hana在visual studio 2017 rc中的残缺使用

3、将hana文件复制到目录下(或者不复制,而是直接添加文件包含目录),进行一些简单配置,然后在cpp文件中随便写一点关联hana的代码:

Boost.Hana在visual studio 2017 rc中的残缺使用

Boost.Hana在visual studio 2017 rc中的残缺使用

Boost.Hana在visual studio 2017 rc中的残缺使用

4、编译。当然通不过!把错误概括一下,我用了一个比较山寨的办法,顺便也传到了https://github.com/freezestudio/hana.zh/blob/master/ReadMe.txt里。

vs2017 解析 delctype(...)::value 时会出错,只好将此语句分解成2步:
1、 using xxx = decltype(...);
2、 xxx::value; 修改处请参见: error C2039 hana/detail/index_if.hpp : value 不是 global namespace 的成员
error C2146 hana/detail/unpack_flatten.hpp : 语法错误 缺少 > (在标识符value的前面)
error C2146 hana/detail/hash_table.hpp : 语法错误 缺少 ; (在标识符type的前面)

  

Boost.Hana在visual studio 2017 rc中的残缺使用

Boost.Hana在visual studio 2017 rc中的残缺使用

Boost.Hana在visual studio 2017 rc中的残缺使用

5、如果还是不能通过编译,那就根据上面的山寨方法接着改吧,基本上就是这3个文件编译时有问题,也可以对照https://github.com/freezestudio/hana.zh的hana头文件修改。经过这个山寨修改,终于通过编译了。我们开始跑demo吧:在hana目录中'example/tutorial/quickstart.cpp',同样问题连连,还是先‘魔改':

#include "stdafx.h"

#include "hana.hpp"
#include <cassert>
#include <iostream>
#include <string> namespace hana = boost::hana;
struct Fish { std::string name; };
struct Cat { std::string name; };
struct Dog { std::string name; }; int main()
{ auto animals = hana::make_tuple(Fish{ "Nemo" }, Cat{ "Garfield" }, Dog{ "Snoopy" }); using namespace hana::literals; // Access tuple elements with operator[] instead of std::get.
Cat garfield = animals[1_c]; // Perform high level algorithms on tuples (this is like std::transform)
auto names = hana::transform(animals, [](auto a) { return a.name; }); auto reverse_names = hana::reverse(names);
auto reverse_names2 = hana::make_tuple("Snoopy", "Garfield", "Nemo"); //assert(hana::reverse(names) == hana::make_tuple("Snoopy", "Garfield", "Nemo")); auto animal_types = hana::make_tuple(hana::type_c<Fish*>, hana::type_c<Cat&>, hana::type_c<Dog>); auto no_pointers = hana::remove_if(animal_types, [](auto a) {
return hana::traits::is_pointer(a);
}); //static_assert(no_pointers == hana::make_tuple(hana::type_c<Cat&>, hana::type_c<Dog>), ""); auto has_name = hana::is_valid([](auto&& x) -> decltype((void)x.name) {}); static_assert(has_name(garfield));
static_assert(!has_name()); return ;
}

看到了吧,static_assert都注释掉了。因为vc编译器通不过!

好了,这就是残缺版的hana。