variadic

c variadic functions confusion

最后都变了- 提交于 2021-02-08 07:10:25
问题 I'm trying to figure out what's behind va_start(), va_arg() macroses. The code below works well. #include <iostream> #include <cstdarg> void f(double a, double b, ...) { va_list arg; va_start(arg, b); double d; while((d = va_arg(arg, double)) != 0) { std::cout << d << '\n'; } } int main(int argc, char *argv[]) { f(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 0.0); return 0; } As I was expected it gave such output: 3 4 5 6 7 8 9. Then I found definitions of that macroses (in internet, cause my

c variadic functions confusion

旧巷老猫 提交于 2021-02-08 07:07:03
问题 I'm trying to figure out what's behind va_start(), va_arg() macroses. The code below works well. #include <iostream> #include <cstdarg> void f(double a, double b, ...) { va_list arg; va_start(arg, b); double d; while((d = va_arg(arg, double)) != 0) { std::cout << d << '\n'; } } int main(int argc, char *argv[]) { f(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 0.0); return 0; } As I was expected it gave such output: 3 4 5 6 7 8 9. Then I found definitions of that macroses (in internet, cause my

template template parameter expansion for variadic templates

丶灬走出姿态 提交于 2021-02-06 08:56:15
问题 I recently learned about the existence of template template parameters and was now wondering if something like this would be possible: template<template<class... > class Container, typename... args> struct ContainerTemplate { using container = std::tuple<Container<args...>...>; }; what i want is a template that gets a Container or some other template class as a template template parameter and then expands the rest of the template arguments in such a way that if Container has N template args

Divorce a parameter pack in a class template

大憨熊 提交于 2020-07-15 06:11:56
问题 I am trying to write a class template that uses a parameter-pack and implements a member function for each type contained in the parameter-pack. This is what I have so far: template <typename...T> class Myclass { public: void doSomething((Some_Operator_to_divorce?) T) { /* * Do Something */ std::cout << "I did something" << std::endl; } }; My goal is to have a class template that can be used in the following way: Myclass<std::string, int, double> M; M.doSomething("I am a String"); M

Divorce a parameter pack in a class template

梦想与她 提交于 2020-07-15 06:11:27
问题 I am trying to write a class template that uses a parameter-pack and implements a member function for each type contained in the parameter-pack. This is what I have so far: template <typename...T> class Myclass { public: void doSomething((Some_Operator_to_divorce?) T) { /* * Do Something */ std::cout << "I did something" << std::endl; } }; My goal is to have a class template that can be used in the following way: Myclass<std::string, int, double> M; M.doSomething("I am a String"); M

How to use class template argument to change argument calls and function signatures?

不羁的心 提交于 2020-06-01 07:40:28
问题 I am trying to find way to design a class template so that an int value is passed, and several function signatures as well as argument lists are dependent on this value. Particularly, considering MyClass : template <int N> class MyClass { typedef SomeType<int, int, int, /* ... N times*/ > MyDepType; myFunction(std::string arg0, std::string arg1, /* ...*/ std::string argN) { /* do stuff */}; public: MyClass() { someFunction(float arg0, float arg1, /* ...*/ float argN); // < someOtherFunction

Extract just the argument type list from decltype(someFunction)

谁说我不能喝 提交于 2020-02-01 20:46:51
问题 I have a variadic template that represents a list of parameters for a function, eg: void myFunc (int,int,std::string) { } template<typename... Args> class MyTemplateClass { }; ... MyTemplateClass<int,int,std::string> myConcrete; // for use with myFunc later Is there any way I can extract just the argument types from decltype(func) to save having to write them manually, eg: MyTemplateClass<something_like_decltype(myFunc)> myConcrete; ie decltype in this case would give me "void(int,int,string)

Handling a void variable in a templatized function in C++11

孤街醉人 提交于 2020-01-24 04:29:22
问题 I have a template class that must perform some operation before calling a function whose parameters and return type are generic. This is the method: template <typename ReturnType, typename ...Args> ReturnType function (Args ...args) { // prepare for call // ... ReturnType rv = makeCall(args...); // [1] // dismiss the call // ... return rv; } Of course it's compiling correctly when ReturnType is not void . When I use it in this context: function<void>(firstArg, secondArg); The compiler