variadic

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

杀马特。学长 韩版系。学妹 提交于 2019-12-03 03:13:05
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? For the number of arguments constraint you can easily check if sizeof...(Args) == N but for checking if all the arguments are doubles you need to build a recursive type trait that checks std::is

How to properly use references with variadic templates

*爱你&永不变心* 提交于 2019-12-03 02:19:13
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. However all the examples I've seen so far online seem to be printf like examples, the difference here seems

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

∥☆過路亽.° 提交于 2019-12-02 20:51:12
What inherent advantages do boost::any and boost::any_cast offer over using void* and dynamic_cast ? 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 least one virtual function (aka polymorphic types) boost::any calls destructors: { boost::any x = std::string(

variadic template class to make a deferred call to a variadic template function

匆匆过客 提交于 2019-12-02 11:31:23
问题 I can create a template class that stores some values in a property and let me later call a method that call a function with this arg. Like this : template <typename U> void g(U u) { cout << u << endl; } template <typename U> class C { public: U u; C(U u) { this->u = u; } void m() { g(u); } }; int main() { C<double> c(5.5); c.m(); } But how to make the same with variadic templates ? I would like to write something like : template <typename ... T> void f(T... a) { cout << "generik" << endl; }

Variadic Ada Functions

夙愿已清 提交于 2019-12-02 09:25:11
问题 I'm studying Ada because I am intrigued by the idea of strict type safety and programming contracts. The idea of "programming for forever" is nice. Anyway, the real question is whether or not Ada has variadic functions. A search on SO suggests that Ada doesn't, and the correct way to do this is with an unconstrained array whose length is determined at runtime. My question then, isn't how do you do it, but rather what is the convention for doing it correctly? Additionally, why is it that Ada

variadic template class to make a deferred call to a variadic template function

↘锁芯ラ 提交于 2019-12-02 07:12:29
I can create a template class that stores some values in a property and let me later call a method that call a function with this arg. Like this : template <typename U> void g(U u) { cout << u << endl; } template <typename U> class C { public: U u; C(U u) { this->u = u; } void m() { g(u); } }; int main() { C<double> c(5.5); c.m(); } But how to make the same with variadic templates ? I would like to write something like : template <typename ... T> void f(T... a) { cout << "generik" << endl; } template <typename ... T> class B { public: T... arg; B(T... arg) { this->arg = arg; } void m() { f(arg

CPP/GPP in Fortran variadic macro (plus Fortran // concatenation)

老子叫甜甜 提交于 2019-12-01 20:14:40
I'm trying to compile a huge, world-renowned numerical weather prediction code - written mostly in Fortran 90 - that uses cpp extensively, and successfully, with PGI, Intel and gfortran. Now, I've inherited a version where experts have added several hundred cases of variadic macros. They use Intel and fpp , which is presumably a little more Fortran-centric, and can get it all to work. I need to use gfortran, and have not been able to get cpp to work on this code with its new additions. A gross simplification of the problem is as follows - Code to preprocess: PRINT *, "Hello" // "Don" #define

Swallowing comma in variadic macros on compilers that do not recognise ##

别等时光非礼了梦想. 提交于 2019-12-01 09:27:14
I need to write a variadic macro in C which must take zero or more arguments. In gcc, that can be achieved by adding "##" after the comma, e.g. ,##____VA_ARGS____ as answered in Variadic macros with zero arguments . However, the compiler in my build system (beyond my control) does not understand the ,## syntax and so does not swallow the comma. Is there a workaround I can use? Thanks! Yes, gcc swallowing the comma is non standard and you should not rely on that. With C99 conforming preprocessors you may achieve a similar effect by testing for a macro arguments that is the empty token. For the

Swallowing comma in variadic macros on compilers that do not recognise ##

♀尐吖头ヾ 提交于 2019-12-01 07:24:53
问题 I need to write a variadic macro in C which must take zero or more arguments. In gcc, that can be achieved by adding "##" after the comma, e.g. ,##____VA_ARGS____ as answered in Variadic macros with zero arguments. However, the compiler in my build system (beyond my control) does not understand the ,## syntax and so does not swallow the comma. Is there a workaround I can use? Thanks! 回答1: Yes, gcc swallowing the comma is non standard and you should not rely on that. With C99 conforming

Variable length parameters in Objective-C

江枫思渺然 提交于 2019-12-01 00:54:18
How can i make a class method with variable length parameters, in Objective-C? For example, a method like -arrayWithObjects: NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil]; Evan Mulawski What you need is a variadic function. These functions take a flexible number of arguments, like NSLog , [NSArray arrayWithObjects:...] , etc. See this tutorial: http://www.numbergrinder.com/node/35 Copied from my answer here: Obj-C, trying to write an alternative to NSLog, but I want my function to concatenate like NSLog? Take a look at varargs, e.g.: Apple Technical Q&A QA1405 . It shouldn