Questions tagged [template-meta-programming]

Template meta-programming is a meta-programming technique in which templates are used by a compiler to generate temporary source code, which is merged by the compiler with the rest of the source code and then compiled.

Filter by
Sorted by
Tagged with
0 votes
0 answers
38 views

Are the template'd lambdas allowed to be self-called?

coming back to C++ after a few years and toying around with c++20 I'm trying to get this template'd lambda to get self called at compile time but I'm not having any sucess: Consider the follow example:...
Nickolov's user avatar
2 votes
2 answers
86 views

How can I pass a templated fixed string to the overload of a constructor of another class

I'm having issues when trying to pass an already initialized string of chars which literally doesn't take the overload correctly: #include <cstddef> #include <ranges> #include <...
Nickolov's user avatar
1 vote
2 answers
274 views

Conditional compile-time warning in C++

I am implementing my own analogue of std::expected for C++17, and I am writing a method that takes a function and invokes it on the successful value of the expected. If the expected contains error, ...
Mikhail Tsaritsyn's user avatar
2 votes
1 answer
89 views

how to effectively turn rvalue reference to lvalue in perfect forwarding

I am implementing a simple generic functor class which can store a function as well as its arguments, and thus it can be called later. The function can be a free function or member function of a class,...
blackYellow's user avatar
0 votes
0 answers
75 views

Inferring (at compile-time) which members are read inside method

Consider this code: #include <type_traits> #include <tuple> // Random structs that follow this pattern struct X { int x; using Type = int; }; struct Y { float y; using Type =...
bigmazi's user avatar
-1 votes
0 answers
43 views

C++ - Template won't deduce the appropriate type when using an rvalue reference [duplicate]

I'm a bit confused about lvalue and rvalue bindings, I have the following code: void MyFunction( int& value ) { std::cout << "Lvalue reference: " << value << std::...
IRP_HANDLER's user avatar
1 vote
0 answers
73 views

What is the purpose of an empty struct with template arguments?

I'm reading some C++ CUDA code, which goes as follows: using Q_vec = typename Vec<scalar_t, VEC_SIZE>::Type; const scalar_t* q_ptr = q + seq_idx * q_stride + head_idx * HEAD_SIZE; ...
Foobar's user avatar
  • 7,664
1 vote
1 answer
65 views

Can I check if an expression is type-dependent?

Something tells me the answer is "no", but I figured I'd ask anyway. I want to check in a macro whether a specific expression is type-dependent. Here's a motivating example: #include <...
HolyBlackCat's user avatar
0 votes
4 answers
106 views

C++ Templates vs OOD woes - How can I get around implementing something like 'templated class member variables'?

Im trying to save a lot of code duplication using templates. Ideally I would like to do something along the lines of this (I know its garbage code): class Registries { public: template<...
InquiryOrEnquiry's user avatar
19 votes
6 answers
2k views

How can I constrain template parameter pack arguments to a "chain" sequence?

Suppose I have two classes: template <typename X, typename Y> class Functor {}; template <typename Start, typename End, typename ...Functors> class Template {}; Template has the ...
SKNB's user avatar
  • 383
3 votes
1 answer
68 views

Problems when specializing variadic template member function

I have the following state machine example that uses enums to specialize template methods with a variadic parameter pack. Why doesn't the second specialization get called when I pass Properties by ...
pic32cpp's user avatar
0 votes
0 answers
30 views

LNK2005 for non-templated function when explicitly instantiating templated functions of the class [duplicate]

I'm trying to follow the document: https://isocpp.org/wiki/faq/templates#separate-template-fn-defn-from-decl It worked well for a class template, but now I need to apply it for a regular class with ...
planet620's user avatar
0 votes
1 answer
78 views

using c++20 concepts to find nth element of parameter pack [duplicate]

I am looking for a non-recursive, modern method (which compiles, at least with gcc and clang) for finding the 𝑛th element of a variadic parameter pack. Indeed, it would seem such a solution, using c++...
phoko's user avatar
  • 1
1 vote
2 answers
119 views

How to have an alternate function signature in C++, due to unused parameter in a bool typed template function?

Disclaimer: this post is question, not a problem to solve with workarounds from what I am proposing. I aim to have a library providing several instances of my function increment, based on a unique ...
roli's user avatar
  • 11
0 votes
1 answer
56 views

C++ Interaction between constexpr and std::is_contant_evaluated

In a CppNow talk (Don't constexpr All the Things - David Sankel), it used the following function as an example: constexpr int f() { if constexpr (std::is_constant_evaluated()) { // slow ...
ABu's user avatar
  • 10.6k
-2 votes
1 answer
68 views

how to create static member function from class template typedef argument?

I'm trying to build a class template where one static methodneeds to have the typedef specified in the template argument. The goal is to specify a typedef like this typedef foobar = void __stdcall foo(...
Jon's user avatar
  • 1
2 votes
2 answers
135 views

How do I concatenate strings repeatedly at compile time?

I wrote the following code for using string in compile time: template<char... Chars> struct CnstString { template<char... Aped> using push_back = CnstString<Chars..., Aped...>...
Konvt's user avatar
  • 53
0 votes
1 answer
94 views

How to allow higher template recursion limit in MSVC?

I'm writing template metaprogramming code in C++ to generate a look-up table for embedded programming purposes (lack of FPU). I've been using MSVC for prototyping. When trying to generate the final ...
Julien BERNARD's user avatar
2 votes
1 answer
97 views

What caused the different behaviors in partial specialization of non-type template argument?

I'm trying to complete a template metaprogramming exercisetext. However, I meet different behaviors when I' trying to partially specialize an non-type template argument. I define a compile time vector....
LChao's user avatar
  • 31
0 votes
2 answers
74 views

Why is inheritance interfering with the compiler's ability to reason about the underlying type?

template<typename T, typename Tuple> struct has_type; template<typename T> struct has_type<T, std::tuple<>> : std::false_type {}; template<typename T, typename U, typename....
LunchMarble's user avatar
  • 5,081
0 votes
1 answer
33 views

Delayed evaluation with boost::mpl::eval_if

I have following code: #include <boost/mpl/apply.hpp> #include <boost/mpl/vector_c.hpp> #include <boost/mpl/transform.hpp> #include <boost/mpl/placeholders.hpp> #include <...
Xiong Zou's user avatar
0 votes
1 answer
67 views

C++ template type and variadic data of that type

I am trying to create a compile time matrix as template parameter template<typename TROW, TROW... TMATRIX> struct CTestMatrix { constexpr std::tuple<TROW...> GetData() { return ...
Anand Rathi's user avatar
1 vote
0 answers
66 views

Using array as a non type template parameter

I have a code in which I encode values of various types on the type level with an use of a helper data structure with core interface identical to std::integral_constant, but more generalized and with ...
iaks's user avatar
  • 33
0 votes
1 answer
44 views

How to provide information for library code client when there are only concepts?

Lets consider following situation, I'm writing library and instead of runtime polymorphism which comes with cost, but have clear and visible interface with all declarations, I choose generic ...
Kazz's user avatar
  • 1,062
2 votes
1 answer
65 views

How do I fix my `Any` class code to output data correctly?

I am trying to simulate std::any, my idea is to use the base class pointer to point to different types of template derived classes, to implement the function of storing different types of data, like ...
Konvt's user avatar
  • 53
0 votes
0 answers
27 views

Variadic Template Argument Function with Concepts Failed to be Invoked

I needed a function that can accept variable no. of arguments. Each one, a pair of the same type. I used this to define the concept. With the following definition of variadic template argument ...
Askr Askr's user avatar
3 votes
3 answers
122 views

How to define a C++ concept that checks if a given template type parameter is a sequential container of pair<T, T>

How can I define C++ concept that checks if a given template type parameter is a sequential container or a range, of pairs (e.g. vector<pair<string, string>>. Let's say the name of the ...
Askr Askr's user avatar
2 votes
1 answer
77 views

Function template detection through SFINAE

I want to write a function that dispatches a lambda either to an external library function template (if this function template exists within the library) or to execute the lambda directly. We can do ...
Hymir's user avatar
  • 853
2 votes
1 answer
106 views

Is it possible to pass a braced-init-list as a template argument?

I have this class template <typename ValueType, std::size_t Size> struct ArrayPrimitive { constexpr ArrayPrimitive(const ValueType (&array)[Size]) { std::copy(array, array + Size, ...
iaks's user avatar
  • 33
2 votes
1 answer
65 views

Sfinae Templated Implicit Conversion Operator Overloads

I am trying to template a few conversion operator functions to accept various arithmetic types, namely signed / unsigned integer / floating point types, so there are 4 different function template ...
Jake O'Neill's user avatar
0 votes
2 answers
70 views

Double fold expression to replace branching statement

I am trying to figure out how to use fold expressions ( or other metaprogramming techniques) to get rid off "if else" branches in the code snippet below. template<typename ... Ts> ...
user3786219's user avatar
0 votes
0 answers
77 views

Problem with using non type parameter class template

I am trying to create some type packing, value packing library for template meta programming usage. I have trouble with some template template non type parameter: #include <type_traits> ...
uxsu's user avatar
  • 1
1 vote
0 answers
50 views

Traversing a std::tuple error when using 'auto' and 'std::make_tuple'

In Generate a std::tuple of std::function from variadic templates I learned an elegant solution for traversing a std::tuple, but I am facing an error I can not find out why. In the code below: #...
canellas's user avatar
  • 563
2 votes
1 answer
93 views

Compile-Time Topological Sort Exceeds Recursion Depth in C++

Hello :) I'm implementing a compile-time topological sort algorithm using C++ template metaprogramming. The algorithm is designed to sort a graph of dependencies between different systems in a game ...
bibanac 's user avatar
1 vote
1 answer
66 views

How can I replace a type in a template at any level of nesting?

I have a type named the_bad. I want to replace that type with the_good using a template metafunction. I expect that template metafunction to be quite complex so let's call it the_ugly. My problem is ...
Guillaume Racicot's user avatar
7 votes
8 answers
465 views

How to declare a function with N arguments of type double?

I would like to have a class template capable of storing a functional object accepting exactly "N" double arguments. This pseudo-code uses a non-existing std::repeated_type function template ...
Adam Ryczkowski's user avatar
3 votes
1 answer
57 views

Detecting Instantiation/Calls with or without the constexpr Keyword in C++20 Constructors/Functions

I am currently trying to find a clean way to create a templated bytearray structure that can be initialized differently based on whether its constructor was instantiated with or without the constexpr ...
0xdeadbeef's user avatar
0 votes
1 answer
59 views

Get return type of template method in a template class

I have a following not compiling code: #include <iostream> #include <type_traits> template <typename T, typename... Args> struct foo {}; template <typename T> struct bar { ...
mouse_00's user avatar
  • 623
0 votes
1 answer
59 views

How to take a function passed as a template class and turn it into a constexpr callable (C++, template meta-programming)?

I have an external function that is passed as a class via a template parameter to another class. I want to call that external function in a constexpr member function and static assert its output value....
ajl123's user avatar
  • 1,202
0 votes
0 answers
45 views

Is there any way to deduce whether this function is a class static function according to the function name

I want to get the type of this function by function name, such as class static function, class non static member function, extern C function, etc. Can it be realized through Template Meta Programming ...
zsnjuts's user avatar
  • 21
1 vote
1 answer
61 views

How to zip own structure using template metaprogramming

I have my list structure and metafunctions for it. template<int ...> class MyList; template<int H, int ... T> class MyList<H, T ...> { public: static const int Head = H; ...
Deadpool's user avatar
1 vote
1 answer
108 views

What's the easiest way to generate a list of combinations in C++ using template meta-programming?

I found the SO post: What's the easiest way to generate a list of combinations in C++?, which tells us how to generate at runtime a list of all possible combinations of a true/false tuple of size ...
ajl123's user avatar
  • 1,202
1 vote
1 answer
97 views

Discluding Constructor Signature from Evaluation in C++23

C++ Standard: 20 or 23 test_property.cpp #include <string> #include <tuple> #include "property.h" class object { public: int index() const { return this->_idx; } ...
fauxanadu's user avatar
1 vote
3 answers
107 views

C++: Writing more generic code for multiple possible operations inside a loop

I have a big function, which for the MWE below is called modify_vec. In it, there are for loops where an operation is performed many times (e.g., for each element of a matrix). I would like the ...
Breno's user avatar
  • 768
3 votes
1 answer
111 views

Is There any Way to associate a Value to a Type?

So I've written a basic type tagging structure: struct TypeTag { inline static size_t counter = 0; template<typename T> static size_t get() { static size_t value = ...
bibanac 's user avatar
0 votes
1 answer
68 views

C++ Template recursion that recurses over indices of a boolean function arguments and evaluates function (template meta-programming)

I am interested in evaluating a boolean function, F that takes in N number of boolean arguments. N corresponds to the number of elements in a tuple, Levels. I want to perform a compile-time ...
ajl123's user avatar
  • 1,202
0 votes
0 answers
39 views

What does the __completion_signature concept in stdexec (senders/ receivers) do?

I'm looking at the reference implementation of P2300 (the senders and receivers proposal). I'm having trouble understanding some of the code: namespace __compl_sigs { template <same_as<...
Frederic Schönberger's user avatar
4 votes
2 answers
113 views

C++ std::tuple<Ts...> to std::tuple<std::vector<T>...>>

Given a variadic template, I would like to define an object that would be a tuple of vector of each type in the variadic template. Something like: template <typename... Ts> class C { std::...
canellas's user avatar
  • 563
2 votes
1 answer
136 views

Sorting Variadic Template Arguments Using Quick Sort

Introduction What I want to do is: Regardless of the order of template arguments, I want the objects which contain the same templates, to also have the same return value of Family::identifier(). What ...
bibanac 's user avatar
2 votes
1 answer
137 views

Check if a type is/convertible to a range in c++20

I want to write a function print(std::ranges::range auto&& range) which prints a range to std::out. This should be able to print a nested range recursively. I am new to this and still learning ...
Arnab Mahanti's user avatar

1
2 3 4 5
47