fold-expression

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

Checking if variadic template parameters are unique using fold expressions

为君一笑 提交于 2021-02-07 05:49:05
问题 Given a variadic template parameter pack, I want to check if all types given to it are unique using an inline constexpr bool and fold expressions. I trie something like this: template<class... T> inline static constexpr bool is_unique = (... && (!is_one_of<T, ...>)); Where is_one_of is a similar bool that works correctly. But this line doesn't compile regardless of what I put into is_one_of. Can this even be done using fold expressions, or do I need to use a regular struct for this purpose?

Using fold expressions to print all variadic arguments with newlines inbetween

落花浮王杯 提交于 2021-02-07 05:25:07
问题 The classic example for C++17 fold expressions is printing all arguments: template<typename ... Args> void print(Args ... args) { (cout << ... << args); } Example: print("Hello", 12, 234.3, complex<float>{12.3f, 32.8f}); Output: Hello12234.3(12.3,32.8) I'd like to add newlines to my output. However, I can't find a good way to do that, the best I've found so far: template<typename ... Args> void print(Args ... args) { (cout << ... << ((std::ostringstream{} << args << "\n").str())); } This

Is there a reason to use std::conjunction/std::disjunction instead of a fold expression over “&&”/“||”?

风流意气都作罢 提交于 2021-02-05 19:58:17
问题 Is there any specific cases you cannot correctly do with std::conjunction / std::disjunction and not using the more "fundamental" (i.e. language feature instead of library feature) fold expression over && / || ? Example: // func is enabled if all Ts... have the same type template<typename T, typename... Ts> std::enable_if_t<std::conjunction_v<std::is_same<T, Ts>...> > func(T, Ts...) { // TODO something to show } vs // func is enabled if all Ts... have the same type template<typename T,

Is there a reason to use std::conjunction/std::disjunction instead of a fold expression over “&&”/“||”?

一笑奈何 提交于 2021-02-05 19:56:48
问题 Is there any specific cases you cannot correctly do with std::conjunction / std::disjunction and not using the more "fundamental" (i.e. language feature instead of library feature) fold expression over && / || ? Example: // func is enabled if all Ts... have the same type template<typename T, typename... Ts> std::enable_if_t<std::conjunction_v<std::is_same<T, Ts>...> > func(T, Ts...) { // TODO something to show } vs // func is enabled if all Ts... have the same type template<typename T,

Unpacking variadic tuples in c++17

戏子无情 提交于 2021-02-04 17:27:50
问题 Is there anything better in c++17 (maybe C++2a) than the classic C++14 way to unpack variadic tuple with std::index_sequence? Anything better than this: template <typename ...I> class MultiIterator { public: MultiIterator(I const& ...i) : i(i...) {} MultiIterator& operator ++() { increment(std::index_sequence_for<I...>{}); return *this; } private: template <std::size_t ...C> void increment(std::index_sequence<C...>) { std::ignore = std::make_tuple(++std::get<C>(i)...); } std::tuple<I...> i; }

c++ macro expansion(__VA_ARGS__ item name and value)

泪湿孤枕 提交于 2021-01-29 09:01:02
问题 Is there any trick to achieve the function of the pseudo-code below? THX. template <typename... T1, typename... T2> void fake_fold((T1 t1, T2 t2)...) { (std::make_pair(t1, t2)), ...; // std::pair<std::string, T2>, T1 type is always sd::string } #define FAKE_MACRO(a, b, c) fake_fold(???) void main() { int var1; std::string var2; double var3; FAKE_MACRO(var1, var2, var3); /* macro expansion then pass to fake_fold: fake_fold("var1", var1, "var2", var2, "var3", var3); */ /* fold expression

Iterate over class inheritances in C++

元气小坏坏 提交于 2021-01-28 14:02:08
问题 Assume I have a some classes architecture (the number of the classes is growing up during the development time), that each class inherit from N classes with the same basic interface. What is the best way (if possible) to create a base function (in the base class OR in the derived class) that will iterate over the inheritances? Target: Avoid developers mistakes and make sure we won't forget to call all the base functions from all of the inheritances & make the code more clear to read and

Can you use a subexpression within fold expressions?

可紊 提交于 2020-01-14 07:26:08
问题 Is the following a legal fold expression? template <std::size_t N, std::size_t... Ix> bool in_range(std::index_sequence<Ix...>) { return ((Ix < N) && ...); } It compiles with clang but not gcc 回答1: Clang is doing the correct thing, the grammar from the Folding expressions Proposal is as follows: fold-expression: ( cast-expression fold-operator ... ) ( ... fold-operator cast-expression ) ( cast-expression fold-operator ... fold-operator cast-expression ) and contains the following wording that

How to make `short-circuit evaluation` also available in `fold expressions`?

我是研究僧i 提交于 2019-12-31 16:52:16
问题 #include <type_traits> #define FORWARD(arg)\ std::forward<decltype(arg)>(arg) template<typename... Args> constexpr bool AndL(Args&&... args) { return (... && FORWARD(args)); } template<typename... Args> constexpr bool AndR(Args&&... args) { return (FORWARD(args) && ...); } int main() { bool* pb = nullptr; false && (*pb = true); // ok at runtime. AndL(false, (*pb = true)); // error at runtime! AndR(false, (*pb = true)); // error at runtime! } The traditional && operator supports short-circuit