template-meta-programming

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

时光毁灭记忆、已成空白 提交于 2020-01-03 09:02:15
问题 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)...} {

c++03 : Variadic templates using boost

◇◆丶佛笑我妖孽 提交于 2020-01-03 04:45:12
问题 I'm trying to find a workaround to use variadic templates within c++03. What I would like to achieve is - within a templated class - instanciated a boost::tuple attribute, which would be composed of a vector for each single class template parameter. This is how it would look like using c++11 variadic templates: template<typename ...Parameters> class Foo { typedef std::tuple<std::vector<Parameters>...> Vectors_t; Vectors_t _vectorsTuple; } I would like to achieve the same using boost::tuple

Ensure template parameter is an enum class [duplicate]

我是研究僧i 提交于 2020-01-02 00:01:03
问题 This question already has an answer here : Is it possible to determine if a type is a scoped enumeration type? (1 answer) Closed 4 months ago . Is there a way to ensure a template parameter is an enum-class type? I know type_traits has std::is_enum , but I don't want it to match regular enums, just enum_classes. Example of the wanted effect: enum class EnumClass {}; enum Enum {}; class Class {}; template <typename T> void Example() { static_assert(/* T is EnumClass */, "`T` must be an enum

How to check whether a type is std::vector::iterator at compile time?

匆匆过客 提交于 2020-01-01 09:16:27
问题 I have a problem where I need to detect whether a given type is an instance of a known nested type such as std::vector::iterator at compile time. I'd like to create the type trait is_std_vector_iterator : #include <type_traits> #include <vector> template<typename T> struct is_std_vector_iterator : std::false_type {}; template<typename T, typename Allocator> struct is_std_vector_iterator<typename std::vector<T,Allocator>::iterator> : std::true_type {}; int main() { return 0; } But I receive

How can I get the depth of a multidimensional std::vector at compile time?

こ雲淡風輕ζ 提交于 2020-01-01 01:11:11
问题 I have a function that takes a multidimensional std::vector and requires the depth (or the number of dimensions) to be passed in as a template parameter. Instead of hardcoding this value I would like to write a constexpr function that will take the std::vector and return the depth as an unsigned integer value. For example: std::vector<std::vector<std::vector<int>>> v = { { { 0, 1}, { 2, 3 } }, { { 4, 5}, { 6, 7 } }, }; // Returns 3 size_t depth = GetDepth(v); This needs to be done at compile

Symbolic differentiation using expression templates in C++

大憨熊 提交于 2019-12-31 13:57:12
问题 How to implement symbolic differentiation using expression templates in C++ 回答1: In general you'd want a way to represent your symbols (i.e. the expressions templates that encode e.g. 3 * x * x + 42 ), and a meta-function that can compute a derivative. Hopefully you're familiar enough with metaprogramming in C++ to know what that means and entails but to give you an idea: // This should come from the expression templates template<typename Lhs, typename Rhs> struct plus_node; // Metafunction

more spirit madness - parser-types (rules vs int_parser<>) and meta-programming techniques

南楼画角 提交于 2019-12-31 10:57:18
问题 The question is in bold at the bottom, the problem is also summarized by the distillation code fragment towards the end. I am trying to unify my type system (the type system does to and from from type to string) into a single component(as defined by Lakos). I am using boost::array , boost::variant , and boost::mpl , in order to achieve this. I want to have the parser and generator rules for my types unified in a variant. there is a undefined type, a int4(see below) type and a int8 type. The

How to know if the argument that is passed to the function is a class, union or enum in c++?

江枫思渺然 提交于 2019-12-30 07:16:11
问题 I want to define an operator<< for all enums, to cout the value and print that it is an enum like this: code: enum AnyEnum{A,B,C}; AnyEnum enm = A; cout << enm <<endl; output: This is an enum which has a value equal to 0 I know a way of doing this with Boost library by using is_enum struct. But I don’t understand how it works. So that's why, in general, I am interested how to identify if the veriable is a class type, union type or an enum (in compile time). 回答1: Determining class types you

Variadic template aliases as template arguments

匆匆过客 提交于 2019-12-30 02:47:12
问题 First some code, then some context, then the question: template <typename T> using id = T; template <template <typename...> class F, typename... T> using apply1 = F <T...>; template <template <typename...> class F> struct apply2 { template <typename... T> using map = F <T...>; }; // ... cout << apply1 <id, int>() << endl; cout << apply2 <id>::map <int>() << endl; Both clang 3.3 and gcc 4.8.1 compile this without error, applying the identity metafunction to int , so both expressions evaluate

Why is initialization of a constant dependent type in a template parameter list disallowed by the standard?

。_饼干妹妹 提交于 2019-12-30 00:49:05
问题 In the answer to this post "(Partially) specializing a non-type template parameter of dependent type", it states: The type of a template parameter corresponding to a specialized non-type argument shall not be dependent on a parameter of the specialization. [ Example: template <class T, T t> struct C {}; template <class T> struct C<T, 1>; // error template< int X, int (*array_ptr)[X] > class A {}; int array[5]; template< int X > class A<X,&array> { }; // error —end example ] My question is why