template-meta-programming

different class implementations based on template parameter

这一生的挚爱 提交于 2020-02-27 06:24:08
问题 I suppose this is trivial for people who know templates... Suppose we want two different implementations of this template class, depending on the value of N: template <int N> class Foo { ... }; For example: template <int N> class Foo { ... // implementation for N <= 10 }; template <int N> class Foo { ... // implementation for N > 10 }; How can we do that in C++11? 回答1: Use an extra template parameter with a default value to distinguish cases: template <int N, bool b = N <= 10> class Foo;

c++ reversed integer sequence implementation

ぐ巨炮叔叔 提交于 2020-01-23 12:07:54
问题 Who knows how to implement C++ std::make_index_sequence reverse version. To get - make_index_sequence_reverse<int, 5> = <4,3,2,1,0> . Thank you! 回答1: IMHO, there is no reason for a index_sequence_reverse : std::index_sequence support sequences of indexes and are order neutral (or even without order). If you can use std::make_index_sequence , for a makeIndexSequenceReverse you can make something as follows #include <utility> #include <type_traits> template <std::size_t ... Is> constexpr auto

c++ reversed integer sequence implementation

点点圈 提交于 2020-01-23 12:07:40
问题 Who knows how to implement C++ std::make_index_sequence reverse version. To get - make_index_sequence_reverse<int, 5> = <4,3,2,1,0> . Thank you! 回答1: IMHO, there is no reason for a index_sequence_reverse : std::index_sequence support sequences of indexes and are order neutral (or even without order). If you can use std::make_index_sequence , for a makeIndexSequenceReverse you can make something as follows #include <utility> #include <type_traits> template <std::size_t ... Is> constexpr auto

Can I extend variant in C++?

风流意气都作罢 提交于 2020-01-21 04:34:04
问题 I'm not sure that this is possible, but say I have: using my_variant = std::variant<Class1, Class2, Class3>; Now at some point, I create a Class4 and would like to extend my_variant2 to include all of my_variant along with Class4 (in a general way, i.e. not just using another using... ) so that I can do something like create an array std::array<my_variant2, n> . Is this something that can be done? 回答1: godbolted #include <variant> template <typename T, typename... Args> struct concatenator;

Why do templates specialisations need to be inlined?

蹲街弑〆低调 提交于 2020-01-21 01:34:52
问题 I am referring to this answer: https://stackoverflow.com/a/4447057/930315 I ran into a similar issue as the OP of the cited question, having a function template<typename T> void func(T& val); and its specialization template<> void func<mytype>(mytype& val); resulted in a duplicate symbols linker error (the methods are implemented in a '.tpp' file that is included at the end of my header). adding inline to the specialised function resolved the issue. Why? 回答1: According to clause 3.2:4 in the

Why do templates specialisations need to be inlined?

守給你的承諾、 提交于 2020-01-21 01:34:01
问题 I am referring to this answer: https://stackoverflow.com/a/4447057/930315 I ran into a similar issue as the OP of the cited question, having a function template<typename T> void func(T& val); and its specialization template<> void func<mytype>(mytype& val); resulted in a duplicate symbols linker error (the methods are implemented in a '.tpp' file that is included at the end of my header). adding inline to the specialised function resolved the issue. Why? 回答1: According to clause 3.2:4 in the

Generating Structures dynamically at compile time

纵饮孤独 提交于 2020-01-20 08:30:52
问题 I have to generate a data structure that contains certain fields only under certain condition. This typically always translates to something like the following struct MyStruct { int alwaysHere; #ifdef WHATEVER bool mightBeHere; #endif char somethingElse; #if SOME_CONSTANT > SOME_VALUE uint8_t alywasHereButDifferentSize; #else uint16_t alywasHereButDifferentSize; #endif ... }; From my point of view this gets easily ugly to look at, and unreadable. Without even talking about the code that

Generating Structures dynamically at compile time

99封情书 提交于 2020-01-20 08:30:07
问题 I have to generate a data structure that contains certain fields only under certain condition. This typically always translates to something like the following struct MyStruct { int alwaysHere; #ifdef WHATEVER bool mightBeHere; #endif char somethingElse; #if SOME_CONSTANT > SOME_VALUE uint8_t alywasHereButDifferentSize; #else uint16_t alywasHereButDifferentSize; #endif ... }; From my point of view this gets easily ugly to look at, and unreadable. Without even talking about the code that

Passing variadic parameters in an already template-variadic function

我与影子孤独终老i 提交于 2020-01-16 06:55:51
问题 The title is bad but I couldn't come up with anything better. Feel free to change it. Here's a template multidimensional array class that I'm currently working on. I'm trying to optimise it as much as I can: #include <array> template <typename T, std::size_t... Dimensions> class multidimensional_array { public: using value_type = T; using size_type = std::size_t; private: template<typename = void> static constexpr size_type multiply(void) { return 1u; } template<std::size_t First, std::size_t

Compile time generated tables

江枫思渺然 提交于 2020-01-15 03:15:09
问题 Thanks to some trickery I'm able to generate a table at compile time, the values in the table are not very useful though. For example a table 5x5 looks like this: 1,2,3,4,5, 1,2,3,4,5, 1,2,3,4,5, 1,2,3,4,5, 1,2,3,4,5, Where the comma's are for clarity purpose. The code which create this table is the following: #include <iostream> using ll = long long; template<typename type,type...data> struct sequence { static type seq_data[sizeof...(data)]; static const ll size; type operator[](ll index){