variadic-templates

Handling zero-argument variadic template in C++11

拟墨画扇 提交于 2020-01-02 01:29:55
问题 Consider the following artificial example: template <typename T, typename... Args> struct A { typedef T Type; }; Using A with 1 or more arguments works while using it with zero arguments fails as expected: error: wrong number of template arguments (0, should be 1 or more) Is it possible to make A handle the case of zero template arguments defining A::Type to int if there are no arguments and to the first template argument if there are? 回答1: First define the primary template as the most

Which is the more specialized template function? clang and g++ differ on that

回眸只為那壹抹淺笑 提交于 2020-01-02 00:13:11
问题 While playing with variadic templates, following this SO question (note: it is not mandatory to go there for following this question), I came to a different behavior of clang (3.8) and g++ (6.1) for the following template overloaded functions: template <class... Ts> struct pack { }; template <class a, class b> constexpr bool starts_with(a, b) { return false; } template <template <typename...> class PACK_A, template <typename...> class PACK_B, typename... Ts1, typename... Ts2> constexpr bool

Variadic template function accepting lambda

徘徊边缘 提交于 2020-01-01 15:07:30
问题 I'm trying to understand the compiler error that I'm getting fo the code below. I've got a variadic template function which accepts a lambda with the specified types, and attempting to call that function results in the template not being considered a valid candidate due to a mismatch. #include <functional> template<typename ... ResultTypes> void executeWithResultHandler(std::function<void (ResultTypes...)> lambda) { } int main(int argc, char **argv) { executeWithResultHandler<int>([] (int arg

Functional composition with variadic templates in C++11

我的未来我决定 提交于 2020-01-01 01:11:11
问题 I'm a mathematician used to doing "old style" C++ programming for a long time now. I feel that some new syntactic constructions offerred by C++11 could help me achieve some better code regarding my professionnal projects. Yet as I'm no CS professionnal I must confess that I lack the knowledge to understand some examples I encounter in my self-learning process, altough I've been pretty lucky/succesful so far. My impression is that variadic templates can be used to implement type-safe functions

Converting Variadic template pack into std::initializer_list

微笑、不失礼 提交于 2019-12-31 13:55:35
问题 Assume that there is a function which accepts several strings: void fun (const std::initializer_list<std::string>& strings) { for(auto s : strings) // do something } Now, I have a variadic template function say foo() as: template<typename ...Args> void foo () { fun(???); } This method is called externally as: foo<A, B, C, D>(); // where A, B, C, D are classes And these classes which are passed as arguments are expected to contain a common static const member: static const std::string value =

Understanding the overhead of lambda functions in C++11

…衆ロ難τιáo~ 提交于 2019-12-31 08:07:45
问题 This was already touched in Why C++ lambda is slower than ordinary function when called multiple times? and C++0x Lambda overhead But I think my example is a bit different from the discussion in the former and contradicts the result in the latter. On the search for a bottleneck in my code I found a recusive template function that processes a variadic argument list with a given processor function, like copying the value into a buffer. template <typename T> void ProcessArguments(std::function

Point of declaration for variadic template

穿精又带淫゛_ 提交于 2019-12-30 09:52:27
问题 At what point is a variadic template considered "declared"? This compiles under clang++ 3.4, but not under g++ 4.8.2. template <typename T> const T &sum(const T &v) { return v; } template <typename T, typename ... Ts> auto sum(const T &v, const Ts & ... params) -> decltype(v + sum(params...)); template <typename T, typename ... Ts> auto sum(const T &v, const Ts & ... params) -> decltype(v + sum(params...)) { return v + sum(params...); } int main() { sum(1, 2, 3); } Apparently g++ won't match

Point of declaration for variadic template

霸气de小男生 提交于 2019-12-30 09:52:16
问题 At what point is a variadic template considered "declared"? This compiles under clang++ 3.4, but not under g++ 4.8.2. template <typename T> const T &sum(const T &v) { return v; } template <typename T, typename ... Ts> auto sum(const T &v, const Ts & ... params) -> decltype(v + sum(params...)); template <typename T, typename ... Ts> auto sum(const T &v, const Ts & ... params) -> decltype(v + sum(params...)) { return v + sum(params...); } int main() { sum(1, 2, 3); } Apparently g++ won't match

Order of parameter pack expansion

£可爱£侵袭症+ 提交于 2019-12-30 09:38:56
问题 I have 2 functions to read binary file. 1st function reads sizeof(T) bytes from file: template<typename T> T read() { ... some IO operations ... }; 2nd function calls first one multiple times with each template parameter: template<typename... Ts> std::tuple<Ts...> read_all() { return std::make_tuple(read<Ts>()...); }; Everything works fine except of 1st function call order. For something like uint32_t a; uint8_t b; std::tie(a, b) = read_all<uint32_t, uint8_t>(); the first will be called read

decltype for overloaded member function [duplicate]

点点圈 提交于 2019-12-30 08:12:27
问题 This question already has an answer here : Disambiguate overloaded member function pointer being passed as template parameter (1 answer) Closed 3 years ago . I have this code: struct Foo { int print(int a, double b); int print(int a); void print(); void print(int a, int b, int c); void other(); }; I can call decltype(&Foo::other) but calling decltype(&Foo::print) end with error, which is clear to me. But how can I specify more "closely" which of the four print methods, I want to resolve to