variadic-templates

What is a good alternative to this C++17 fold expression in C++14?

☆樱花仙子☆ 提交于 2019-12-19 21:47:19
问题 Here is a nice, succinct fold expression based lambda in C++17: #include <cstdint> using ::std::uint64_t; constexpr auto sumsquares = [](auto... n) { return ((n * n) + ...); }; // I want this to work. uint64_t foo(uint64_t x, uint64_t y, uint64_t z) { return sumsquares(x, y, z); } // And this too double bar(uint64_t x, double y) { return sumsquares(x, y); } I have this code I've written to do something similar in C++14, but it seems a lot more verbose and confusing than it should be. I'm

My std::hash for std::tuples… Any improvements? [closed]

旧时模样 提交于 2019-12-19 11:40:08
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago . Some may have noticed that std::hash does not support tuples. So I added an overload which just seems "nicer" than the solution I saw up till now. Anyone got ideas to further cut down this code? Please note that this is a compiler killer! The only one that could compile it

Variadic template operator<<

余生长醉 提交于 2019-12-19 11:28:13
问题 I'm trying to change some of my functions foo() into operator<<() , simply for the sake of getting some "half C/half C++" code to look more like C++. Happens, though, I've got stuck at the following transformation step : template <class... T> inline const size_t foo(const T&... data) { return sizeof...(T); } struct bar { template <class... T> inline const size_t operator<<(const T&... data) { return sizeof...(T); } }; int main(int argc, char *argv[]) { bar a; std::cout << ">>> length " << foo

Use std::tuple for template parameter list instead of list of types

爷,独闯天下 提交于 2019-12-19 09:02:31
问题 I'm trying to make a call to a templated function like this : typedef std::tuple<int, double, bool> InstrumentTuple; Cache cache; InstrumentTuple tuple = cache.get<InstrumentTuple>(); I know I could "simply" pass the types of the tuple. This is what I do know but it is quite cumbersome since I make a lot of calls to this function and since the tuples are quite long: InstrumentTuple tuple = c.get<int, double, bool>(); // syntax I'd like to avoid So I tried multiple implementations of the get

Inspect template parameter pack in gdb

冷暖自知 提交于 2019-12-19 07:23:40
问题 I'm trying to debug the following simple program: #include <iostream> template <class... Args> void printAll(Args&&... args) { using swallow = int[]; swallow{0, (std::cout << args, 0)... }; } int main() { printAll(1, "23", 4); } Compiled with gcc 4.9.2 using: g++ -std=c++11 -g -O0 foo.cxx And then debugging with gdb 7.9 using: gdb a.out (gdb) break foo.cxx:5 Breakpoint 1 at 0x400884: file foo.cxx, line 5. (gdb) run Starting program: /..[snip]../a.out Breakpoint 1, printAll<int, char const (&)

Wrapping each type in a variadic template in a templated class

本秂侑毒 提交于 2019-12-19 05:53:46
问题 Given a variadic template Types... , I would like to store A<> for each of the types in the pack. This could be done in a tuple of A<> 's, but I'd need to programmatically derive the type of said tuple. Is such a thing even possible in c++11/14/17? template <class T> class A { }; template <class... Types> class B { // A tuple of A<>'s for each type in Types... std::tuple<A<Type1>, A<Type2>, ...> data; }; 回答1: Simply with: template <class... Types> class B { std::tuple<A<Types>...> data; }; 来源

implementing a variadic zip function with const-correctness

北城以北 提交于 2019-12-19 03:44:06
问题 I'm trying to implement a zip function. zip 's parameters are each wrapped<Ti> , where Ti varies from parameter to parameter. zip takes these wrapped<Ti> s and produces a wrapped<tuple<T1&,T2&,...TN&>> , or in other words a wrapped tuple of references to its parameters. The references should preserve const -ness. Here's my first stab at zip with one parameter, which doesn't work in general: #include <utility> #include <tuple> // implement forward_as_tuple as it is missing on my system

Which compiler, if any has a bug in parameter pack expansion?

泪湿孤枕 提交于 2019-12-18 19:40:23
问题 When experimenting with convenient ways to access tuples as containers, I wrote a test program. on clang (3.9.1, and apple clang) it compiles as expected, producing the expected output: 1.1 foo 2 on gcc (5.4, 6.3), it fails to compile: <source>: In lambda function: <source>:14:61: error: parameter packs not expanded with '...': +[](F& f, Tuple& tuple) { f(std::get<Is>(tuple)); }... ^ <source>:14:61: note: 'Is' <source>: In function 'decltype(auto) notstd::make_callers_impl(std::index_sequence

variadic templates - ambiguous call

笑着哭i 提交于 2019-12-18 19:27:09
问题 The following code compiles in both gcc 4.7.2 and MSVC-11.0: template <typename T> void foo(T bar) {} template <typename T, typename... Args> void foo(T bar, Args... args) {} int main() { foo(0); // OK } Why? I think that it's must be ambiguous call: ISO/IEC 14882:2011 14.5.6.2 Partial ordering of function templates [temp.func.order] 5 ... [ Example: template<class T, class... U> void f(T, U...); // #1 template<class T > void f(T); // #2 template<class T, class... U> void g(T*, U...); // #3

C++ - How to introduce overload set from variadic number of bases.

北城余情 提交于 2019-12-18 18:55:25
问题 The derived class hides the name of an overload set from the base class if the derived class has the same name defined, but we can always introduce that overload set back with using-declaration: template <class BASE> class A : public BASE { public: using BASE::some_method; void some_method(); } But what if I introduce all overload sets from variadic base classes? Would I be able to write something like this? template <class... BASES> class A : public BASES... { public: using BASES::some