template-meta-programming

Get deepest class in CRTP inheritance chain

此生再无相见时 提交于 2019-12-06 01:26:11
I would like to know how to solve the following problem (C++17): suppose there are several template classes, inherited from each other in CRTP-like fashion (single inheritance only). For a given instantiated template base class, find the class that is furthest from it down the inheritance chain. I first thought that is should be pretty easy, but was not able to accomplish this. To simplify, suppose that every root and every intermediate class has using DerivedT = Derived in its public area. Example: template <class T> struct GetDeepest { using Type = ...; }; template <class T> struct A { using

Ambiguous metafunction or undefined type

半腔热情 提交于 2019-12-05 23:50:36
I am new to metafunctions. I want to write a function that replaces all matches of a certain type in a compound type with some other type. In example: replace<void *, void, int>::type should be int * , replace<void, void, int>::type should be int , etc. I basically failed with two different approaches so far: template < typename C, // Type to be searched typename X, // "Needle" that is searched for typename Y // Replacing type > struct replace { typedef C type; }; // If the type matches the search exactly, replace template < typename C, typename Y > struct replace<C, C, Y> { typedef Y type; };

Metafunction to compute x^n and return the integer limit without overflow if not possible?

吃可爱长大的小学妹 提交于 2019-12-05 21:26:05
问题 Consider the following code: template <std::intmax_t Base, std::intmax_t Exponent> struct integer_power_bounded { static_assert(Exponent >= 0, "Error in 'integer_power_bounded': 'Exponent >= 0' is false"); static constexpr std::intmax_t value = /* something */; }; template <std::intmax_t Base> struct integer_power_bounded<Base, 0> { static constexpr std::intmax_t value = 1; }; Instead of /* something */ , I would like to return std::numeric_limits<std::intmax_t>::min() or std::numeric_limits

Build template from arguments of functions?

橙三吉。 提交于 2019-12-05 18:14:07
template<class... Foos> // N = sizeof...(Foos) template<typename... Args> // M = sizeof...(Args) void split_and_call(Args&&... args) { // Using Python notation here... Foos[0](*args[:a]); // a = arity of Foos[0] Foos[1](*args[a:b]); // b-a = arity of Foos[1] ... Foos[N-1](*args[z:M]); // M-z = arity of Foos[N-1] } Assumptions: All types in Foos are callable All types in Foos are unambiguous Any type in Foos may have an arity of 0 Args is the concatenation of all of the argument types used by Foos Can this be done with just Foos and without Args ? I'm actually not sure how to do it even if I

Getting the type of a member

廉价感情. 提交于 2019-12-05 14:11:18
问题 NOTE : This question was originally asked way back in 2012. Before the decltype specifier was fully implemented by any major compilers. You should not be looking at this code unless you only have access to C++03. All major C++11 compliant compilers now support decltype . Is there an easy way to retrieve the type of a member? In C++03 struct Person { std::string name; int age; double salary; }; int main() { std::vector<Person> people; // get a vector of people. std::vector<GET_TYPE_OF(Person:

boost.proto + detect invalid terminal before building the expression tree

ⅰ亾dé卋堺 提交于 2019-12-05 12:11:19
I was playing with Boost.Proto, mostly for fun and to see if in future I could make some use of it in my own projects. That said, as probably most beginners of this library, i've played with a modified version of the 'lazy vector' example, but using transforms instead of contexts to perform the evaluation. The vector is defined as follows (ok, i know, 'vector' is not a good name for something defined at the global namespace scope...) template <std::size_t D, class T> class vector { T data_[D]; enum { dimension = D }; // Constructors, destructors... }; // expression wrapper template <class>

Detect if type is a “mapping”

别说谁变了你拦得住时间么 提交于 2019-12-05 11:59:30
I'd like to parse c++ containers into another object using their ::iterator member type. Containers who's iterator member type points to an object of a single type (vectors, queues, etc.) will turn into a list-like object, and containers who's iterator member type points to an std::pair will turn into a map-like object. I'm trying to write a member function to detect the latter kind of container, but it does not work. Here's what I have so far: #include <tuple> #include <iterator> #include <type_traits> template <typename T> struct is_pair : std::false_type { }; template <typename T, typename

Constexpr decltype

走远了吗. 提交于 2019-12-05 10:55:12
I recently asked a question here ( Detecting instance method constexpr with SFINAE ) where I tried to do some constexpr detection at compile time. Eventually, I figured out that one can exploit noexcept to do this: any constant expression is also noexcept . So I put together the following machinery: template <class T> constexpr int maybe_noexcept(T && t) { return 0; } ... constexpr bool b = noexcept(maybe_noexcept(int{})); This works and b is true as you'd expect, as zero-initializing an int is a constant expression. It also correctly yields zero when it should (if I change int to some other

C++ zero-cost abstraction for SoA/AoS memory layouts

人走茶凉 提交于 2019-12-05 09:13:05
Say I have a large code using Array of Structures (AoS) memory layout. I would like to build a zero-cost abstraction in C++ which allows me to switch between AoS and SoA with as little refactoring effort as possible. For instance take a class with access member functions struct Item{ auto& myDouble(){ return mDouble; } auto& myChar(){ return mChar; } auto& myString(){ return mString; } private: double mDouble; char mChar; std::string mString; }; which is used inside a container in a loop std::vector<Item> vec_(1000); for (auto& i : vec_) i.myDouble()=5.; I would like to change the first

How to count the number of CRTP subclasses of a template class?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 08:27:57
Does anyone know of a method to use CRTP to count the number of subclasses of an object? Suppose we had a setup similar to the following one: template <typename T> class Object { .... }; const unsigned int ObjectSubClassCount = ...; class Subobject : public Object<SubObject> { .... }; class Second : public Object<Second> { .... }; and so on, such that, using TMP, we might have a constant ( ObjectSubClassCount ) that represents the total number of subclasses? Does anyone know a way to do this? Edit: I am wanting to use the result as a template parameter later on, so I need it to be done with