variadic-functions

Varargs with null parameters in Java

笑着哭i 提交于 2019-12-04 03:24:11
The following code simply uses a null reference as a varargs parameter. package currenttime; import java.util.Arrays; public class Main { private static void temp(String...str) { System.out.println(Arrays.asList(str)); } public static void main(String[] args) { temp(null,null); temp(null); } } The first call to the method temp(null, null); displays [null, null] means that str[0]=null and str[1]=null . but the later call to temp(null); causes the NullPointerException to be thrown which appears that the str itself is null . If it's type cast to String something like this temp((String)null); , it

Abstract functions and variable arguments list

我怕爱的太早我们不能终老 提交于 2019-12-04 03:17:53
问题 I have an abstract class an I like to know if it's possible to define an abstract function with variable arguments list? Give me an example if it's possible. 回答1: Yes, it is possible in principle. An example follows below. You can see the output here. Also read about variable arguments list here and here #include <iostream> #include <cstdarg> using namespace std; class AbstractClass{ public: virtual double average(int num, ... ) = 0; }; class ConcreteClass : public AbstractClass{ public:

Passing std::vector<int> items to variadic function

拈花ヽ惹草 提交于 2019-12-04 03:13:45
问题 I'm using gcc 4.6. Assume that there is a vector v of parameters I have to pass to a variadic function f(const char* format, ...). One approach of doing this is: void VectorToVarArgs(vector<int> &v) { switch(v.size()) { case 1: f("%i", v[0]); case 2: f("%i %i", v[0], v[1]); case 3: f("%i %i %i", v[0], v[1], v[2]); case 4: f("%i %i %i %i", v[0], v[1], v[2], v[3]); // etc... default: break; } } // where function f is void f(const char* format, ...) { va_list args; va_start (args, format);

C++ Variadic Function Templates of Known Type

北城余情 提交于 2019-12-04 02:51:36
I'm currently trying to get my head around some of the things I can do with variadic template support. Let's say I have a function like this - template <typename ... Args> void foo(Args ... a) { int len = sizeof...(tail); int vals[] = {a...}; /* Rest of function */ } /* Elsewhere */ foo(1, 2, 3, 4); This code works because I assume beforehand that the arguments will be integers, but obviously will fail if I provide something else. If I know that the parameter packs will contain a particular type in advance, is there some way that I can do without the templating and have something like - void

Alternatives to function overloading in C

二次信任 提交于 2019-12-04 02:30:10
问题 I'm looking for an elegant way to avoid re-writing a function, whose implementation is almost the same, but only the signature (the number of input parameters and their data types) is different. I know function overloading is not possible in C. I also know about the existence of variadic functions. But I think they won't be helpful in this situation. Consider the following problem, where we need to calculate the area of a triangle. We have two functions implementing two different formulae: S

How can I pass an array as parameters to a vararg function?

孤街浪徒 提交于 2019-12-04 00:58:14
问题 I have some code that looks like this: uint8_t activities[8]; uint8_t numActivities = 0; ... activities[numActivities++] = someValue; ... activities[numActivities++] = someOtherValue; ... switch (numActivities) { 0 : break; 1 : LogEvent(1, activities[0]); break; 2 : LogEvent(1, activities[0], activities[1]); break; 3 : LogEvent(1, activities[0], activities[1], activities[2]); break; // and so on } where LogEvent() is a varargs function. Is there a more elgant way to do this? [Update] Aplogies

Two variadic templates for a single function?

倾然丶 夕夏残阳落幕 提交于 2019-12-04 00:43:48
问题 In C++11 is it possible to have two variadic templates for a single function ? If not, is there a trick to write something like that : template <class... Types, class... Args> void f(const std::tuple<Types...>& t, Args&&... args) 回答1: That's perfectly legal: #include <tuple> using namespace std; template <class... Types, class... Args> void f(const std::tuple<Types...>& t, Args&&... args) { // Whatever... } int main() { std::tuple<int, double, bool> t(42, 3.14, false); f(t, "hello", true, 42,

varargs(va_list va_start) doesn't work with pass-by-reference parameter [duplicate]

北城以北 提交于 2019-12-03 23:57:19
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Are there gotchas using varargs with reference parameters Hi, I have a problem with varargs. Look at my code(Microsoft Visual Studio 2005 or 2008). #include <stdarg.h> struct Test { int a; }; void T1(int n, ...) { va_list args; va_start(args, n); char* p = va_arg(args, char*); va_end(args); } void T2(Test n, ...) { va_list args; va_start(args, n); char* p = va_arg(args, char*); va_end(args); } void T3(const Test

Abstract method with variable list of arguments

Deadly 提交于 2019-12-03 22:42:55
I haven't quite found an elegant way to solve this issue. I have an abstract class that several other classes are inheriting with an abstract method that can contain anywhere from zero to 4-5 arguments of varying types. public abstract class Item { public abstract void use(); } For instance, I have a Book class that inherits this and takes no arguments when overriding use(), I have a Key class that inherits and takes a String and a Queue as arguments when overriding, etc... I've tried using generics but I have to input the number used, such as Item, when it actually depends on the class.

Cython equivalent of c define #define myfunc(node x,…) SetNode(x.getattributeNode(),__VA_ARGS__)

喜你入骨 提交于 2019-12-03 21:27:40
问题 Cython equivalent of c define #define myfunc(Node x,...) SetNode(x.getattributeNode(),__VA_ARGS__) I have a c api SetNode which takes first argument a node of struct type node and N variables (N is variable number from 0-N) here is a c example to solve such problum exampleAPI.c #include<stdarg.h> float sumN(int len,...){ va_list argp; int i; float s=0; va_start(argp,len); for(i=0;i<len;i++){ s+=va_arg(argp,int); } va_end(argp); } exampleAPI.h #include<stdarg.h> float sumN(int len,...)