template-meta-programming

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

淺唱寂寞╮ 提交于 2019-12-07 10:40:54
问题 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]

Detect if type is a “mapping”

那年仲夏 提交于 2019-12-07 09:25:11
问题 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

Check if a given type has a inner template rebind

假如想象 提交于 2019-12-07 07:51:29
I need a trait which will check if a given type has an inner template named rebind which takes a single template type parameter. Something like: template <typename X> struct has_rebind { static const bool value = /* ??? */; }; This is similar to what BOOST_TTI_HAS_TEMPLATE does, but I want to implement the trait without the use of Boost TTI, and I do not know how that macro is implemented. Barry C++11 and up gives us a wealth of alternatives with which to write arbitrarily complex type traits in fairly simple fashion. One such is void_t : template <typename... > using void_t = void; All you do

What is the recommended way to simulate concepts and constraints? [closed]

感情迁移 提交于 2019-12-07 06:35:17
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 12 months ago . Before the introduction of concepts and constraints, there are several ways to simulate this compile-time check. Take a " order() " function for example: (how to implement LessThanComparable without concepts or constraints is another story) Use static_assert template

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

徘徊边缘 提交于 2019-12-07 06:30:36
问题 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:

Constexpr decltype

喜欢而已 提交于 2019-12-07 05:09:35
问题 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

How to make static_assert play nice with SFINAE

偶尔善良 提交于 2019-12-07 05:00:33
问题 Update I posted a working rough draft of rebind as an answer to the question. Though I didn't have much luck finding a generic way to keep static_assert s from breaking metafunctions. Basically I want to check if a templated type T<U, Args...> can be constructed from some other type T<V, Args...> . Where T and Args... is the same in both types. The problem is, T<> might have a static_assert in it that totally breaks my metafunction. Below is a rough summary of what I'm trying to do. template

C++14 Metaprogramming: Automagically build a list of types at compile / init time

不问归期 提交于 2019-12-07 04:11:40
问题 Using C++14 and some combination of the Curiously Recurring Template Pattern (CRTP) and possibly Boost.Hana (or boost::mpl if you wish), can I build a list of types at compile time (or static initialization time) without an explicit declaration? As an example, I have something like this (see it on Coliru ): #include <iostream> #include <boost/hana/tuple.hpp> #include <boost/hana/for_each.hpp> namespace { struct D1 { static constexpr auto val = 10; }; struct D2 { static constexpr auto val = 20

How to detect whether a type is a lambda expression at compile time?

半世苍凉 提交于 2019-12-07 03:05:37
问题 Suppose I have a type my_struct enclosing a member variable, f , which is a function. It's possible for f to be a c++11 lambda function. Since it is illegal to assign to lambda objects, I'd like to implement my_struct 's assignment operator in such a way that when f is a lambda, it is not assigned. Is it possible to build a type trait is_lambda which can inspect a type for lambda-ness? In code: #include <type_traits> template<typename Function> struct is_lambda { // what goes here? };

constexpr function not calculate value in compile time

橙三吉。 提交于 2019-12-07 02:59:30
问题 I want to compare meta programming and use of constexpr in c++0x. then I write a fib function in both model. when I use meta programming model, answer print out very fast because it calculated in compile time. but when I use constexpr funcion it calculate value in run time, not in compile time. I using g++( gcc ) 4.8 .can any body help me? #include <iostream> using namespace std; #define NUM 42 template <unsigned int N> struct Fibonacci { enum { value = Fibonacci<N - 1>::value + Fibonacci<N -