如何让模板推导其参数

How do to have a template deduce its parameters

我正在尝试为 back_inserter 编写一个接收器,以减少增加代码的 std::copy() 命令的数量。

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

template <typename OutputIterator>
class sink
{
public:
    sink(OutputIterator out) : _out(out) { }
    OutputIterator _out;
};

template <typename OI, typename C>
sink<OI>& operator<<(sink<OI>& s, const C& c)
{
    std::copy(c.begin(), c.end(), s._out);
    return s;
}


int main(int, const char*[])
{
    std::vector<uint8_t> c;
//    auto s = sink<std::back_insert_iterator<std::vector<uint8_t>>>(std::back_inserter(c));
    auto s = sink(std::back_inserter(c));

    s << std::vector<uint8_t>{'F','e','e','d','i','n','g',' ','f','r','o','g','g','i','e','s'};
    s << std::string("Hungry hippos");

    std::copy(c.begin(), c.end(), std::ostream_iterator<int>(std::cout, ":"));
}

然而这给出了错误:

main.cpp: In function 'int main(int, const char**)':
main.cpp:27:18: error: missing template arguments before '(' token
     auto s = sink(std::back_inserter(c));
                  ^

下面的代码可以工作,但不太理想,因为它看起来比到处都有很多 std::copy 函数调用更糟糕。

auto s = sink<std::back_insert_iterator<std::vector<uint8_t>>>(std::back_inserter(c));

如何提示编译器自动推断类型?

最终我想改进系统,这样我就可以简单地写

sink(out) << reply::stock_reply(reply::bad_request);

其中 out 是一个 back_insert_iterator 并且 stock_reply 给出了一个固定的响应。

C++17 添加了这个确切的功能,以及在需要消除歧义的情况下的周围帮助。它被称为 class template argument deduction

如果这不是一个选项,您可以求助于 "make" 辅助函数等变通方法。这就是你的情况(这里 forward 对你来说不是绝对必要的):

template<typename OutputIterator>
sink<std::decay_t<OutputIterator>> make_sink(OutputIterator&& output_iterator)
{
    return sink<std::decay_t<OutputIterator>>(std::forward<OutputIterator>(output_iterator));
}

Live demo herestd::decay_t 是为了让函数在传递左值时做正确的事情。