template-meta-programming

How do I capture the results of a recursive function at compile-time?

你。 提交于 2019-12-10 18:23:38
问题 #include <iostream> template <typename T> struct node { T value; node const* prev; constexpr node(const T& value, node const* prev = nullptr) : value{value}, prev{prev} {} constexpr node push_front(const T& value) const { return node(value, this); } }; struct Something { node<int> n; constexpr Something(const int i) : n{node<int>(i)} {} constexpr Something(const node<int>& n) : n{n} {} }; constexpr void print(const Something& s) { bool first = true; for (const node<int>* i = &s.n; i !=

Implement is_destructible with Detected Idiom

馋奶兔 提交于 2019-12-10 18:14:13
问题 Here is my implementation of is_destructible_v: template<class T> struct is_unknown_bound_array : std::false_type {}; template<class T> struct is_unknown_bound_array<T[]> : std::true_type {}; template<typename T, typename U = std::remove_all_extents_t<T>> using has_dtor = decltype(std::declval<U&>().~U()); template<typename T> constexpr bool is_destructible_v = (std::experimental::is_detected_v<has_dtor, T> or std::is_reference_v<T>) and not is_unknown_bound_array<T>::value and not std::is

Check for function signature also for inherited functions

橙三吉。 提交于 2019-12-10 18:12:33
问题 I need to check, if a containers erase function returns the iterator. I'd normally check for the function signature via e.g. boost. But in case of a boost class (e.g. flat_set) erase is inherited and thus not found by the check. But I really need it. SFINAE to check for inherited member functions only shows a C++11 solution which I can't use yet. I tried something like this: template <typename T> class has_member_func_erase_it_constit { typedef typename T::iterator iterator; typedef typename

Metaprograming: Failure of Function Definition Defines a Separate Function

大憨熊 提交于 2019-12-10 18:07:39
问题 In this answer I define a template based on the type's is_arithmetic property: template<typename T> enable_if_t<is_arithmetic<T>::value, string> stringify(T t){ return to_string(t); } template<typename T> enable_if_t<!is_arithmetic<T>::value, string> stringify(T t){ return static_cast<ostringstream&>(ostringstream() << t).str(); } dyp suggests that rather than the is_arithmetic property of the type, that whether to_string is defined for the type be the template selection criteria. This is

How to “iterate” over a list of templates at compile time?

我们两清 提交于 2019-12-10 17:55:50
问题 This is the extraction of a follow-up question to this answer. Given the following "loop" technique #pragma once // loop.hpp #include <type_traits> #include <utility> template<std::size_t... indices, class LoopBody> void loop_impl(std::index_sequence<indices...>, LoopBody&& loop_body) { (// C++17's fold expression loop_body(std::integral_constant<std::size_t, indices>{}), ... ); } template<std::size_t N, class LoopBody> void loop(std::integral_constant<std::size_t, N>, LoopBody&& loop_body) {

How can I generate a tuple of N type T's?

守給你的承諾、 提交于 2019-12-10 16:03:07
问题 I want to be able to write generate_tuple_type<int, 3> which would internally have a type alias type which would be std::tuple<int, int, int> in this case. Some sample usage: int main() { using gen_tuple_t = generate_tuple_type<int, 3>::type; using hand_tuple_t = std::tuple<int, int, int>; static_assert( std::is_same<gen_tuple_t, hand_tuple_t>::value, "different types" ); } How can I accomplish this? 回答1: Fairly straightforward recursive formulation: template<typename T, unsigned N, typename.

error instantiating redBlackTree template

做~自己de王妃 提交于 2019-12-10 14:47:35
问题 I'm having trouble instantiating a RedBlackTree container with chars, but it works with ints: import std.stdio; import std.container; void main() { auto r1 = redBlackTree!(int)(); // works auto r2 = redBlackTree!(char)(); // error instantiating } I'm using DMD32 D Compiler v2.060. Any thoughts? Thanks. 回答1: you need to use a type that is comparable (i.e. can use the < operator or provide your own comparator as the second template parameter char (and wchar) is only useful for use in arrays due

How to “dereference a type” in C++03?

六眼飞鱼酱① 提交于 2019-12-10 14:34:52
问题 How do I get the "dereferenced type" of another type in C++03? Note that it can be other dereferenceable type like std::vector<int>::iterator . e.g. if I have template<typename T> struct MyPointer { T p; ??? operator *() { return *p; } }; How can I figure out what to replace the ??? with? ( No Boost ! I want to know how to figure it out myself.) 回答1: In the general case, you can't. For raw pointers, you can partially specialize as shown in other answers- custom smart pointers may have a

what is std::identity and how it is used?

荒凉一梦 提交于 2019-12-10 14:04:38
问题 I just want to know what is the purpose of std::identity? I could not find anything useful on web. I know how it is implemented : template <typename T> struct identity { T operator()(T x) const { return x; } }; why do we actually need this? 回答1: The struct you have in your code is the identity function T -> T where T is a template parameter. This function isn't useful on its own, but may be useful in other contexts where you need to pass a function parameter and the identify function is the

Custom compile error message when undefined subtype is accessed

时间秒杀一切 提交于 2019-12-10 13:57:41
问题 I have some types which have sub-types with the same name each: struct TypeA { typedef int subtype; }; struct TypeB { typedef float subtype; }; and also types which don't have this sub-type but which are used in the same context: struct TypeC { // (no subtype defined) }; How can I add a dummy sub-type which gives a custom compile error message? My (so far unsuccessful) attempt is: struct TypeC { struct subtype { static_assert(false, "Attempt to access the non-existent subtype of TypeC."); };