variadic-functions

Is there any way to retrieve a float from a varargs function's parameters?

限于喜欢 提交于 2019-12-24 00:18:35
问题 If the function was defined with a prototype which explicitly stated the types of the parameters, eg. void somefunc(int arg1, float arg2); but is implemented as void somefunc(int arg1, ...) { ... } is it possible to use va_arg to retrieve a float? It's normally prevented from doing this because varargs functions have implicit type promotions, like float to double, so trying to retrieve an unpromoted type is unsupported, even though the function is being called with the unpromoted type do to

Scala type ascription for varargs using _* cause error

懵懂的女人 提交于 2019-12-23 20:03:22
问题 I have a rudimentary understanding of Scala varargs: that parameters to a method accepting varargs need to hint that it is a varargs using _* . Using Scala 2.10.3, I define the following two methods scala> def method(varargs:Int*)(more:String*) = println(varargs,more) method: (varargs: Int*)(more: String*)Unit scala> val method2 = method(1,2,3)_ method2: Seq[String] => Unit = Invoking them directly using a List or Range works fine scala> val paramList = List("hi","ho") paramList: List[java

variadic function - how to ensure parameters passed correctly

社会主义新天地 提交于 2019-12-23 19:35:12
问题 Is there any way (built-in or a code pattern) to ensure that a variadic function is passed the correct number of parameters? (This will be included as part of an API obviously, I can check my own internal code.) I was considering requiring a UN32 Magic Number to be the last argument passed and check that for validity in the variadic function. Does anyone have any thoughts on that? 回答1: You could use the PP_NARG macro to add a count semi-automatically. int myfunc (int count, ...); #define

Is using the address of an uninitialized variable UB? [duplicate]

喜夏-厌秋 提交于 2019-12-23 17:50:28
问题 This question already has answers here : Is it undefined behavior to take the address of an uninitialized pointer? (5 answers) Closed 3 years ago . Is this small code UB? void Test() { int bar; printf("%p", &bar); } IMO it's not UB, but I'd like some other opinions. It simply prints the address of bar , even if bar has never been initialized. 回答1: TL:DR No, your code does not invoke UB by using anything uninitialized , as you might have thought. The address of a(ny) variable (automatic, in

dynamic_cast<> ing variable arguments to templates

孤者浪人 提交于 2019-12-23 15:11:47
问题 I have a C++ application that executes test cases. It is possible that some test cases will depend on output from other test cases. All test cases implement a basic interface: /// base class for all test cases class ITest { public: virtual void Execute() = 0; }; Test cases that produce some object that may be useful to other test cases implement this interface: /// implemented by test cases that provide data to other test cases template< class Obj > class IDependency { public: virtual Obj Get

How to override a variadic method in Objective-C

青春壹個敷衍的年華 提交于 2019-12-23 11:54:12
问题 I'm trying to extend a class that has a variadic method such as: - (void)someMethod:(id)arguments, ... ; and in the subclass override it by calling the original method like: - (void)someMethod:(id)arguments, ... { [super someMethod:arguments, ...]; // override implementation ... } but this doesn't work. Anyone know how to work this? Thanks. 回答1: similar to printf / vprintf , the base would declare: - (void)someMethod:(id)arguments, ... ; the subclass would implement: - (void)vsomeMethod:(id

Passing variadic arguments in one function to another function in D

醉酒当歌 提交于 2019-12-23 10:58:28
问题 I have a variadic D-style function foo(format, ...) , which is a wrapper around writefln . I'd like to do something like this: foo(format, <...>) { //... writefln(format, ...); } Essentially, passing on the ellipsis parameter(s) to writefln. I understand that this isn't easy/possible in C/C++, but is there a way to accomplish this in D? 回答1: This will do it for you: import std.stdio; void customWrite(Args...)(string format, Args args) { writefln(format, args); } 回答2: I had forgotten that

Suppressing “ISO C99 requires rest arguments to be used”

拈花ヽ惹草 提交于 2019-12-23 07:25:41
问题 Consider the following two macros: #define PNORM( v, s, ... ) { \ if( VERBOSITY_CHECK( v ) ) { \ if( ( errno = pthread_mutex_lock(&server.output_mutex) ) ) { \ PERROR_LOCKFREE( normal, "\tpthread_mutex_lock failed on output_mutex.\r\n" ) ; \ } \ fprintf( stdout, s, ## __VA_ARGS__ ) ; \ fflush( stdout ) ; \ if( ( errno = pthread_mutex_unlock(&server.output_mutex) ) ) { \ PERROR_LOCKFREE( normal, "\tpthread_mutex_unlock failed on output_mutex.\r\n" ) ; \ } \ } \ } #define PERROR_LOCKFREE( v, s,

EnumSet from array, shortest variant?

老子叫甜甜 提交于 2019-12-23 06:47:18
问题 I need an EnumSet from an array (which is given through a varargs method parameter). First, I was surprised that there is no varargs constructor method in EnumSet (there is EnumSet#of(E first, E... rest) ). As a workaround, I used the following variant: EnumSet<Options> temp = EnumSet.copyOf(Arrays.asList(options)); However, this triggers a java.lang.IllegalArgumentException: Collection is empty . So, now I ended up with the following, which looks somewhat ridiculous: EnumSet<Options> temp =

Type safety: Potential heap pollution via varargs parameter subtrees [duplicate]

扶醉桌前 提交于 2019-12-23 05:26:28
问题 This question already has answers here : Possible heap pollution via varargs parameter (5 answers) Closed 3 years ago . I'm reading J. Bloch's effective Java and I got that unchecked casts is never good unless we made sure that the cast is safe. Now, since Java Collection frameworks doesn't provide us with Tree data structure I have to write on my own. public interface TreeVisitor<E, R> { public R visit(E leaf); public R visit(E val, Tree<E>... subtrees); } public abstract class Tree<E> {