variadic-templates

Overloading the End of Recursion for a Variable Length Template Function

时光怂恿深爱的人放手 提交于 2019-12-10 14:58:13
问题 François Andrieux gave me a good workaround for this Visual Studio 2017 problem. I was trying to build on his answer like so: template<class T, size_t N> ostream& vector_insert_impl(ostream& lhs, const char*, const T& rhs) { return lhs << at(rhs, N); } template<class T, size_t N, size_t... I> ostream& vector_insert_impl(ostream& lhs, const char* delim, const T& rhs) { return vector_insert_impl<T, I...>(lhs << at(rhs, N) << delim, delim, rhs); } template <typename T, size_t... I> ostream&

Compiler bug, or non standard code? - Variadic template capture in lambda

[亡魂溺海] 提交于 2019-12-10 14:51:53
问题 I have the following C++11 code; template<typename... T> int g(T... t) { return 0; } template<class... Args> void f(Args... args) { auto lm = [&, args...] { return g(args...); }; lm(); } int main() { f(2, 5, 7); } I do believe that it's valid C++11, according to; Section 5.1.2.23 of the standard; A capture followed by an ellipsis is a pack expansion (14.5.3). [ Example: template<class... Args> void f(Args... args) { auto lm = [&, args...] { return g(args...); }; lm(); } — end example ]

Showing values of parameters packs in gdb [duplicate]

99封情书 提交于 2019-12-10 14:29:52
问题 This question already has an answer here : Inspect template parameter pack in gdb (1 answer) Closed 3 years ago . How can one see the values of a parameter pack in a variadic function in gdb? Sample code (VariadicDebug.cpp): template <typename... Ts> int Do(int a, Ts... ts) { // Add breakpoint here. a can be seen using 'print a' but how to show ts??? return a; } int main(int argc, char **argv) { return Do(0, "Hello world!", 88.9); } Compile with g++ --std=c++11 -O0 -g VariadicDebug.cpp And

C++ : Check if the template type is one of the variadic template types [duplicate]

风格不统一 提交于 2019-12-10 14:23:07
问题 This question already has answers here : Check if a type is passed in variadic template parameter pack (2 answers) Closed 4 years ago . Let's say we have function: template <typename Kind, typename... Kinds> void foo(){...}; What is the simplest way to check if the type 'Kind' is one of the types 'Kinds' in C++ (including C++1z)? 回答1: You could use the following type trait: template <typename...> struct is_one_of { static constexpr bool value = false; }; template <typename F, typename S,

Create a “do-nothing” `std::function` with any signature?

♀尐吖头ヾ 提交于 2019-12-10 14:16:20
问题 I would like to create a simple no-op std::function object with an arbitrary signature. To that end, I've created two functions: template <typename RESULT, typename... ArgsProto> std::function<RESULT(ArgsProto...)> GetFuncNoOp() { // The "default-initialize-and-return" lambda return [](ArgsProto...)->RESULT { return {}; }; } template <typename... ArgsProto> std::function<void(ArgsProto...)> GetFuncNoOp() { // The "do-nothing" lambda return [](ArgsProto...)->void {}; } Each of these works well

Create alias for a list of types and passing it as a template parameter

萝らか妹 提交于 2019-12-10 13:04:17
问题 I am using variadic templates to implement the visitor pattern: template<typename... Types> class Visitor; template<typename Type> class Visitor<Type> { public: virtual void visit(Type &visitable) = 0; }; template<typename Type, typename... Types> class Visitor<Type, Types...>: public Visitor<Types...> { public: using Visitor<Types...>::visit; virtual void visit(Type &visitable) = 0; }; template<typename... Types> class VisitableInterface { public: virtual void accept(Visitor<Types...>

Can default function arguments “fill in” for expanded parameter packs?

依然范特西╮ 提交于 2019-12-10 12:48:51
问题 The following code fails to compile : #include <iostream> template<typename F, typename ...Args> static auto wrap(F func, Args&&... args) { return func(std::forward<Args>(args)...); } void f1(int, char, double) { std::cout << "do nada1\n"; } void f2(int, char='a', double=0.) { std::cout << "do nada2\n"; } int main() { wrap(f1, 1, 'a', 2.); wrap(f2, 1, 'a'); } g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out main.cpp: In instantiation of 'auto wrap(F, Args&& ...) [with F = void(

C++ template partial specialization: Why cant I match the last type in variadic-template?

£可爱£侵袭症+ 提交于 2019-12-10 12:45:01
问题 I try to write a IsLast type traits to check if a given type is the last one in a std::tuple , but the code below does not compile. I know how to get around it but I am curious why the compiler does not like it.I guess there must be some rule on specialization of variadic-template that I am not aware of. The code is at: https://godbolt.org/g/nXdodx Error message: error: implicit instantiation of undefined template 'IsLast<std::tuple<std::__cxx11::basic_string<char>, int>, int>' There is also

g++ and clang++ different behaviour when `std::make_index_sequence` and `std::index_sequence` are used for a template parameter default type

六眼飞鱼酱① 提交于 2019-12-10 12:44:33
问题 Another "who's right between g++ and clang++?" question for C++ standard gurus. Given the following code #include <utility> template <std::size_t N, typename = std::make_index_sequence<N>> struct foo; template <std::size_t N, std::size_t ... Is> struct foo<N, std::index_sequence<Is...>> { }; template <std::size_t N> void bar (foo<N> const &) { } int main() { bar(foo<42u>{}); } I see that g++ compile where clang++ gives the following error tmp_003-14,gcc,clang.cpp:32:4: error: no matching

How to reverse an integer parameter pack?

非 Y 不嫁゛ 提交于 2019-12-10 12:06:19
问题 Sadly, I cannot use any of stl/std libraries from C++, because I am programming for a embedded Operating System which only has available gcc 4.4.4 with bare C++, so, no std::tuple , std::forward , std::apply or std::anything_else . To help understand meta generic generated code, I am presenting a minimal example code compiled with clang because it has a option to show us the generated template-meta-programming/metaprogramming code. This question is just for curiosity because instead of