Beginning generically, plus decltype considering local using-declaration

后端 未结 2 1514
猫巷女王i
猫巷女王i 2021-02-08 08:23

C++0x\'s ranged-for loop has a special exception to handle arrays (FDIS §6.5.4), and there are two functions, std::begin and end, which are overloaded to handle arrays or to sel

2条回答
  •  别跟我提以往
    2021-02-08 09:06

    I've encountered the same situation while using tuples:

    template
    auto f(Tuple&& tuple)
    -> /* ??? */
    {
        using std::get;
        return get(tuple);
    }
    

    which accepts both std::tuple and boost::tuple, and accepts both lvalues and rvalues as opposed to template auto f(std::tuple& tuple) -> /* ??? */.

    This particular case was solved with a traits class, which is in fact provided by the Standard: std::tuple_element. As usual with traits classes, the idea is that tuple is a protocol and anything that want to conform to it will provide a specialization for e.g. tuple_element. So in my case the solution already existed.

    In your case, if you were writing a library, I'd recommend writing (and documenting) such a traits class. In application code or other situations, I'm not so sure.

提交回复
热议问题