variadic

Data structure with variadic templates

自古美人都是妖i 提交于 2019-12-11 02:13:17
问题 I have a Menu<T> class, whose options are items of type T, and it may have submenus of type Menu<T> (with no limit to the depth of nested submenus). template <typename T> class Menu { private: class Option { const std::string name; const T item; Menu<T>* submenu; Option* next = nullptr; friend class Menu<T>; Option (const std::string& itemName, const T& t, Menu<T>* menu = nullptr) : name(itemName), item(t), submenu(menu) {} ~Option() {if (submenu) delete submenu;} inline T choose() const;

Call function with (unknown) variable number of parameters?

青春壹個敷衍的年華 提交于 2019-12-10 17:38:40
问题 I'm need to send params to the function array_intersect_key() but sometimes i'm need to send 2 arrays, sometimes - 3 or more: array_intersect_key($arr_1, $arr_2); OR array_intersect_key($arr_1, $arr_2, $arr_3, $arr_4); 回答1: Assuming you want to create your own function like this, the key is to use func_get_args() : function mumble(){ $args = func_get_args(); foreach ($args as $arg){ ... whatever } } If you just want to call it with multiple args, either "just do it", or use call_user_func

What is a good typesafe alternative to variadic functions in C++?

牧云@^-^@ 提交于 2019-12-10 13:14:22
问题 In joint with this question. I am having trouble coming up with a good type safe solution to the following seemingly basic problem. I have a class music_playlist that has a list of the songs it should play. Seems pretty simple right, just make an std::list of all the songs in queue, and make it available to the user. However, out of necessity the audio decoding, and the audio rendering happens on separate threads. So the list needs to be mutex protected. Often times the mutex was forgotten by

Ltac : optional arguments tactic

感情迁移 提交于 2019-12-10 10:06:58
问题 I want to make a Ltac tactic in coq which would take either 1 or 3 arguments. I have read about ltac_No_arg in the LibTactics module but if I understood it correctly I would have to invoke my tactic with : Coq < mytactic arg_1 ltac_no_arg ltac_no_arg. which is not very convenient. Is there any way to get a result like this ? : Coq < mytactic arg_1. Coq < mytactic arg_1 arg_2 arg_3. 回答1: We can use the Tactic Notation mechanism to try to solve your issue because it can handle variadic

How to write C function accepting (one) argument of any type

落花浮王杯 提交于 2019-12-10 03:42:01
问题 I am implementing simple library for lists in C, and I have a problem with writing find function. I would like my function to accept any type of argument to find, both: find(my_list, 3) and find(my_list, my_int_var_to_find) . I already have information what is type of list's elements . For now I've found couple of ways dealing with this: different function with suffix for different types: int findi(void* list, int i) , int findd(void* list, double d) - but I don't like this approach, it seems

Multikey map using variadic templates

◇◆丶佛笑我妖孽 提交于 2019-12-09 17:38:46
问题 I'm trying to implement a map with different access keys using variadic templates in c++. What I want to get is to make such syntax work: MultikeyMap<int, double, float> map1; // int and double are keys, float is value type map1[ 2 ] = 3.5; map1[ 5.7 ] = 22; MultikeyMap<unsigned long long, int, float, double, int> map2; // more keys, int is value type map2[100000000000ULL] = 56; // etc... What I have now looks like: template<class V, class... Krest> class MultikeyMap; template<class V, class

Contaner for different functions?

泄露秘密 提交于 2019-12-09 09:12:02
问题 I'm trying to implement a container class for different functions where I can hold function pointers and use it to call those functions later. I'll try to discribe my problem more accurate. As example, I have 2 different test functions: int func1(int a, int b) { printf("func1 works! %i %i\n", a, b); return 0; } void func2(double a, double b) { printf("func2 works! %.2lf %.2lf\n", a, b); } and I also have array of variants, which holds function arguments: std::vector<boost::variant<int, double

Swift 3.0 closure expression: what if the variadic parameters not at the last place in the parameters list?

对着背影说爱祢 提交于 2019-12-08 14:16:25
Update at 2016.09.19 There is a tricky, indirect way to use variadic parameters before some other parameters in closure expression parameters list, haha let testClosure = { (scores: Int...) -> (_ name: String) -> String in return { name in return "Happy" } } let k = testClosure(1, 2, 3)("John") And I found some related issues in bugs.swift.org: SR-2475 SR-494 Original Post According to the document of Swift 3.0 , for a closure expression, "variadic parameters can be used if you name the variadic parameter"(see Closure Expresssion Syntax part). But for Swift 2.x, the description is "Variadic

Store a function with arbitrary arguments and placeholders in a class and call it later

强颜欢笑 提交于 2019-12-08 00:08:31
问题 So I am creating a type of event handler and I am in the process of writing an "Event Listener Wrapper", if you will. The basic idea is this: When you want to subscribe to an event, you create a function that should be called when the event fires. <-- already have that done (kinda, I'll explain) You put this listener function into a wrapper to pass the function onto the dispatcher. The dispatcher gets an event, finds the wrapper for you listener, and calls the underlying function with the

Concatenation of tokens in variadic macros

不想你离开。 提交于 2019-12-07 15:55:34
问题 In C, is it possible to concatenate each of the variable arguments in a a variadic macro? Example: MY_MACRO(A, B, C) // will yield HDR_A, HDR_B, HDR_C MY_MACRO(X, Y) // will yield HDR_X, HDR_Y The normal ## operator has special meaning for variadic macros (avoiding the comma for empty argument list). And concatenation when used with __VA_ARGS__ takes place with the first token only. Example: #define MY_MACRO(...) HDR_ ## __VA_ARGS__ MY_MACRO(X, Y) // yields HDR_X, Y Suggestions? 回答1: First,