2017-05-26 81 views
0

的扣除之前的代码片段说不是几个段落更多:使用boost ::花:: eval_if_t汽车

#include <boost/hana/fwd/eval_if.hpp> 
#include <boost/hana/core/is_a.hpp>                                  
#include <iostream> 
#include <functional> 

using namespace boost::hana; 

template<class arg_t> 
decltype(auto) f2(arg_t const& a) 
{ 
    constexpr bool b = is_a<std::reference_wrapper<std::string>, 
          arg_t>; 

    auto wrapper_case = [&a](auto _) -> std::string& 
         { return _(a).get(); }; 

    auto default_case = [&a]() -> arg_t const& 
         { return a; }; 

    return eval_if(b, wrapper_case, default_case); 
} 

int main() 
{ 
    int a = 3; 
    std::string str = "hi!"; 
    auto str_ref = std::ref(str); 

    std::cout << f2(a) << ", " << f2(str) << ", " << f2(str_ref) 
       << std::endl; 
} 

编译器错误是:

$> g++ -std=c++14 main.cpp 
main.cpp: In instantiation of ‘decltype(auto) f2(const arg_t&) [with arg_t = int]’: 
main.cpp:42:22: required from here 
main.cpp:31:19: error: use of ‘constexpr decltype(auto) boost::hana::eval_if_t::operator()(Cond&&, Then&&, Else&&) const [with Cond = const bool&; Then = f2(const arg_t&) [with arg_t = int]::<lambda(auto:1)>&; Else = f2(const arg_t&) [with arg_t = int]::<lambda(auto:2)>&]’ before deduction of ‘auto’ 
    return eval_if(b, wrapper_case, default_case); 
      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

有没有递归,我使用gcc-6.0.2,它大概已经解决了关于decltype(auto)的一些错误,并且具有完整的C++ 14实现,并且符合boost::hana的要求,所以,我的错误必须在我的实现中,但我不知道什么是关于的错误。

注意:铿锵声++ 3.8.0会抛出类似的编译器错误。

回答

4

首先,如果路径没有说清楚,boost/hana/fwd/eval_if.hpp只是一个前向声明。如果你想认真使用它,你需要包括整个东西,即boost/hana/eval_if.hpp。这是原始错误的原因。

然后,该bool是错误的:

constexpr bool b = is_a<std::reference_wrapper<std::string>, 
         arg_t>; 

它强制转换到bool意味着该类型不再携带值的信息。使用auto

+0

我虽然有些模板完全在fwd文件中出于某种原因。我刚刚拿到了'hana'文档中的头文件。 'b'的问题我还没有完全理解。 'b'这是一个在编译时已知的常量值,所以'eval_if'应该按照预期工作。 –

+2

@ Peregring-lk作为函数参数传递时,值的不变性会丢失。它必须嵌入到它的工作类型中。 –

+0

>我刚拿到了头文件中的头文件。 @ Peregring-lk你能指点我在哪里你从那个例子的文件?我想解决它,如果它不明确必须包括'boost/hana/eval_if.hpp'。 –