variadic-functions

@SafeVarargs on interface method

泄露秘密 提交于 2019-12-08 15:09:39
问题 In this code, package com.example; interface CollectorIF<T> { // @SafeVarargs // Error: @SafeVarargs annotation cannot be applied to non-final instance method addAll void addAll(T... values); } class Collector<T> implements CollectorIF<T> { @SafeVarargs public final void addAll(T... values) { } } class Component<T> { public void compute(T value) { Collector<T> col1 = new Collector<>(); col1.addAll(value); // No warning CollectorIF<T> col2 = new Collector<>(); col2.addAll(value); // Type

Converting a variadic macro to a variadic template function?

喜夏-厌秋 提交于 2019-12-08 06:54:53
问题 Given a variadic macro of the form: #define MY_CALL_RETURN_F(FType, FId, ...) \ if(/*prelude omitted*/) { \ FType f = (FType)GetFuncFomId(FId); \ if(f) { \ return f(__VA_ARGS__); \ } else { \ throw invalid_function_id(FId); \ } \ } \ /**/ -- how can this be rewritten to a variadic function template? template<typename FType, typename ...Args> /*return type?*/ tmpl_call_return_f(MyFunId const& FId, /*what goes here?*/) { ... FType f = (FType)GetFuncFomId(FId); return f(/*what goes here?*/); ...

Java: varargs in interface

左心房为你撑大大i 提交于 2019-12-08 06:06:54
问题 public interface MyInterface { public ArrayList<Double> f(ArrayList<Double>... args); } I get the warning: Type safety: Potential heap pollution via varargs parameter paramOfChildren . I can suppress this warning with @SuppressWarnings("unchecked") . However, in every class that implements this interface, I get this warning again. I wonder if there's a way to suppress this warning once for all the classes implementing the interface. If not, is there a good reason that we shouldn't do it? 回答1:

Variadic construction for initialising vector of unique_ptr to base type

点点圈 提交于 2019-12-08 05:50:43
问题 Below is an example program where a 'Container' class needs to store a list of 'Items' via a base class pointer. Having started with C++11/14 the natural choice would be to use std::unique_ptr and variadic templates in the case I have. However being new I cannot fathom how to convert the variadic list into and initialiser list for the vector of unique_ptr in a manner that compiles. Help is greatly appreciated as I have failed to find anything online that helps me with this issue so far

Clojure: Transforming Varargs But Keeping them Varargs

痴心易碎 提交于 2019-12-08 02:25:45
问题 I am working on a little pet project in Clojure. I have a function that I pass varargs: (defn foor [bar & args] (let new-args (custom-transform args)] (do-something new-args)))))) But, do-something is expecting varargs, not a list object. In custom-transform (defn custom-transform [& args] (if vars vars nil)) How do I preserve the "vararg"-iness of my args after applying a transformation? 回答1: I think apply should do the trick: (apply do-something new-args) Your problem isn't specific to

How to use varargs as the parameter of Constructor.getConstructor( ) in java

微笑、不失礼 提交于 2019-12-08 01:33:38
问题 I have a java class like below which I want to create an instance of this class dynamically by using class name. class Demo { public Demo(String... s) { //some initialization here. } } And I want to create an object using following code Class<?> klass = Class.forName("Demo"); Constructor<?> con = klass.getConstructor("**what should be here**"); Object obj = con.newInstance(param1, param2, ...); 回答1: String... is just String[] so you can use Constructor<?> con = klass.getConstructor(String[]

C++ how to use ellipsis without a preceding argument

£可爱£侵袭症+ 提交于 2019-12-08 01:18:16
问题 Hi I have a class with a member function that takes a variable number of arguments. The class knows how many arguments to expect once it is instantiated . e.g class myClass { myClass(int num_args){na=num_args;}; private: int na; public: void do_something(int num_args, ...); } void myClass::do_something(int num_args, ...) { va_list vl; va_start(vl, num_args); for(int i=0; i < num_args; i++) do_anotherthing(va_arg(vl, type)); va_end } so i end up calling as follows: myClass A(5); myClass B(4);

Pushing and popping the first element of a std::tuple

三世轮回 提交于 2019-12-07 16:02:02
问题 I am writing a function in C++ with a variable number of arguments (and different types) in this way template<typename ...Ts> void myFunction(Ts ...args) { //create std::tuple to access and manipulate single elements of the pack auto myTuple = std::make_tuple(args...); //do stuff return; } What i would like to do, but I don't know how, is to push and pop elements from the tuple, in particular the first element... something like //remove the first element of the tuple thereby decreasing its

Platform inconsistencies with vsprintf and va_list

走远了吗. 提交于 2019-12-07 11:21:14
问题 Background: I am currently trying to "extend" standard C formatting with support for handling a certain struct, similar to how Objective-C extends C formatting to allow support for NSString with the "%@" sequence. The one problem I'm struggling with is that vsprintf seems to be behaving differently on OS X versus Linux (I've tested with Ubuntu 10.10 and 12.04). On OS X, it is behaving how I thought it should, where after calling vsprintf, calling va_arg returns the ms pointer (as if the

Variadic function without named argument

[亡魂溺海] 提交于 2019-12-07 10:38:44
问题 I noticed, that both GCC and MSVC are happy with the following code: #include <iostream> void foo(...); int main() { foo(); } void foo(...) { std::cout << "foo\n"; } More specifically, code was run under GCC 6.2.0 and Visual Studio 2015. I know that C requires at least one named parameter preceding the ellipsis, which allows to handle any number of arguments using specialized va_start , va_args , and va_end macros from <stdarg.h> (here <cstdarg> ) header. Otherwise, it won't even compile.