2016-10-04 82 views
1

我找不到訪問真實對象的方法,hana::for_each迭代元組。遍歷boost :: hana :: tuple

struct A { 
    std::string name; 
} 

struct B { 
    std::string name; 
} 

using type_t = decltype(boost::hana::tuple_t<A, B>); 
type_t names; 

boost::hana::for_each(names, [&](const auto& a) { 
     std::cout << a.name << std::endl; 
    }); 

a類型似乎是hana::tuple_impl<...>,似乎是不澆注料其基礎類型decltype(std::decay_t<a>)::type

我基本上想迭代一個模板對象(容器)的列表,它們具有相同的接口但包含不同的值。更好的方式來實現這一點是值得歡迎

+0

我懷疑這'decltype(升壓::花:: tuple_t )'。這是什麼意思?本身不是'tuple_t '*類型*嗎? – Nawaz

+0

@Nawaz文檔說它的用法就像這樣'auto types = hana :: tuple_t ;'所以我猜這是一個C++ 14變量模板。 http://www.boost.org/doc/libs/1_61_0/libs/hana/doc/html/index.html – Etherealone

回答

8

tuple_t適用於hana::type s的元組。你想正常的對象,這僅僅是tupletuple

boost::hana::tuple<A, B> names; 
boost::hana::for_each(names, [&](const auto& x) { 
    std::cout << x.name << std::endl; 
}); 
+1

它確實有效。你能否詳細說明何時使用hana類型以及它們究竟是什麼?文檔對此有點誤導。它說話像我_have to_ wrap對象(它不是整數,'tuple_c'是用於整數的)''hana_types'和'tupe_t'是執行它的簡單方法。 – Etherealone

+1

@獨立'tuple_t '大約是'tuple ...>的包裝。當你想要'type_c'時使用它。 – Barry

+1

謝謝。我不知道我是否得到它,但它看起來像我會使用'tuple_t'在MPL-ish方式下操作類型(hana對'hana_type'類型的對象進行操作,並允許我們通過這些類型處理實際的C++類型對象(值))。正常的元組用於正常的對象使用。 – Etherealone