Questions tagged [template-meta-programming]

5

votes
0

answer
35

Views

Template metafunctions to generate pybind11 bindings

I am trying to create python bindings for some C++ functions using pybind11. The functions are templated but in the python bindings, I need one of the template parameter to be a function argument. Currently the way I have it is a lot of repetition for each template parameter. enum MyEnum {E1, E2, E3...
Bilentor
1

votes
2

answer
57

Views

Definition and Initialization of a tuple whose components are of the same templated class, but with different specialisations

I am new to c++ metaprogramming. I tried to look at other answers, but I was not able to find one that could suit my problem. Or simply I was not able to apply it to my case. Here I will post a simplified version of the code, to highlight the main features which I would like to obtain. What I would...
Garo
1

votes
2

answer
61

Views

Selecting Variadic Type with Runtime Index using Lookup Table

Consider a variadic pack of types. It's possible to select and use one of those types indexed by a runtime value using a recursive indexing function and a lambda with an auto parameter as follows: #include #include #include template struct identity { using type = T; }; template struct variadic {...
rtek
1

votes
2

answer
310

Views

how to write a C++ class member function wrapper?

I want to wrap some class member functions and do some preparing and cleaning up work around them. I try to copy some other thread pool code but get some error I cannot deal out. How to do it correctly? #include #include using namespace std; class A { public: void connect() {}; void close() {}; te...
Liu Weibo
1

votes
1

answer
138

Views

C++ template meta progr. conditional memory allocation

I am trying to implement a simple Linear-Algebra library (for learning purposes). Problem => I would like the Matrix template class to either declare a fixed-size array at compile time, or use dynamic memory allocation if the number of Rows and Columns are not yet known. How can i effectively / cor...
Leo G.
1

votes
2

answer
348

Views

Does example of Erwin Unruh work today?

Here is the example from Erwin Unruh famous for using C++ template meta-programming to produce the prime number in the compiler error message as follow. // Erwin Unruh, untitled program, // ANSI X3J16-94-0075/ISO WG21-462, 1994. template struct D { D(void *); operator int(); }; template struct is...
ar2015
1

votes
0

answer
74

Views

Is a is_variadic type trait possible in C++17?

Is it possible, in C++17, to design a type trait which would detect whether a callable is variadic (and therefore can take an arbitrarily long number of parameters) or not? template struct is_variadic; I currently do not see how to do that, but I cannot convince myself that it's not doable. So if i...
Vincent
1

votes
0

answer
1.4k

Views

How to generate C code from Python?

I am writing an embedded algorithm. Currently I have lookup tables that are hard-coded: static const int32_t sine[65536] = { ... } The algorithm should be portable on different platforms but, the lookup table length changes depending on the configuration and on the destination platform. My current...
nowox
1

votes
1

answer
73

Views

boost MPL::vector not recognized and Meta State Machine (MSM) transition table not defined

I'm trying to learn boost's meta state machine (MSM) library. Following the simple state machine example in http://redboltz.wikidot.com/simple-state-machine, I face some problems: I have a problem in defining the state machine's 'transition table' using boost::mpl::vector. // Transition table struc...
qwa
5

votes
6

answer
291

Views

How does changing a template argument from a type to a non-type make SFINAE work?

From the cppreference.com article on std::enable_if, Notes A common mistake is to declare two function templates that differ only in their default template arguments. This is illegal because default template arguments are not part of function template's signature, and declaring two different functio...
David Hammen
1

votes
0

answer
54

Views

Retrieving std::tuple object given its member value references

Let's say that I have an std::tuple: std::tuple< int, float, bool > my_tuple; and a function: void my_function( int& i, float& f, bool& b); Is it possible to retrieve the my_tuple object from within my_function if I can guarantee that its arguments i, f, b are members of my_tuple? For example - is i...
tsuki
1

votes
2

answer
60

Views

How to transform this list of types with brigand?

I have the following list of types: using ComponentList = brigand::list How can I transform the previous list into a new list that would look like this: using ComponentHandleList = brigand::list; I basically want to generate a new list that has wrappers for every element in the previous list. I tri...
Bryan Triana
1

votes
2

answer
148

Views

Invalid explicitly-specified argument for template parameter which is constexpr

I have a static_loop construct like this template void static_loop(F&& f) { static_assert(n = 8) f(std::integral_constant()); if constexpr (n >= 7) f(std::integral_constant()); if constexpr (n >= 6) f(std::integral_constant()); if constexpr (n >= 5) f(std::integral_constant()); if constexpr (n >= 4...
Amos
1

votes
1

answer
111

Views

Lazy evaluation wrapper class in C++?

What is a good way to implement a wrapper class for CRTP lazy evaluation? Specifically, I am looking to work with delayed or lazy evaluation similar to the answers to this question and am interested in wrapping the CRTP derived classes in a generic class so that I can do something like the following...
Adam
1

votes
1

answer
88

Views

How to get the signature of a callable type?

I want to build a nice, modern interface for building a computation tree, something like this: auto [F, G] = calcs.emplace( [](int a, int b){ return a + b; }, [](){ return 4; } ); I got the inspiration from taskflow, but here we might add arguments and return types as well, and here comes the probl...
Müller András
1

votes
0

answer
66

Views

CUDA: recognize existence of function and distinguish __host__ and __device__

I'm trying to detect the existence of operators for some types. But I cannot distinguish between __host__ and __device__ functions. I already have a simple implementation for identifying the operator in normal C++ but it can't make the distinction between the __host__ and __device__. Below, there is...
mbtg
0

votes
0

answer
3

Views

Use std:array as template parameter of template TEMPL1 and recursively its values for another template TEMPL2 used inside TEMPL1

I would like to insert an std::array, known at compile-time, as a template parameter. In this way, instead of iterating among 0,...,N, I could iterate from array[0] to array[N] and use such values for other templates which needs, as a parameter, array[I], I=0,...,N. Last thing I tried so far is some...
Garo
1

votes
0

answer
14

Views

Cross platform variadic macro with 0..n parameters

For a parameter pack I need a macro which can take any number of parameters (types actually), which works cross platform. This code works nicely with GCC, LLVM and MSVC (after the preprocessor had been reworked to support the ## sequence (see Behavior 4 [comma elision in variadic macros] ): class A...
Mike Lischke
1

votes
1

answer
128

Views

Using CRTP technique to ensure unique template arguments

From boost::units: struct my_base_dimension1 : units::base_dimension { }; // ok struct my_base_dimension2 : units::base_dimension { }; // ok struct my_base_dimension3 : units::base_dimension { }; // error I'm trying to understand how the code ensures that template arguments are unique. I don't u...
Arlen
1

votes
1

answer
497

Views

using mpl::fold with a placeholder and my own struct mishap

I have the following primary template: template struct Sol; and I specialize it for some pos values like so: template struct Sol { static const mpl::vector_c jumps; static const size_t value = mpl::fold::type::value; } but I get that Sol expected size_t and got mpl_::_1. I know in this case I could...
Palace Chan
11

votes
2

answer
524

Views

c++ generic compile-time for loop

In some contexts, it could be useful/necessary to have a for loop evaluated/unrolled at compile time. For example, to iterate over the elements of a tuple, one needs to use std::get, which depends on a template int parameter I, hence it has to be evaluated at compile time. Using compile recursion on...
francesco
1

votes
1

answer
1.7k

Views

c++ template metaprogramming: how to create and iterate over list of types that are “typedefs” in template class.

My code is a factory that creates objects based on types from template parameters. I want to extend this to a 'list of' types. This is what I have: Algo1 defines a type indata. FASSubscriberFactory::Create() returns a pointer to FASSubscriber . See here: struct Algo1 { typedef DataType1 indata;...
jaybny
1

votes
1

answer
351

Views

Boost signals - type passed as parameter

I found this C++ code that uses boost signals and I'm trying to understand it. // A boost::signal wrapper structure template struct SignalBase : public boost::noncopyable { typedef boost::function_traits< Signature > SignatureTraits; typedef boost::signal SignalType; typed...
Jacob Krieg
1

votes
1

answer
238

Views

Implementing the A(:,k)=b; Matlab-like syntax in a C++ matrix library

I have developed an expression templates-based C++ matrix class of my own. I have overloaded the () operator so that I can read or write element matrices as, for example, cout
JackOLantern
1

votes
1

answer
87

Views

What's the difference between C++ Concepts and C++ Template Constraints?

As the title says, there's something called Concepts Lite (Template Constraints) and Concept Design for the STL. Can anybody explain some fundamental differences between these?
ScarletAmaranth
1

votes
1

answer
127

Views

How am I supposed to write a metafunction that works like the ones in MPL?

I seem to be missing something when trying to write a meta-function that calls into MPL code. The follow code fails to compile with the following error on inst2, but works fine on inst1: error C2903: 'apply' : symbol is neither a class template nor a function template using namespace boost::mpl; te...
Mike
1

votes
1

answer
307

Views

Disabling operators overloads if a given template is not specialized for the operator parameters type

I'm writting a metaprogramming library which includes a set of compile-time arithmetic types and functions. For example: metafunctions.hpp template struct function { using result = T; }; template struct add_t; //Add metafunction template using add = typename add_t::result; fixed_point.hpp: #include...
Manu343726
1

votes
1

answer
706

Views

Getting tuple element by type (pre C++0x)

I have a templated class that holds a tuple and want to be able to retrieve the elements by type at compile time. To simplify things the container class is restricted to a maximum of three entries: template< class U = null_type, class V = null_type, class W = null_type > class TupleContainer { publi...
boris
1

votes
1

answer
49

Views

Template specialisation where parameter is non-type parameterized template for metaprogramming

I have the following code: template class VariableString{ public: enum{ fieldSize = -1000 }; .... }; template class field_trait{ public: enum{ fieldSize = FieldSize }; .... }; template class field_trait{ public: enum{ fieldSize = -1000 }; .... }; static_assert(field_trait::fieldSize == -1000, 'Varia...
Ben Jaguar Marshall
1

votes
1

answer
827

Views

Not compile time constant

I have: static const std::array dataSizes = { std::make_pair(ServerD::ContentType, 1) , std::make_pair(ServerD::RemoteAddress, 2) , std::make_pair(ServerD::RemoteUser, 3) , std::make_pair(ServerD::Url, 4) }; template struct SizeFinder { static const unsigned int SizeFor(ServerD data) { return (data...
Gmt
1

votes
1

answer
127

Views

How to overload function switching by argument functor arguments

I want to write function overload switched by arguments functor(function pointer, function object or lambda)'s arguments. So, functor arguments is (int) or (int, int). My bad implementation is hare. void function_int(int){ return; }; void function_int_int(int, int){ return; } template boolean some_...
user3188838
1

votes
1

answer
57

Views

Which solution to parse a text with Regex and save to a Database the capturing group?

I've to face the following challenge which I would like to automatize using Regex: I have to match this two mappings: GetSQLParameter('@switch', SqlDbType.TinyInt, 1), _ GetSQLParameter('@IDUtente', SqlDbType.Int), _ sqlClient.AddParameterWithValue('switch', SqlDbType.TinyInt, user.IDUser); sqlClien...
M.F05051985
1

votes
1

answer
276

Views

Want to understand this paper that uses templates

I found this paper searching for links about C++ template metaprogramming. I'm new to both security and templates in C++ and am hoping someone can point me in the right direction. I'm asking for no other reason that it piqued my interest, and I'd like to learn more in general. Here is the paper:...
user2079828
1

votes
2

answer
160

Views

Trailing return type issue when using restricted function

I am writing a library using C++AMP for the internals, and I have discovered the following does not work and I am curious as to why (as it works if I remove restrict(amp) from the functor): template using SumType = decltype( std::declval() + std::declval() ); template auto TestFunc(T t, Func f) ->...
Thomas Russell
1

votes
1

answer
94

Views

Find untagged template options/parameters/args by position

In short: I want to extract various options from variadic template parameters, but not only by tag but by index for those parameters, that have no known tag. I like the approach in boost (e.g. heap or lockfree policies), but want to make it compatible with STL containers - the allocator parameter. P...
firda
1

votes
1

answer
321

Views

Alternatives for std::enable_if and explicit overloading for template template parameters

Consider the following setup: template< typename Held > class Node{ //... }; template< typename Held > class vNode{ //... }; template class Branch{ //... }; template class Graph; // undefined template< typename node_t > class Graph< node_t >{ //specialization for an ending node //... }; templ...
tsuki
1

votes
1

answer
650

Views

compile time loop over templated type

I have been searching through SO, and other forums looking for a way to determine the parameters and return type of a lambda, and then act on those parameters in order to do a type lookup on a repo of objects that have already been instantiated. The point is to create a way to do dependency injectio...
pje
1

votes
1

answer
55

Views

C++: How to test compile errors to check constraints

I wrote constraints on C++ type using templates. Now I want to test them in my test suite, in order to notice when the behavior breaks. Think about Field class like this. template class Field { template Field SubString(typename std::enable_if= Offset + Count)>::type* = 0) const { return Field(data_...
12651654123zzz
1

votes
1

answer
169

Views

Creating a type list of all constructors of a class

I'm trying to implement a lazy initialisation class, lazy, that stores T's construction arguments and uses them to construct a T the first time it's needed. Currently I'm capturing the arguments in a function object (stored in a boost::function) that constructs the object in a specified location whe...
Yaron Tausky
1

votes
1

answer
433

Views

Is double/nested variadic template expansion allowed in modern C++? [duplicate]

This question already has an answer here: Where and why do I have to put the “template” and “typename” keywords? 6 answers I've been trying to use C++ metaprogramming to build constructs such as f(g(args...), g(args...), ... g(args...)) given callables f and g, integer n and variadic argume...
Antoine

View additional questions