variadic-templates

template variadic function: arbitrary number of classes with arbitrary number of constructor parameters

馋奶兔 提交于 2019-12-11 05:07:09
问题 I am unsure of what the proper name is, so far I comprehend the notion of using variadic template arguments (e.g., in comparison to std::initializer_list ). So let's assume there is an arbitrary number of classes k and an arbitrary number of parameters i which is dependant on each class k constructor. I know that I may construct any class k using a variadic template: template <typename T, typename... Args> void make_class(Args... args) { auto k = T(args...); } However I want to allow creation

c++ variadic template pack expansion by groups of arbitrary size

邮差的信 提交于 2019-12-11 04:54:41
问题 (Somehow related to this previous question) I want to evaluate the parameters of a template function by groups of N parameters. Something like this: template <size_t N, typename ... Ts> void evaluate(Ts const & ... fn) { for( int i=0; i<2; i++ ) runH<N>::func(i, fn...); } int main() { run<3>( // N = 2 [](int i){ cout << "lambda func 1: " << std::to_string( i ) << endl; }, [](int i){ cout << "lambda func 2: " << std::to_string( i ) << endl; }, [](int i){ cout << "lambda func 3: " << std::to

Sample function processing time decorator in c++11

我与影子孤独终老i 提交于 2019-12-11 04:06:18
问题 I am trying to write a template that will take any function and log its time and after some struggle with template syntax i have come up with below solution: template <typename Func, typename... Args> auto timeMyFunction(Func f, Args... args)-> typename std::enable_if<std::is_same<decltype(f(args...)),void>::value,void>::type { auto start = std::chrono::steady_clock::now(); f(args...); auto end = std::chrono::steady_clock::now(); std::chrono::duration<double> diff = end-start; std::cout <<

C++ execute an action for each type in a given list

拈花ヽ惹草 提交于 2019-12-11 04:06:15
问题 I have been given the charge to refactor old code and have stumbled upon this case: void myClass::doStuff() { for( myIterator< Type1 > it( this->getDatabase() ); it; ++it ) { do1( *it ); do2( *it ); do3( *it ); } for( myIterator< Type2 > it( this->getDatabase() ); it; ++it ) { do1( *it ); do2( *it ); do3( *it ); } for( myIterator< Type3 > it( this->getDatabase() ); it; ++it ) { do1( *it ); do2( *it ); do3( *it ); } } It's obviously bad since I copy basically the same code 3 times so I decided

Getting nth variadic argument value (not type)

痴心易碎 提交于 2019-12-11 03:47:01
问题 Ignore the missing perfect forwarding. (Assume arguments are perfectly forwarded in the real implementation.) // Base case: no args template<typename TF> void forEach2Args(TF) { } // Recursive case: some args template<typename TF, typename... Ts> void forEach2Args(TF mFn, Ts... mXs) { mFn(getNth<0>(mXs...), getNth<1>(mXs...)); forEach2Args(mFn, getAllAfter<2>(mXs...)); } int main() { int result{0}; forEach2Args([&result](auto a1, auto a2) { result += (a1 * a2); }, 2, 4, 3, 6); // roughly

Deduce variadic args and return type from functor template parameter (MSVC-specific)

微笑、不失礼 提交于 2019-12-11 02:43:44
问题 The function invoke in the following code is a simple wrapper for invoking another function / functor / lambda, such that invoke(f,args...) equals f(args...) . (The reason behind this is to also have overloads for member functions, allowing a generalized syntax for both usages.) This implementation works in g++: template <class Fn, class ...Args> auto invoke(Fn f, Args ...args) -> decltype(f(args...)) { return f(args...); } int y = invoke([](int x){ return x+1; }, 42); // y = 43 Demo However,

Create std::array from variadic template

混江龙づ霸主 提交于 2019-12-11 02:24:14
问题 I'd like to achieve something like that: #include <string> #include <array> enum class MyEnum{ A, B, C }; template<MyEnum... Args> class MyClass{ public: MyClass() { } private: std::array<MyEnum, sizeof...(Args)> array; }; Now I have an array, which can hold all passed to template values. But how can I populate this array with template parameters? 回答1: If what you are wanting is to put all the MyEnum values into array , then you can unpack them into an initialiser list and initialise array

recursive variadic template can't deduce argument

送分小仙女□ 提交于 2019-12-11 02:07:16
问题 I have a recursive function template where type 'T' is used internally but the variadic template argument is used only for generate the recursive template. int qi(int t) { return 0; } template <typename T, typename... U> int qi(int t) { //do stuff... return qi<U...>(t); // <--- error here } If I try to compile it, I get the error could not deduce template argument for 'T' . Why can't the function be deduced to the non template variant of qi ( int qi(int t){} ) ? 回答1: The problem you're

How to find maximum value in parameter pack?

↘锁芯ラ 提交于 2019-12-11 01:54:26
问题 Here is an example of problem: a constant variable's template should expand its type based on parameters. While direct way is possible, by giving size of type, or underlying typename, it's error-prone. #include <iostream> template<size_t bit> constexpr const uint16_t BIT = 1 << bit; template<size_t... bits> constexpr const uint16_t BITS = (uint16_t(1 << bits)|...); int main() { std::cout << BITS<0,1,3,12> << std::endl; } Idea is to implement template data type which would return type which is

How do I change the number of template arguments supported by MSVC++'s std::tuple?

人盡茶涼 提交于 2019-12-11 01:06:47
问题 MSVC++ doesn't yet support variadic templates, so its standard library "fakes" these for classes like std::tuple through use of macros. I recently tried compiling one of my projects with the VC11 beta, and got this to show for it: gtest\gtest.h(9735): error C2977: 'std::tuple' : too many template arguments c:\program files (x86)\microsoft visual studio 11.0\vc\include\utility(72) : see declaration of 'std::tuple' gtest\gtest.h(9743): error C2977: 'std::tuple' : too many template arguments