variadic-templates

Overloaded function as argument of variadic template function

邮差的信 提交于 2020-01-11 05:03:05
问题 I'm trying to make variadic template function, which takes as arguments overloaded function and its arguments :) int sumall(int a) { return a; } int sumall(int a, int b) { return a+b; } template<typename R, typename... A> R doit( R(*f)(A...), A... a) { return f(a...); } I want to call doit without any template specifiers nor casting: cout << doit(sumall, 7, 6) << endl That doesn't compile, but when return types are void, everything work perfect: void printsum(int a) { cout << a << endl; }

g++ variadic template issue

天涯浪子 提交于 2020-01-11 04:40:07
问题 So I gave this program to g++ and clang (both on Linux, x86_64): #include <iostream> using namespace std; template<char... Cs> struct A { static const string s; static A a; ~A() { cout << "s = " << s << "\n"; } }; template<char... Cs> const string A<Cs...>::s = {{Cs...}}; template<char... Cs> A<Cs...> A<Cs...>::a; int main(void) { (void)A<'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'>::a; return 0; } Clang outputs s = aaaaaaaaaaaaaaaa (as expected). g++

Wrap a function pointer in C++ with variadic template

我们两清 提交于 2020-01-09 19:48:47
问题 The Question I have a number of C++ functions void f() , R g(T a) , S h(U a, V b) and so on. I want to write a template function that accepts f , g , h and so on as a template argument and calls that function. ie I want something like this: template<MagicStuff, WrappedFunction> ReturnType wrapper(MagicallyCorrectParams... params) { extra_processing(); // Extra stuff that the wrapper adds return WrappedFunction(params); } ... wrapper<f>(); // calls f wrapper<g>(T()); // calls g wrapper<h>(U(),

Wrap a function pointer in C++ with variadic template

允我心安 提交于 2020-01-09 19:47:33
问题 The Question I have a number of C++ functions void f() , R g(T a) , S h(U a, V b) and so on. I want to write a template function that accepts f , g , h and so on as a template argument and calls that function. ie I want something like this: template<MagicStuff, WrappedFunction> ReturnType wrapper(MagicallyCorrectParams... params) { extra_processing(); // Extra stuff that the wrapper adds return WrappedFunction(params); } ... wrapper<f>(); // calls f wrapper<g>(T()); // calls g wrapper<h>(U(),

Is there a way to define Variadic template macro?

旧街凉风 提交于 2020-01-07 09:56:06
问题 Is there a way to define variadic template macro just like variadic macro? For example, if define variadic macro like: #define PRINT_STRING(fmtId, ...) { \ CString fmt; \ fmt.FormatString(fmt, ##__VA_ARGS__); \ cout << fmt << endl; } Could we define something like: #define PARSE_FUNCTION(functionName, typename...) \ std::function<int(typename...)> m_##functionName(){ \ return (std::function<int(typename...)>) functionName; } 回答1: __VA_ARGS__ can be used multiple times, so you could write:

Initializing a static char based on template parameter

吃可爱长大的小学妹 提交于 2020-01-07 02:31:28
问题 I want to do something like this : template<typename T> const char * toStr(T num) { thread_local static char rc[someval*sizeof(T)] str = "0x000...\0"; // num of zeros depends on size of T // do something with str return str; } I'm guessing there's some template metaprogramming I'd have to do but I'm not sure where to start. Edit: I found a related question here: How to concatenate a const char* in compile time But I don't want the dependency on boost. 回答1: Not sure to understand what do you

C++ Calling a virtual method from a multiply inherited template class

社会主义新天地 提交于 2020-01-06 19:28:56
问题 I have a lot of code here but I'm afraid this is as little code as I could put to convey the problem, so please bear with me: #include <iostream> #define ASINSTANCE(x, type, y) \ type * y = dynamic_cast<type *>(&(x)); \ if (y) class Fruit { virtual void a() = 0; // This is to surpress the "Fruit isn't polymorphic" we'd otherwise get. }; class Apple : public Fruit { virtual void a() { } }; class Orange : public Fruit { virtual void a() { } }; class Banana : public Fruit { virtual void a() { }

Rare bug with variadic templates?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-06 14:14:46
问题 I were using variadic templates in a program and it's come up an unexpected error. I isolated the error and I has shocked it: #include<cctype> #include<iostream> // try to delete this line class A { public: void constructor() { } template<typename... Args> void constructor( int (*f)(int), Args... args ) { // process( f ) constructor( args... ); } template<typename... Args> A( Args... args ) { constructor( args... ); } }; int main() { A a; a.constructor( std::isspace ); // ok A b( std::isspace

Using template parameter pack instead of macro

孤者浪人 提交于 2020-01-06 05:57:10
问题 I am reading Modern C++ design by Andrei Alexandrescu and I am trying to use some of the type list examples that he his giving. In the example below I want to create a list of a Option struct that holds a type and an integer. Later I want to create a typelist of those Options and then pass that to a another struct FindTypeForMapping along with an integer. If the integer match any of the integer set in the list of Options, then the expression should evaluate to the type for that Options,

template multiple variadic inheritance with variadic argument types

十年热恋 提交于 2020-01-06 04:23:06
问题 I need to inherit multiple times the following class, taking variadic arguments as template parameters. template <class SignalDispatcherClass, class ... ArgTypes> class ISignalMap { //private public: void RegisterSlot(SignalAddress pSignalFunc, ISlotInvoker<ArgTypes...>* pSlotInvoker) { //implementation } }; So far I can expand a parameter pack and get multiple class specializations, but with functions taking only one argument. template <class SignalDispatcherClass, class ... ArgTypes> class