Apply the first valid function of a set of N functions

前端 未结 5 2043
南旧
南旧 2020-12-10 06:52

This previous answer shows how to apply function based on the validity of a call: here. However, it applies to two functions. I was wondering if the concept could be general

5条回答
  •  佛祖请我去吃肉
    2020-12-10 07:18

    Use boost::hana::overload_linearly:

    hana::overload_linearly(f1, f2, f3)(a, b, c, d, e)
    

    If none of the expressions are valid, it's a compile error, but it's easy to make it do nothing in that case:

    hana::overload_linearly(f1, f2, f3, [](auto&&...) {})(a, b, c, d, e)
    

    Alternatively, use boost::hof::first_of, which does the same thing

提交回复
热议问题