variadic

Cocoa - Calling a variadic method from another variadic one (NSString stringWithFormat call)

南笙酒味 提交于 2019-12-04 12:32:16
I have a problem with [NSString strigWithFormat:format] because it returns an id, and I have a lot of code where I changed a NSString var to an other personal type. But the compiler does not prevent me that there are places where a NSString is going to be set into another type of object. So I'm writing a category of NSString and I'm goind to replace all my calls to stringWithFormat to myStringWithFormat . The code is : @interface NSString (NSStringPerso) + (NSString*) myStringWithFormat:(NSString *)format; @end @implementation NSString (NSStringPerso) + (NSString*) myStringWithFormat:(NSString

C++ : create custom function dispatcher from variadic template

谁都会走 提交于 2019-12-04 12:24:53
I have some functions that read various types from serialized data, eg: class DataDeserializer { int getInt(); std::string getString(); MyClass getMyClass(); } I then have various callback functions that take arbitrary parameters, eg: void callbackA (int, int, int); void callbackB (int, std::string); void callbackC (std::string, int, MyClass, int); I want to call the various callbacks with arguments read from the deserialized data stream. What I would like is to automate the boilerplate code as much as possible. I was thinking maybe I could use templates. If I had some sort of Dispatcher class

Multikey map using variadic templates

点点圈 提交于 2019-12-04 06:31:40
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 K, class... Krest> class MultikeyMap<V, K, Krest...> : protected std::map<K, V>, protected MultikeyMap

How to write a generic variadic lambda that discards its parameters?

我与影子孤独终老i 提交于 2019-12-04 03:22:44
问题 I want to write a lambda that takes an arbitrary number of arguments by universal reference and ignores them entirely. The obvious method would be to use the syntax for a variadic universal parameter pack and omit the parameter name: auto my_lambda = [](auto&&...) { return 42; }; This works fine (with gcc 4.9.2) until I try to pass a non trivially-copyable object: struct S { S() {} S(S const&) {} }; my_lambda("meow", 42, S{}); ^ error: cannot pass objects of non-trivially-copyable type

Mixins, variadic templates, and CRTP in C++

╄→尐↘猪︶ㄣ 提交于 2019-12-03 17:02:57
问题 Here's the scenario: I'd like to have a host class that can have a variable number of mixins (not too hard with variadic templates--see for example http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.103.144). However, I'd also like the mixins to be parameterized by the host class, so that they can refer to its public types (using the CRTP idiom). The problem arises when trying to mix the two--the correct syntax is unclear to me. For example, the following code fails to compile with g++ 4

How to properly use references with variadic templates

蹲街弑〆低调 提交于 2019-12-03 12:49:46
问题 I have something like the following code: template<typename T1, typename T2, typename T3, typename T4> void inc(T1& t1, T2& t2, T3& t3, T4& t4) { ++t1; ++t2; ++t3; ++t4; } template<typename T1, typename T2, typename T3> void inc(T1& t1, T2& t2, T3& t3) { ++t1; ++t2; ++t3; } template<typename T1, typename T2> void inc(T1& t1, T2& t2) { ++t1; ++t2; } template<typename T1> void inc(T1& t1) { ++t1; } I'd like to reimplement it using the proposed variadic templates from the upcoming standard.

Variadic macros with 0 arguments in C99

六月ゝ 毕业季﹏ 提交于 2019-12-03 12:49:37
I have some debugging code that looks like the following: #define STRINGIFY(x) #x #define TOSTRING(x) STRINGIFY(x) #define AT __FILE__ ":" TOSTRING(__LINE__) void __my_error(const char*loc, const char *fmt, ...); #define my_error(fmt, ...) __my_error(AT, fmt, ##__VA_ARGS__) The last macro is used so I can insert the location into the debug output as to where the error occurred. However, when I call the function like this: my_error("Uh oh!"); I would like my code to be C99, so I find when this compiles, I get the following error: error: ISO C99 requires rest arguments to be used I know I can

A variadic template method to accept a given number of doubles?

孤者浪人 提交于 2019-12-03 11:57:53
问题 template <unsigned int N> class myclass { public: template <typename... Args> void mymethod(Args... args) { // Do interesting stuff } }; I want mymethod to be called only with exactly N doubles. Is that possible? That is, say that I have: myclass <3> x; x.mymethod(3., 4., 5.); // This works x.mymethod('q', 1., 7.); // This doesn't work x.mymethod(1., 2.); // This doesn't work How can I get this done? 回答1: For the number of arguments constraint you can easily check if sizeof...(Args) == N but

Why is `boost::any` better than `void*`?

Deadly 提交于 2019-12-03 07:20:43
问题 What inherent advantages do boost::any and boost::any_cast offer over using void* and dynamic_cast ? 回答1: The advantage is that boost::any is way more type-safe than void* . E.g. int i = 5; void* p = &i; static_cast<double*>(p); //Compiler doesn't complain. Undefined Behavior. boost::any a; a = i; boost::any_cast<double>(a); //throws, which is good As to your comment, you cannot dynamic_cast from a void* . You can dynamic_cast only from pointers and references to class types which have at

Mixins, variadic templates, and CRTP in C++

我们两清 提交于 2019-12-03 06:00:28
Here's the scenario: I'd like to have a host class that can have a variable number of mixins (not too hard with variadic templates--see for example http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.103.144 ). However, I'd also like the mixins to be parameterized by the host class, so that they can refer to its public types (using the CRTP idiom). The problem arises when trying to mix the two--the correct syntax is unclear to me. For example, the following code fails to compile with g++ 4.4.1: template <template<class> class... Mixins> class Host : public Mixins<Host<Mixins>>... { public: