template-meta-programming

Implementation of Vector in C++ [closed]

本秂侑毒 提交于 2019-11-29 20:31:31
I recently wrote an implementation of STL Vector as a programming exercise. The program compiles but I receive a strange error saying: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc I've never come up with this error before and am not sure what exactly should be changed within my implementation to make it function correctly. Can someone take a look through my code and see if anything jumps out at them as wrong in this specific case? Sorry I can't be more specific, I'm not sure where to look myself, thanks in advance. #include <iostream> #include <string>

C++ compile time function execution

随声附和 提交于 2019-11-29 17:39:57
问题 I have string tags in my code that are converted to numbers and used to search values in a tag-value structure. I have something like this: void foo() { type value = search("SomeTag"); } Where search is defined like this: type search(const char* tag) { return internal_search(toNumber(tag)); } Because all the time tag is constant at compile time I want to remove the call that converts the tag to a number from search function. I know it is possible to execute some simple functions at compile

Generating Structures dynamically at compile time

坚强是说给别人听的谎言 提交于 2019-11-29 16:06:08
I have to generate a data structure that contains certain fields only under certain condition. This typically always translates to something like the following struct MyStruct { int alwaysHere; #ifdef WHATEVER bool mightBeHere; #endif char somethingElse; #if SOME_CONSTANT > SOME_VALUE uint8_t alywasHereButDifferentSize; #else uint16_t alywasHereButDifferentSize; #endif ... }; From my point of view this gets easily ugly to look at, and unreadable. Without even talking about the code that handle those fields, usually under ifdefs too. I'm looking for an elegant way to achieve the same result

C++ compile-time predicate to test if a callable object of type F can be called with an argument of type T

冷暖自知 提交于 2019-11-29 11:34:54
问题 I would like to create a compile-type function that, given any callable object f (function, lambda expression, function object, ...) and a type T , evaluates to true, if f can be called with an argument of type T , and false if it cannot. Example: void f1(int) { ... } void f2(const std::string&) { ... } assert( is_callable_with<int>(f1)); assert(!is_callable_with<int>(f2)); I'm thinking that a clever use of the SFINAE rule could achieve this. Possibly somehow like this: template<typename T,

Use of void template argument in early detection idiom implementation

和自甴很熟 提交于 2019-11-29 10:35:27
In n4502 the authors describe an early implementation of the detect idiom that encapsulates the void_t trick. Here's its definition along with usage for defining a trait for is_assignable (really it's is_copy_assignable ) template<class...> using void_t = void; // primary template handles all types not supporting the operation: template< class, template<class> class, class = void_t< > > struct detect : std::false_type { }; // specialization recognizes/validates only types supporting the archetype: template< class T, template<class> class Op > struct detect< T, Op, void_t<Op<T>> > : std::true

C++ templates: how to determine if a type is suitable for subclassing

青春壹個敷衍的年華 提交于 2019-11-29 09:33:34
Let's say I have some templated class depending on type T . T could be almost anything: int , int* , pair <int, int> or struct lol ; it cannot be void , a reference or anything cv-qualified though. For some optimization I need to know if I can subclass T . So, I'd need some trait type is_subclassable , determined as a logical combination of basic traits or through some SFINAE tricks. In the original example, int and int* are not subclassable, while pair <int, int> and struct lol are. EDIT : As litb pointed out below, unions are also not subclassable and T can be a union type as well. How do I

Create a type list combination of types in C++

折月煮酒 提交于 2019-11-29 09:29:47
Im trying to create some tool to create a list of types based on combinations of other types. Lets say we have three types struct A{}; struct B{}; struct C{}; I want to get a list of tuples which has every possible combination of N types A,B or C. For a N=2 case, this would be std::tuple<A,A> std::tuple<A,B> std::tuple<A,C> std::tuple<B,A> std::tuple<B,B> std::tuple<B,C> std::tuple<C,A> std::tuple<C,B> std::tuple<C,C> The idea is to create a tuple which holds a container for all those types, so I can later store any of those types inside the container list. template <typename ...Combinations>

How to detect the presence of a static member function with certain signature?

為{幸葍}努か 提交于 2019-11-29 08:02:29
I found several questions & answers on SO dealing with detecting at compile time (via SFINAE) whether a given class has a member of certain name, type, or signature. However, I couldn't find one that also applies to static public member functions (when pointer-to-member tricks won't work). Any ideas? Following may help: ( https://ideone.com/nDlFUE ) #include <cstdint> #define DEFINE_HAS_SIGNATURE(traitsName, funcName, signature) \ template <typename U> \ class traitsName \ { \ private: \ template<typename T, T> struct helper; \ template<typename T> \ static std::uint8_t check(helper<signature,

transpose template function boolean arguments to runtime function arguments with template metaprogramming

一世执手 提交于 2019-11-29 07:20:37
I have a function which takes several boolean template arguments: template<bool par1, bool par2, bool par2> void function(int arg1, int arg2, int arg3); I want to generate automatically at compile-time (with whatever template magic, with C++11 if needed) a table (or something equivalent as in the funny structures of C++ metaprogramming) of the function pointers to all the combination of the values of the template parameters par* , so that I can construct a function which takes these template parameters as runtime arguments and forward to the right template instantiation: void runtime_function

Using Boost::odeint with Eigen::Matrix as state vector

為{幸葍}努か 提交于 2019-11-29 05:59:15
I'm trying to utilize the ODE integration capabilities of Boost using the Matrix class from Eigen 3 as my state vector, but I'm running into problems deep into Boost that I don't understand how to address. A minimal example of what I'm trying to do: #include <Eigen/Core> #include <boost/numeric/odeint/stepper/runge_kutta_dopri5.hpp> #include <iostream> using namespace Eigen; using namespace boost::numeric::odeint; template<size_t N> using vector = Matrix<double, N, 1>; typedef vector<3> state; int main() { state X0; X0 << 1., 2., 3.; state xout = X0; runge_kutta_dopri5<state> stepper; // If I