How to write a variadic method which replaces chained method calls?

前端 未结 1 1716
借酒劲吻你
借酒劲吻你 2020-12-19 06:09

I am working on a recursive map class called group_by which models the SQL namesake.

For example, gb is a group_by object

1条回答
  •  伪装坚强ぢ
    2020-12-19 07:01

    Chained method calls are essentially recursive, so you need to implement at recursively:

    child_type& at( I const& key ) {
        return m_map.at( key );
    }
    
    template
    auto at(const I &i, const J &j, const Ks &...ks)
    -> decltype(m_map.at(i).at(j, ks...)) {
        return m_map.at(i).at(j, ks...);
    }
    

    Note that since at requires at least 1 argument, the variadic form takes at least 2 parameters. This is significantly easier to implement than dispatching on sizeof..., and should be easier to read.

    0 讨论(0)
提交回复
热议问题