variadic

Check for arguments type in a variadic template declaration

两盒软妹~` 提交于 2019-11-29 11:32:48
I got a plain variadic template declaration, just like the classic one: template <typename... Arguments> class VariadicTemplate; What I need to achieve is in by letting the VariadicTemplate class do perform some type checking; the variadic template should check in some iterative form that all the arguments received should be let say of the type <Foo> . I have seen something similar somewhere but now I can not recognize where it was :P struct Foo {}; #include <type_traits> template<class T, class...> struct are_same : std::true_type {}; template<class T, class U, class... TT> struct are_same<T,

cpp: catch exception with ellipsis and see the information

亡梦爱人 提交于 2019-11-29 11:31:14
I know that you can catch "all exceptions" and print the exception by try { //some code... }catch(const std::exception& e) { cout << e.what(); } but this is just for exceptions derived from std::exception. I was wondering if there is a way to get some information from an ellipsis catch try { //some code... }catch(...) { // ?? } If the mechanism is the same as ellipsis for functions then I should be able to do something like casting the argument of the va_list and trying to call the what() method. I haven't tried it yet but if someone knows the way I'd be excited to know how. Sorry, you can't

Repeated use of a variadic function argument doesn't work

风流意气都作罢 提交于 2019-11-29 03:43:33
I have a function that tries to log stuff to the console and also to a log file, but it doesn't work. The second use of the variable length argument gives garbage written to the console. Any ideas? void logPrintf(const char *fmt, ...) { va_list ap; // log to logfile va_start(ap, fmt); logOpen; vfprintf(flog, fmt, ap); logClose; va_end(ap); va_list ap2; // log to console va_start(ap2, fmt); printf(fmt, ap2); va_end(ap2); } The original code fails because it tries to use printf() where it needs to use vprintf() . Taking dubious points like the logOpen and logClose statements at face value (given

Count of parameters in a parameter pack? Is there a C++0x std lib function for this?

可紊 提交于 2019-11-29 03:26:42
I was just wondering if there was anything in the C++0x std lib already available to count the number of parameters in a parameter pack? I'd like to get rid of the field_count in the code below. I know I can build my own counter, but it just seems like this would be an obvious thing to include in the C++0x std lib, and I wanted to be sure it wasn't already there :) Home-grown counter implementations are most welcome too. template<const int field_count, typename... Args> struct Entity { const tuple<Args...> data; const array<const char*, field_count> source_names; Entity() : data() { } }; Yes,

A clean way to store a function and its (arbitrary-type, arbitrary-number) arguments

心已入冬 提交于 2019-11-29 02:31:22
For a library, I'd like a function to accept another function and its arguments, then store them all for calling later. The arguments must allow for any mixture of types, but the functions only need to return void. Something like this: void myFunc1(int arg1, float arg2); void myFunc2(const char *arg1); class DelayedCaller { ... public: static DelayedCaller *setup(Function func, …); }; ... DelayedCaller* caller1 = DelayedCaller::setup(&myFunc1, 123, 45.6); DelayedCaller* caller2 = DelayedCaller::setup(&myFunc2, "A string"); caller1->call(); // Calls myFunc1(), with arguments 123 and 45.6

Check if C++0x parameter pack contains a type

眉间皱痕 提交于 2019-11-29 02:25:36
I was wondering if C++0x provides any built-in capabilities to check if a parameter pack of a variadic template contains a specific type. Today, boost:::mpl::contains can be used to accomplish this if you are using boost::mpl::vector as a substitute for variadic templates proper. However, it has serious compilation-time overhead. I suppose, C++0x has compiler-level support for std::is_same. So I was thinking if a generalization like below is also supported in the compiler. template <typename... Args, typename What> struct is_present { enum { value = (What in Args...)? 1 : 0 }; }; No, you have

Does Haskell have variadic functions/tuples?

会有一股神秘感。 提交于 2019-11-29 00:07:01
问题 The uncurry function only works for functions taking two arguments: uncurry :: (a -> b -> c) -> (a, b) -> c If I want to uncurry functions with an arbitrary number of arguments, I could just write separate functions: uncurry2 f (a, b) = f a b uncurry3 f (a, b, c) = f a b c uncurry4 f (a, b, c, d) = f a b c d uncurry5 f (a, b, c, d, e) = f a b c d e But this gets tedious quickly. Is there any way to generalize this, so I only have to write one function? 回答1: Try uncurryN from the tuple package

Creating a string list and an enum list from a C++ macro

橙三吉。 提交于 2019-11-28 21:20:05
In order to make my code shorter and easier to change I want to replace something like enum{ E_AAA, E_BBB, E_CCC }; static const char *strings{"AAA", "BBB", "CCC" }; With a macro, like INIT(AAA, BBB, CCC); but when I try doing a macro with variable arguments, and stringification I get an error as the arguments are not declared. Any idea on how to do this? Dr Beco Here a solution I learned a few days ago. The simplified version that attends your question is: #define ENUM_MACRO(name, v1, v2, v3, v4, v5, v6, v7)\ enum name { v1, v2, v3, v4, v5, v6, v7};\ const char *name##Strings[] = { #v1, #v2,

How do I handle an unspecified number of parameters in Scheme?

六眼飞鱼酱① 提交于 2019-11-28 20:27:31
For example ((fn-stringappend string-append) "a" "b" "c") I know how to handle this (f x y z) . But what if there's an unknown number of parameters? Is there any way to handle this kind of problem? In Scheme you can use the dot notation for declaring a procedure that receives a variable number of arguments (also known as varargs or variadic function ): (define (procedure . args) ...) Inside procedure , args will be a list with the zero or more arguments passed; call it like this: (procedure "a" "b" "c") As pointed out by @Arafinwe, here's the equivalent notation for an anonymous procedure:

How to create a variadic generic lambda?

独自空忆成欢 提交于 2019-11-28 15:24:05
问题 Since C++14 we can use generic lambdas: auto generic_lambda = [] (auto param) {}; This basically means that its call operator is templated based on the parameters marked as auto. The question is how to create a lambda that can accept a variadic number of parameters similarly to how a variadic function template would work ? If this is not possible what is the closest thing that could be used the same way ? How would you store it ? Is it possible in a std::function ? 回答1: I am not sure what