【发布时间】:2019-10-21 22:33:14
【问题描述】:
我正在使用与此示例类似的代码使用 C++17 进行编译:
#include <iostream>
#include <iterator>
class Foo {};
template <typename... Args,
typename ostream_type = ::std::basic_ostream<Args...>,
typename ostreambuf_iterator_type = ::std::ostreambuf_iterator<Args...>>
ostream_type &operator<<(ostream_type &os, const ::Foo f) {
// Do ostreambuf_iterator_type stuff with 'f' here...
return os;
}
int main() {
::Foo f;
::std::cout << f;
return 0;
}
我发现当我将Args... 应用于ostream_type 和ostreambuf_iterator_type 的模板参数列表时,模板类型推导失败,但如果我分配char_type 和@987654326 就可以了@来自ostream_type。
typename ostreambuf_iterator_type = ::std::ostreambuf_iterator<typename ostream_type::char_type, typename ostream_type::traits_type>>
为什么会这样,::std::basic_ostream 和 ::std::ostreambuf_iterator 的模板参数相同?
【问题讨论】:
-
我已经意识到我可以在函数体中进行更多的推导,我不需要指定迭代器参数,但是这段代码仍然提出了为什么 Args... 没有的问题没有像我预期的那样工作。
标签: c++ variadic-templates variadic-functions template-argument-deduction type-deduction