本文介绍了java的varargs是否有C ++ 14的等价物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力为我们从Java中了解的varargs找到一个C ++(14)等价物。



我尝试了什么:



我有点(但不完全)了解可变参数模板和函数,所以我认为我想做的是这样的:

 模板< typename ... A> 
void foo(A ... a){ / * ... * / }



现在,虽然这适用于可变数量的参数,但我也希望能够指定A中所有类型应该是哪种类型。例如在Java中可以做到:

  void  oof( int  ... i){ / *   ... * / } 



有没有一种干净的方法可以在C ++ 14中实现这一点?

解决方案

I'm struggling to find a C++ (14) equivalent for the varargs we know from Java.

What I have tried:

I kinda (but not fully) understand variadic templates and functions, so what I think I'd like to do is something like this:

template <typename... A>
void foo(A... a) { /*...*/ }


Now, while this works for a variable amount of parameters, I'd also like to be able to specify of which type all the types in A should be. For example in Java one could do:

void oof(int... i) { /*...*/ }


Is there a clean way to achieve this in C++ 14?

解决方案


这篇关于java的varargs是否有C ++ 14的等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 00:24
查看更多