variadic-templates

uniform initialization with variadic templates

半城伤御伤魂 提交于 2021-02-15 06:05:15
问题 I have a POD ChParam and it's a parameter in the variadic template function set . I'd like to pass to function arguments(constructor parameters) in curly braces p.set({ Param::D, 1000.f }, { Param::p, 2000.f }) . And thought that the constructor will be called implicitly and the ChParam objects will be created. But it's impossible, I should explicitly create an object a.set(ChParam{ Param::D, 1000.f }, ChParam{ Param::p, 2000.f }); is it possible somehow to use the variant p.set({ Param::D,

Create template pack from set of traits

若如初见. 提交于 2021-02-11 15:02:35
问题 Is it possible (and if so, how) to generate a template pack from a indexed set of type traits so they can be used to instantiate a variant or a tuple? #include <variant> template<int n> struct IntToType; template<> struct IntToType<0> { using type = int; static constexpr char const* name = "int"; // Other compile-time metadata }; template<> struct IntToType<1> { using type = double; static constexpr char const* name = "double"; // Other compile-time metadata }; using MyVariant = std::variant

Smart pointer toolkit using variadic CRTP

只愿长相守 提交于 2021-02-11 12:52:28
问题 I am about to design and implement a kind of smart pointer toolkit - a set of classes to define various types of smart pointers like unique_ptr, intrusive_ptr, shared_ptr, observing_ptr, tagged_ptr etc. Just to mention I am working in freestanding environment where I have no c++ library available. My intersion is to avoid code duplications, make it follow an elegant design principle. Let me describe my thoughts in there. Design Considerations: I wanna use variadic CRTP approach to mixin the

Access a type in a variadic template by index

柔情痞子 提交于 2021-02-10 11:47:38
问题 I would like to obtain a type in a variadic template by index. The index is specified as a template argument. I managed to find a 'hack' that works, but I believe that it is not in the spirit of variadic template programming. Besides, it uses extra memory. Here is the code with some explanations: template <typename... InputPortTypes> class PipelineReceiver { protected: // This tuple is used for storing types only // Hence, I would like to get rid of it, but I am not sure how. std::tuple< std:

Access a type in a variadic template by index

巧了我就是萌 提交于 2021-02-10 11:45:09
问题 I would like to obtain a type in a variadic template by index. The index is specified as a template argument. I managed to find a 'hack' that works, but I believe that it is not in the spirit of variadic template programming. Besides, it uses extra memory. Here is the code with some explanations: template <typename... InputPortTypes> class PipelineReceiver { protected: // This tuple is used for storing types only // Hence, I would like to get rid of it, but I am not sure how. std::tuple< std:

Iterating over C++ variadic template typelist [duplicate]

断了今生、忘了曾经 提交于 2021-02-10 08:05:34
问题 This question already has answers here : Call void function for each template type in a variadic templated function? (3 answers) Closed 2 months ago . Suppose I use a variadic template as typelist: template <typename ... Types> struct tl {}; using my_list = tl<MyTypeA, MyTypeB, MyTypeC>; Now I want to invoke a template function for each type, such as: myFunc<MyTypeA>(); myFunc<MyTypeB>(); How would I accomplish that ? 回答1: With c++17 you can use fold expressions. template <typename ... Types>

how to print many variables with there name and their corresponding value in c++?

有些话、适合烂在心里 提交于 2021-02-09 09:55:32
问题 #include <bits/stdc++.h> using namespace std; #define __deb(X...) (cout << "[" << #X << "]:" << X) template <typename... type> void debug(type &&... args) { ((__deb(args)), ...); } int main() { int a = 1, b = 3; debug(a,b); return 0; } I got output like [args]:1[args]:3 but I wanted output like [a]:1[b]:3 回答1: One way could be to quote all the macro arguments using #__VA_ARGS__ and parse that string in the C++ function. Example: #include <iostream> #include <sstream> #include <string>

std::bind with variadic template and auto return type

孤街醉人 提交于 2021-02-08 18:55:44
问题 Following the code in this question, I have a std::bind with a variadic template function. If I try to provide a function template with auto return, gcc rejects the program: #include <functional> template <typename... Args auto inv_impl(Args... a) { return (a + ...); } template <typename... Args> auto inv(Args... args) { auto bound = std::bind(&inv_impl<Args...>, args...); return bound; } int main() { auto b = inv(1, 2); } The compile error is: foo.cc: In instantiation of ‘auto inv(Args ...)

std::bind with variadic template and auto return type

☆樱花仙子☆ 提交于 2021-02-08 18:49:16
问题 Following the code in this question, I have a std::bind with a variadic template function. If I try to provide a function template with auto return, gcc rejects the program: #include <functional> template <typename... Args auto inv_impl(Args... a) { return (a + ...); } template <typename... Args> auto inv(Args... args) { auto bound = std::bind(&inv_impl<Args...>, args...); return bound; } int main() { auto b = inv(1, 2); } The compile error is: foo.cc: In instantiation of ‘auto inv(Args ...)

std::bind with variadic template and auto return type

扶醉桌前 提交于 2021-02-08 18:49:14
问题 Following the code in this question, I have a std::bind with a variadic template function. If I try to provide a function template with auto return, gcc rejects the program: #include <functional> template <typename... Args auto inv_impl(Args... a) { return (a + ...); } template <typename... Args> auto inv(Args... args) { auto bound = std::bind(&inv_impl<Args...>, args...); return bound; } int main() { auto b = inv(1, 2); } The compile error is: foo.cc: In instantiation of ‘auto inv(Args ...)