[英]How do I expand integer_sequence?
I have a function which looks like this: 我有一个看起来像这样的功能:
template <typename T, std::size_t... I>
std::ostream& vector_insert(std::ostream& lhs, const char* delim, const T& rhs, std::index_sequence<I...>) {
std::ostream_iterator<float> it(lhs, delim);
((*it++ = at(rhs, I)), ...);
return lhs;
}
This is my final attempt and I'm still failing on my expansion of the integer_sequence
I'm hoping someone can tell me how to write a line that will effectively expand to: 这是我的最后一次尝试,我仍然无法扩展
integer_sequence
我希望有人可以告诉我如何编写一条有效扩展的行:
*it++ = at(rhs, 0U), *it++ = at(rhs, 1U), *it++ = at(rhs, 2U)
Other things I've tried are: 我尝试过的其他事情是:
*it++ = at(rhs, I...)
*it++ = at(rhs, I)...
(*it++ = at(rhs, I))...
All of them are giving me the error: 所有这些都给了我错误:
error C3520:
I
: parameter pack must be expanded in this context错误C3520:
I
:必须在此上下文中扩展参数包
How do I expand this thing? 我该如何扩展这个东西?
EDIT: 编辑:
@AndyG has pointed out that this seems to be a visual-studio-2017 bug. @AndyG指出 ,这似乎是一个视觉工作室2017年的错误。
This seems like a compiler bug with Visual C++. 这似乎是Visual C ++的编译器错误。 I'm not aware of any easy fix for it other than simplifying the expression in which the parameter pack is expanded.
除了简化扩展参数包的表达式之外,我不知道任何简单的修复。 Converting to a recursive approach seems to reliably work around the problem.
转换为递归方法似乎可靠地解决问题。 For example :
例如 :
#include <array>
#include <iostream>
#include <iterator>
template <typename T>
const auto& at(const T& v, size_t i) { return v[i]; }
// End of recursion
template<class T>
void vector_insert_impl(std::ostream_iterator<int> &, const char*, const T&)
{}
// Recursion case
template<class T, std::size_t N, std::size_t... I>
void vector_insert_impl(std::ostream_iterator<int> & iter, const char* delim, const T&rhs)
{
*iter++ = at(rhs, N);
// Continue the recursion
vector_insert_impl<T, I...>(iter, delim, rhs);
}
template <typename T, std::size_t... I>
std::ostream& vector_insert(std::ostream& lhs, const char* delim, const T& rhs, std::index_sequence<I...>)
{
std::ostream_iterator<int> it(lhs, delim);
// Call the recursive implementation instead
vector_insert_impl<T, I...>(it, delim, rhs);
return lhs;
}
int main() {
std::array<int, 5> v = { 1, 2, 3, 4, 5 };
vector_insert(std::cout, " ", v, std::make_index_sequence<v.size()>());
}
Here, the parameter pack I
is only expanded in the context of providing template parameters which VC++ has no trouble with. 这里,参数包
I
仅在提供VC ++没有问题的模板参数的上下文中进行扩展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.