C++ 如何将模板参数包转换为函数的多个指针参数?
我希望实现以下目标:C++ 如何将模板参数包转换为函数的多个指针参数?,c++,arrays,variadic-templates,functor,template-meta-programming,C++,Arrays,Variadic Templates,Functor,Template Meta Programming,我希望实现以下目标: template <typename Functor, typename... Params> void foo( size_t n /* magic goes here, producing function parameters p1, p2, etc. */ /* corresponding to the parameter pack. */ ) { for(size_t i =
template <typename Functor, typename... Params>
void foo(
size_t n
/* magic goes here, producing function parameters p1, p2, etc. */
/* corresponding to the parameter pack. */
)
{
for(size_t i = 0; i < n; i ++) {
std::cout << Functor()(
/* pass p1[i], p2[i], etc. corresponding to the */
/* parameter pack. */
) << '\n';
}
}
模板
福娃(
尺寸
/*这里有魔术,产生函数参数p1、p2等*/
/*对应于参数包*/
)
{
对于(大小i=0;i模板
无效foo(标准::尺寸\u t n,
Ts常数*\uuuu限制\uuuuu…Ts
) {
对于(大小i=0;i STD:Cuth<代码>限制> <代码>不是C++。请列出您的精确编译器,因为没有标准的C++程序可以包含<代码>限制符>代码>,因此您的解决方案必须是编译器特定的。@ YAKK:足够公平;请参阅编辑注释。
void foo(
size_t n,
const int* __restrict__ p1,
const int* __restrict__ p2
)
{
for(size_t i = 0; i < n; i ++) {
std::cout << std::plus()(p1[i], p2[i]) << '\n';
}
}
template<class Functor, class...Ts>
void foo( std::size_t n,
Ts const* __restrict__ ... ts
) {
for(size_t i = 0; i < n; i ++) {
std::cout << Functor{}( ts[i]...) << '\n';
}
}