template-meta-programming

Is it possible to match recursively integer template parameters in C++?

∥☆過路亽.° 提交于 2021-02-18 11:11:51
问题 I have the following problem. I define a N dimensional vector as so #include <vector> #include <utility> #include <string> template <int N, typename T> struct NVector{ typedef std::vector<typename NVector<N-1,T>::type> type; }; template <typename T> struct NVector<1,T> { typedef std::vector<T> type; }; I wish to write a higher order function Map that can transform the leaf elements of the nested vector no matter how deep and return a new nested vector of the same shape. I have tried template

Is it possible to match recursively integer template parameters in C++?

僤鯓⒐⒋嵵緔 提交于 2021-02-18 11:11:26
问题 I have the following problem. I define a N dimensional vector as so #include <vector> #include <utility> #include <string> template <int N, typename T> struct NVector{ typedef std::vector<typename NVector<N-1,T>::type> type; }; template <typename T> struct NVector<1,T> { typedef std::vector<T> type; }; I wish to write a higher order function Map that can transform the leaf elements of the nested vector no matter how deep and return a new nested vector of the same shape. I have tried template

How to detect the presence and type of a member variable given its name?

吃可爱长大的小学妹 提交于 2021-02-15 11:59:28
问题 I know how to write a class that can detect at compile time if a given class T has a member with a given name with given type Type, e.g. #include <type_traits> template <typename T, typename Type, bool = std::is_class<T>::value> struct has_member_foo { private: template <Type T::*> struct helper; template <typename U> static std::false_type test(...); template <typename U> static std::true_type test(helper<&U::foo> *); typedef decltype(test<T>(nullptr)) testresult; public: static const bool

C++ meta-function over templates

孤街浪徒 提交于 2021-02-11 18:19:42
问题 I have some templates like the ones below that I can use to define simple expressions e.g. Expr<constant,int,int,1,1> = 2 Expr<sub, Expr<constant,int,int,1,1>, Expr<constant,int,int,2,0>, 1, 1> = x - 2. I want to define a meta-function that takes an Expr and returns another Expr that is a modified version of the one passed as input. The output will be based on the template arguments of the input so I guess I have to define multiple function templates that specialize different inputs.

C++ meta-function over templates

亡梦爱人 提交于 2021-02-11 18:18:17
问题 I have some templates like the ones below that I can use to define simple expressions e.g. Expr<constant,int,int,1,1> = 2 Expr<sub, Expr<constant,int,int,1,1>, Expr<constant,int,int,2,0>, 1, 1> = x - 2. I want to define a meta-function that takes an Expr and returns another Expr that is a modified version of the one passed as input. The output will be based on the template arguments of the input so I guess I have to define multiple function templates that specialize different inputs.

c++ Use std::enable_if to conditionally add getters to a variadic variant template

可紊 提交于 2021-02-08 09:12:22
问题 I am trying to add specializations for the case where my variant has any of int , float , bool , and others as template arguments. My attempt so far is: #include <iostream> #include <variant> #include <string> #include <type_traits> template<typename... Types> struct variant : std::variant<Types...> { using std::variant<Types...>::variant; template<typename T> const T& get() const { return std::get<T>(*this); } #define VARGET(X) typename std::enable_if<(std::is_same<Types, X>::value || ... ),

Passing result of std::bind to std::function “overloads”

微笑、不失礼 提交于 2021-02-08 05:17:42
问题 I have problem similar to Passing different lambdas to function template in c++ but now with wrappers created by std::bind instead of lambdas. I have two overloads of method Add that take different forms of std::function : template<typename T> struct Value { T value; }; template <typename T> void Add(Value<T> &value, function<bool()> predicate) { } template <typename T> void Add(Value<T> &value, block_deduction<function<bool(const Value<T> &)>> predicate) { } This now works fine with lambdas

SFINAE to determine if a type has a potentially overloaded method

杀马特。学长 韩版系。学妹 提交于 2021-02-07 22:41:53
问题 I was looking for an SFINAE solution to check at compile time if a type has a method. My goal is to check if a type is a valid "duck type", but instead of a useless compile error, I want to use static_assert to provide an informative message. I found [this question], which provides a fairly good answer to my problem, except it fails when the type provides overload to the method: template<typename...> // parameter pack here using void_t = void; template<typename T, typename = void> struct has

Does virtual inheritance force a base class to be default constructible?

邮差的信 提交于 2021-02-07 13:17:11
问题 In the following code, the compiler is requesting the base class X to be default constructible . However, if I remove the virtual keyword from the inheritance of the class Node , the access to the member m_x becomes, of course, ambiguous, but the default constructor for class X is no longer required. What is the reason for that? #include <iostream> struct Apply { template< typename T > struct Node : virtual T // this line contains the virtual inheritance { template< typename ...Args> Node(

Does virtual inheritance force a base class to be default constructible?

让人想犯罪 __ 提交于 2021-02-07 13:16:33
问题 In the following code, the compiler is requesting the base class X to be default constructible . However, if I remove the virtual keyword from the inheritance of the class Node , the access to the member m_x becomes, of course, ambiguous, but the default constructor for class X is no longer required. What is the reason for that? #include <iostream> struct Apply { template< typename T > struct Node : virtual T // this line contains the virtual inheritance { template< typename ...Args> Node(