variadic-functions

Odd behavior when recursively building a return type for variadic functions

This is probably going to be a really simple explanation, but I'm going to give as much backstory as possible in case I'm wrong. Advanced apologies for being so verbose. I'm using gcc4.5, and I realize the c++0x support is still somewhat experimental, but I'm going to act on the assumption that there's a non-bug related reason for the ...

How to find the length of a parameter pack ?

Hi All, Suppose I have a variadic template function like template<typename... Args> unsigned length(Args... args); How do I find the length of the parameter list using the length function ? ...

va_arg with pointers

I want to initialize a linked list with pointer arguments like so: /* * Initialize a linked list using variadic arguments * Returns the number of structures initialized */ int init_structures(struct structure *first, ...) { struct structure *s; unsigned int count = 0; va_list va; va_start(va, first); for (s = f...

Problem passing a reference as a named parameter to a variadic function

I'm having problems in Visual Studio 2003 with the following: void foo(const char*& str, ...) { va_list args; va_start(args, str); const char* foo; while((foo = va_arg(args, const char*)) != NULL) { printf("%s\n", foo); } } When I call it: const char* one = "one"; foo(one, "two", "three", NULL); I get: ...

Problem with variable argument function in C++

I'm trying to create a variable length function (obviously, heh) in C++, and what I have right now works, but only for the first argument. If someone could please let me know how to get this working with all the arguments that are passed, I would really appreciate it. Code: void udStaticObject::accept( udObjectVisitor *visitor, ... ) {...

Variadic C++ function doesn't work when fetching arguments of type float

I have a variadic template function: template<typename T, typename ArgType> vector<T> createVector(const int count, ...) { vector<T> values; va_list vl; va_start(vl, count); for (int i=0; i < count; ++i) { T value = static_cast<T>(va_arg(vl, ArgType)); values.push_back(value); } va_end(vl); return values; } Thi...

Use variadic functions in C89 without passing number of arguments or a final argument?

Let's say I have a variadic function foo(int tmp, ...), when calling foo function I need to know how many arguments there are. I'm aware of two ways of finding out how many arguments there are: Use a final argument when calling foo, like -1, so your function call will be like this: foo(tmp, 1, 2, 9, -1) and when you are inside foo and...

Help understanding the C stdarg macros (va_start ...)

Hi all, recently i came across this function (used in a logger class). I understand what it does, but i do not know how: void Reporter::print( const char* szFormat, ...) { // note: m_out is a FILE* va_list args; va_start(args, szFormat); vfprintf( m_out, szFormat, args); va_end(args); } I read the reference, but s...

var arg list to tempfile, why is it needed?

I have this code inside a constructor of a class (not written by me) and it writes a variable arg list to a tmp file. I wondered why this would be needed? The tmpfile is removed after this ctor goes out of scope and the var arg list sits inside the m_str vector. Can someone suggest a better way of doing this without the use of a tmp...

objective-c macro for a variadic function

Hello, Here's an example of what i'm trying to achieve. I'm trying to create a macro, that would look like this: SOMEMACRO(obj, obj, obj, ..., obj); The macro would compile to: some_function(obj, obj, obj, ..., obj, SOMETHING_ELSE, SOMETHING_ELSE); Here's an example macro for a 1 parameter function: #define SOMEMACRO(x) some_func...

How to create a polyvariadic haskell function?

I need a function which takes an arbitrary number of arguments (All of the same type), does something whith them and afterwards gives a result back. A list of arguments is impracticable in my specific case. As I looked through the haskell libs, there is the function printf from module Text.Printf, which uses a similar trick. Poorly I co...

Getting the type of passed argument in variadic function

Is there any trick to get the type of the passed in arguments without explicitly stating the type in a string as one of the argument? To add on the post just now, basically what I want to do is if is type A call functionA else if is type B call functionB. Could variadic template solve that? Thanks. ...

c++ variadic function: What is the best way to replace?

In my current project there's some variadic functions being used (ellipsis) that are really being used quite ofen. I have to make some impacting changes to the project, so I thought i might as well get rid of these variadics too. The question I have is, how does one best replace these if the number of arguments passed to these are so v...

Specifying one type for all arguments passed to variadic function or variadic template function w/out using array, vector, structs, etc?

I'm creating a function (possibly member function, not that it matters... maybe it does?) that needs to accept an unknown number of arguments, but I want all of them to be the same type. I know I could pass in an array or vector, but I want to be able to accept the list of args directly without extra structure or even extra brackets. I...

Variable Argument lists with boost?

Hello I wanted to write a function with a variable argument list. I want to explore my options. I'm pretty sure I came accross a boost template class that was designed for this purpose, but I can't think of the name of it? Can anyone tell me? or did I dream this up! Thanks ...

decltype with a variadic template function

I want to write a simple adder (for giggles) that adds up every argument and returns a sum with appropriate type. Currently, I've got this: #include <iostream> using namespace std; template <class T> T sum(const T& in) { return in; } template <class T, class... P> auto sum(const T& t, const P&... p) -> decltype(t + sum(p...)) { ...

Smalltalk Variadic functions

Does Smalltalk(especially Squeak/Pharo) have some form of variadic functions? I was just reading about the power of designing your own control statments in smalltalk and while I'm a big fan of ifTrue: ifFalse: I was having a hard time coming up with a good way to implement arbitrary if,if else, if else,...,else statements thinking how u...

Overriding a variadic method in objective-c

When subclassing in objective-c, how can I forward a call to the superclass in the case of a variadic method. By what should I replace the ??? below to send all the objects I got? - (void) appendObjects:(id) firstObject, ... { [super appendObjects: ???]; } ...

How to convert variadic function arguments into collection such as Set or List

Dear all, In Java, we can use variadic function in the following way: public Set packStrings(String...strings){ for (String str : strings){ //Do something on the str } //How to convert strings into a Set? } My question is, what is the type of "strings"? is it String[]? How to put the string(s) denoted by the "st...