variadic

dependent types with variadic templates

风格不统一 提交于 2020-01-04 02:54:07
问题 Can you see anything wrong with this function declaration? template<typename... Containers> std::tuple<typename Containers::value_type...> foo(const Containers &...args); When I try to call it, like this: foo(std::list<int>(), std::vector<float>()); MSVC2013 says error C2027: use of undefined type 'std::tuple<Containers::value_type> . I tried rewriting the function declaration with the "late return" syntax and it made no difference. Is there any way I can achieve what this code is trying to

Passing arguments to another variadic function

两盒软妹~` 提交于 2020-01-02 06:24:08
问题 Is there any way at all for this code to compile and work as intended without resorting to va_list stuff ? #include <iostream> void fct(void) { std::cout << std::endl; } void fct(int index, int indexes...) { std::cout << index << ' '; fct(indexes); //or fct(indexes...); ? } int main(void) { fct(1, 2, 3, 4, 5, 6, 7); return 0; } 回答1: I suspect you have misunderstood the meaning of the signature void fct (int index, int indexes...) I suspect you think that fct() expect a int single value (

C++ : create custom function dispatcher from variadic template

自闭症网瘾萝莉.ら 提交于 2020-01-01 14:56:07
问题 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

How to find the length of a parameter pack?

百般思念 提交于 2019-12-30 06:35:27
问题 Suppose I have a variadic template function like template<typename... Args> unsigned length(Args... args); How do I find the length of the parameter list using the length function ? 回答1: Use sizeof... : template<typename... Args> constexpr std::size_t length(Args...) { return sizeof...(Args); } Note you shouldn't be using unsigned , but std::size_t (defined in <cstddef> ). Also, the function should be a constant expression. Without using sizeof... : namespace detail { template<typename T>

VS2010 C++ variadic template example

天大地大妈咪最大 提交于 2019-12-30 04:24:10
问题 I have a class template and I can't seem to figure out how to perform a Variadic Template style instantiation. Here is the "code" so far of what I'm looking for: template<typename _Classname, typename... Args> class CFunctorStartExT { friend class CXXFactory; protected: template<typename U> CFunctorStartExT(typename U& _functor, Args&... args) : m_Functor(_functor), m_args(args) { } virtual bool ProcessLoop(CSomeClass* pThread) { return m_Functor(pThread, m_args); } protected: _Classname& m

Compilation Error on Recursive Variadic Template Function

我们两清 提交于 2019-12-29 06:16:12
问题 I've prepared a simple variadic template test in Code::Blocks, but I'm getting an error: No matching function for call to 'OutputSizes()' Here's my source code: #include <iostream> #include <typeinfo> using namespace std; template <typename FirstDatatype, typename... DatatypeList> void OutputSizes() { std::cout << typeid(FirstDatatype).name() << ": " << sizeof(FirstDatatype) << std::endl; OutputSizes<DatatypeList...>(); } int main() { OutputSizes<char, int, long int>(); return 0; } I'm using

How do vararg functions find out the number of arguments in machine code?

心已入冬 提交于 2019-12-28 06:02:25
问题 How can variadic functions like printf find out the number of arguments they got? The amount of arguments obviously isn't passed as a (hidden) parameter (see a call to printf in asm example here). What's the trick? 回答1: The trick is that you tell them somehow else. For printf you have to supply a format string which even contains type information (which might be incorrect though). The way to supply this information is mainly user-contract and often error-prone. As for calling conventions:

Prefix each parameter in a variadic macro that points to another macro

眉间皱痕 提交于 2019-12-25 16:39:53
问题 Let's assume that I have these macros that are prefixed with ATTRIB_ #define ATTRIB_A "a" #define ATTRIB_B "b" #define ATTRIB_C "c" I would like to be able to use a variadic macro that unpacks each given parameter then prefixes it with ATTRIB_ to obtain the full name of an attribute macro in order to expand that macro: #define ATTRIBS(...) CONFUSED_HERE(##__VA_ARGS__) Which would be used as: const char * a = ATTRIBS(A, B, C); And the result would be equal to: const char * a = "a" "b" "c"; As

Applying a function to an arbitrarily long list of arguments

半城伤御伤魂 提交于 2019-12-24 05:17:09
问题 I want to create a function apply that takes a function with an arbitrary amount of arguments as well as a list of integers, and returns the result of the function (Where each integer in the list is an argument in order. I was thinking something like: apply :: ([Int] -> Int) -> [Int] -> Int apply f x:xs = apply (f x) xs apply f [] = f But I know this won't work because the type signature is wrong - the function doesn't take a list of ints, it just takes some amount of int arguments.

C++ variadic template arguments method to pass to a method without variadic arguments

元气小坏坏 提交于 2019-12-23 18:30:23
问题 I have the following question, I really can't compile from all the questions and articles researched: In C++, is it possible to have a method with variadic template arguments that specify types of arguments (as a meta-description type for parameters of in, out, in/out of a certain type, to be passed by value, by address etc.), to loop through these variadic arguments in order to instantiate variables of specified types, and be passed these variables to functions specified by a pointer in a