template-meta-programming

Template partial specialization for multiple types overriding member function

只谈情不闲聊 提交于 2020-01-07 02:12:07
问题 I have class member functions defined as follows, providing a specification for one of them and letting the user provide their own specification for others: template <typename T> class Foo { // specialization provided for integral types template <typename Enable = T> typename std::enable_if<std::is_integral<Enable>::value, size_t>::type bar(const T& value); // provide your own specialization for other types template <typename Enable = T> typename std::enable_if<!std::is_integral<Enable>:

Template partial specialization for multiple types overriding member function

核能气质少年 提交于 2020-01-07 02:12:06
问题 I have class member functions defined as follows, providing a specification for one of them and letting the user provide their own specification for others: template <typename T> class Foo { // specialization provided for integral types template <typename Enable = T> typename std::enable_if<std::is_integral<Enable>::value, size_t>::type bar(const T& value); // provide your own specialization for other types template <typename Enable = T> typename std::enable_if<!std::is_integral<Enable>:

Template partial specialization for multiple types overriding member function

a 夏天 提交于 2020-01-07 02:12:02
问题 I have class member functions defined as follows, providing a specification for one of them and letting the user provide their own specification for others: template <typename T> class Foo { // specialization provided for integral types template <typename Enable = T> typename std::enable_if<std::is_integral<Enable>::value, size_t>::type bar(const T& value); // provide your own specialization for other types template <typename Enable = T> typename std::enable_if<!std::is_integral<Enable>:

metafunction overload C++ - enable_if

时间秒杀一切 提交于 2020-01-06 19:57:12
问题 Let's say I want to have 2 meta functions named multiplicate. Those metafunctions should operate on vector types. One metafunction should take as input two vectors and multiply one value by another Another one should take as input one vector and scalar and multiply all values in vector by scalar. The code I would like to have compilabe: template <int V1, int V2, int V3...> struct vector_c{ enum{ v1 = V1, v2 = V2, v3 = V3, /// }; }; template <typename Vector1, typename Vector2> struct

metafunction overload C++ - enable_if

大憨熊 提交于 2020-01-06 19:57:07
问题 Let's say I want to have 2 meta functions named multiplicate. Those metafunctions should operate on vector types. One metafunction should take as input two vectors and multiply one value by another Another one should take as input one vector and scalar and multiply all values in vector by scalar. The code I would like to have compilabe: template <int V1, int V2, int V3...> struct vector_c{ enum{ v1 = V1, v2 = V2, v3 = V3, /// }; }; template <typename Vector1, typename Vector2> struct

3 different / same ways of doing N-factorial compile time in C++

淺唱寂寞╮ 提交于 2020-01-06 04:45:08
问题 I am trying to play with template metaprogramming, constexpr and if constexpr and have come up with 3 different ways of doing a N-recursive / N-factorial operation. All three examples are some I've found here on SO or by searching on the net - and then modified it, so they do the same The first example is using template metaprogramming: example 1 template<int N> struct NGenerator { static const int result = N + NGenerator<N-1>::result; }; template<> struct NGenerator<0> { static const int

Disabling operators overloads if a given template is not specialized for the operator parameters type

蹲街弑〆低调 提交于 2020-01-06 01:18:10
问题 I'm writting a metaprogramming library which includes a set of compile-time arithmetic types and functions. For example: metafunctions.hpp template<typename T> struct function { using result = T; }; template<typename LHS , typename RHS> struct add_t; //Add metafunction template<typename LHS , typename RHS> using add = typename add_t<LHS,RHS>::result; fixed_point.hpp : #include "metafunctions.hpp" template<long long int BITS , unsigned int PRECISSION> struct fixed_point { operator float() {

optimise “binary_fold” algorithm and make it left (or right) associative

天大地大妈咪最大 提交于 2020-01-05 23:33:03
问题 Following my original question and considering some of the proposed solutions I came up with this for C++14: #include <algorithm> #include <exception> #include <iterator> #include <cstddef> template<class It, class Func> auto binary_fold(It begin, It end, Func op) -> decltype(op(*begin, *end)) { std::ptrdiff_t diff = end - begin; switch (diff) { case 0: throw std::out_of_range("binary fold on empty container"); case 1: return *begin; case 2: return op(*begin, *(begin + 1)); default: { //

optimise “binary_fold” algorithm and make it left (or right) associative

旧巷老猫 提交于 2020-01-05 23:32:10
问题 Following my original question and considering some of the proposed solutions I came up with this for C++14: #include <algorithm> #include <exception> #include <iterator> #include <cstddef> template<class It, class Func> auto binary_fold(It begin, It end, Func op) -> decltype(op(*begin, *end)) { std::ptrdiff_t diff = end - begin; switch (diff) { case 0: throw std::out_of_range("binary fold on empty container"); case 1: return *begin; case 2: return op(*begin, *(begin + 1)); default: { //

std::is_constructible doesn't give the correct result [duplicate]

混江龙づ霸主 提交于 2020-01-03 09:03:12
问题 This question already has answers here : Why does is_constructible claim something is constructible when it isn't? (2 answers) Closed 3 years ago . Originated from this CodeReview topic: #include <cstddef> #include <algorithm> #include <iostream> #include <type_traits> #include <utility> template <typename T> class aggregate_wrapper : public T { private: using base = T; public: using aggregate_type = T; template <typename... Ts> aggregate_wrapper(Ts&&... xs) : base{std::forward<Ts>(xs)...} {