444 questions
4
votes
1
answer
107
views
Is the type of a non-type template parameter part of the function signature?
Regardless of whether this is a good idea or not, is it allowed in C++ to have two template functions, that differ only in the type of a non-type template parameter.
I am asking this question, because ...
1
vote
1
answer
60
views
Is there a way to evaluate a bundle of constexpr functions at compile time in C++?
I am new to C++ templates and I'm trying to make a constexpr template function that takes a paramater pack of other functions of return type bool (and in this example input type int) and checks if a ...
0
votes
3
answers
137
views
Loop over pairs from 2 constexpr arrays to instantiate and run template function
C++ version for project is 20.
Suppose we have some service class template:
template<int someSegmentSize>
class SomeAbstractFileService {
public:
void init() {
std::cout << &...
0
votes
1
answer
141
views
How to expand function template parameters package in std::function as the function's argument?
This question is an extension of another question from a decade ago:
#include<functional>
template <typename ReturnType, typename... ArgumentTypes>
struct Caller
{
static void Call(std:...
3
votes
1
answer
174
views
Why template function which is constrained to work with integrals, is working fine with char as well? [duplicate]
I have a simple cpp template function as follows.
#include <iostream>
#include <concepts>
template <typename T> requires std::integral<T>
T add(T a, T b)
{
std::cout <&...
1
vote
3
answers
138
views
How do I pass a parameter pack to a std:tuple?
This is a simple question, yet the Internet offers no answer or examples to this one:
what is the correct means of passing a parameter pack to a std::tuple ?
I have tried the following, only to find I ...
3
votes
1
answer
57
views
How to use attribute (not its value) of a struct/class as template/function parameter?
To be short, I want to achieve something like "templatize" the operation of "getting an attribute of a object" as a generalized template function, like the function foo below.
...
8
votes
1
answer
165
views
Weird behaviour with 'if constexpr' and templates in MSVC
The code below behaves incorrectly.
When f<0> or f<5> is called, it prints as if k = true, but the if statement behaves as if k = false. When f<1> is called, it does the opposite.
...
0
votes
0
answers
85
views
C++ Function Parameters Guiding CTAD
I am trying to do something involving CTAD within function calls and it's best to first explain the desired behavior with an example, before asking my questions. NB: the below is a bit of a ...
0
votes
0
answers
40
views
partial function template specialization with templated object as function argument [duplicate]
suppose i have this class :
template <typename T> class MyClass{
}
template <typename T> class MyOtherClass{
}
and some generic template function
template <typename A> void ...
1
vote
1
answer
88
views
How to create a generic factory function that creates an instance of some type? [duplicate]
I need to create a factory function to create an instance that can accept any type with the different number of arguments.
I have two classes Employee and Contact and to create an instance for both ...
1
vote
2
answers
115
views
Is it mandatory to have a function template to pass std::vector as an argument?
Is it mandatory to have a function template to pass std::vector as an argument as in the below code?
Also, in the parameter, why do we need to pass <T> along with std::vector?
template <...
2
votes
2
answers
92
views
Parameter pack and perfect forwarding
I just wrote following simple code but it doesnt compile:
#include <iostream>
#include <string>
class Obj{
public:
std::string name = "Name";
std::string l_name = "...
0
votes
2
answers
75
views
Declaring and naming a type in the function signature so it can be re-used in the function
I have a utility function for transforming an iterable according to a functor:
template<typename Iterable, typename Functor>
auto transform(Iterable const& input, Functor&& ...
0
votes
1
answer
98
views
Can't create multiple constexpr getter
I have a struct which wraps multiple std::sets of data. I want access to the sets to be computed at compile time. To do this, I created a template constexpr getter method to return the requested set.
/...
3
votes
4
answers
164
views
Template specialization using a variable argument
I am trying to wrap a c-style variant in a library. It uses a type enum, which I mean to fill using templates.
A simplified version looks roughly like this:
enum Type
{
Type_Int,
Type_Double,
...
4
votes
1
answer
79
views
Function that takes both variadic arguments and a class type?
In C++, is it possible to create a templated function which takes both a class type and a variable number of arguments? The function I'm imagining would work something like this (although this is ...
2
votes
2
answers
84
views
How to handle variable number of arguments and string variables
In the following code, the variable number of arguments assume a string variable in itself but it does not happen for string literals
#include <iostream>
#include <string>
#include <...
0
votes
0
answers
53
views
CPP inheritance with template functions
I have the code like this (it's simplified version, really code more complex):
class IFoo {
public:
template <typename T> T GetInc(T x) const { return x; }
};
class Foo : public IFoo {
public:...
30
votes
1
answer
2k
views
Overload resolution and template argument deduction - why is 0 special?
In the following example, 0 behaves in a special way: it chooses a different overload than one would expect for one example function call. I would like to know why. My understanding is also below.
#...
-1
votes
1
answer
106
views
Function argument list doesn't match function template (function template is made for passing stack allocated array) [closed]
I made function template to pass stack-allocated array as parameter, which is shown below:
struct Pos {
int row;
int col;
Pos(int r, int c) {
row = r; col = c;
}
Pos() {
...
0
votes
2
answers
139
views
How can I find function arguments in a C++ function template?
I am working on a C++ function template and I am trying to find a way to access the function arguments within the template. Specifically, I have a template function called enumerate that takes a ...
1
vote
1
answer
100
views
How does overload resolution work with variadic template arguments and a non template argument derived type
I'm trying to create a overload for a function with variadic template arguments for a type and all its possible derivates.
Naively i tried with just one overload and the base class as the first ...
1
vote
1
answer
148
views
Battling type parameter order and enable_if specifications in function templates
Consider:
template <typename InputIt, typename T = typename std::remove_const<typename InputIt::value_type>::type>
std::vector<T> WorkOnIt( InputIt first, InputIt last)
{
std::...
7
votes
1
answer
188
views
Is a function template accepting const char(&)[N] more specialized than function template accepting const T&?
I've defined two versions of a function template called compare:
#include <cstring>
using namespace std;
// fist version
template <size_t N, size_t M>
int compare(const char (&a)[N], ...
5
votes
1
answer
209
views
c++ address of an overloaded function
I have the following small C++ code sample, but I can't figure out why the compiler works this way, although I have spent quite a lot of time studying cppreference. I would appreciate any explanations!...
0
votes
0
answers
68
views
The difference of " *mat.ptr<float>(i,j)" and "*mat.ptr(i,j)"?
I'm attempting to modify values in an Opencv::Mat using the code below:
void gaussian_high_pass_kernel(cv::Mat& gaussianBlur, float sigma)
{
float d0 = sigma;
for (int i = 0; i < scr....
2
votes
1
answer
77
views
Why do I have `-Wunsupported-friend` warning when I compile friend function template?
I want to declare member function of template class as friend. But I got warning message such as warning: dependent nested name specifier 'Schedule<T>::' for friend class declaration is not ...
0
votes
1
answer
185
views
Is there any workaround for a virtual function template with a type constraint in this case?
If you have a concept and a class member function template like so:
template<typename T>
concept Vector2 = requires (T t) { t.x; t.y; };
struct Shape
{
bool contains(const Vector2 auto&)...
1
vote
2
answers
103
views
deducing non-type template argument type from template function argument
I have a template function parametrized by a compile-time constant whose type should be the same as the type of the function argument.
For instance, I'm seeking to have this kind of syntax:
#include &...
1
vote
1
answer
145
views
Can you conditionally remove a function parameter based on a template parameter?
my function looks like this
template<bool extra>
void func(int& arg1, const int arg2){
//a lot of code...
if (extra && arg2 > 0) ++arg1;
arg1 *= 10;
//a lot of ...
1
vote
1
answer
117
views
Function template overloading vs class templates
Function-templates can be overloaded by using different template-parameter-lists (if I interpret https://en.cppreference.com/w/cpp/language/function_template correctly). If the template-parameter-...
0
votes
1
answer
68
views
declaring a template function with void return taking a typedef
I have looked at most of the related posts but couldn't find a response that related to my specific usage scenario.
The code is as shown below:
//classA.h file
#include <type_traits>
#include &...
4
votes
0
answers
150
views
If I forward declare a function template, may I put the definition after the calling site and not explicit instantiate it at all?
In a header file of a large project, I have to forward declare a function template before the calling site. The code boils down to this:
//H1.h
#pragma once
template <typename>
void f();
...
2
votes
1
answer
313
views
How to create an array of N floats values with fold expression?
Suppose the following function
template<size_t N>
constexpr std::array<float, N> make_ones()
{
std::array<float, N> ret{};
for (size_t k = 0; k != N; ++k)
{
ret[k]...
2
votes
2
answers
128
views
What is the difference of using `typename` in the following?
What is the difference between using "typename" before the return type of a function and without using it at the declaration of a function like the following below?
And what is different if ...
0
votes
3
answers
148
views
Why I need default sum function, for a variadic template sum?
I want to calculate the sum of any number of arguments given to function sum. Assuming that integers given to the function will satisfy operator+.
If I comment out the function sum() (the one having ...
2
votes
2
answers
144
views
Is there a way to pass a function template, as an argument in another function?
The best way for me to describe what I am asking is a simple example.
template<typename T>
void execute_example(T* begin, T* end)
{
T val = 10 * 0.8;
while (begin != end)
{
...
2
votes
3
answers
139
views
How to define and use std::less as a template argument in a function definition?
What is the proper C++ syntax to supply and use std::less as a template argument?
#include <iostream>
#include <functional>
template<typename CMP>
float order(float a, float b)
{
...
9
votes
2
answers
433
views
Implementing variadic Max function in C++20
Despite, the fact, we have std::max, I wanted to try if it is possible to make a Max version that takes variadic arguments and calls the Max recursively for finding the max element.
I saw similar ...
2
votes
2
answers
173
views
Is it possible to return a member variable of a class specified by a template function?
I am trying to generalize a function for a game engine I am writing to simplify the shader loading process.
Anyway, the difficulty arises in my attempts to templating the function. I attempted to ...
-1
votes
1
answer
317
views
a type template function that accepts only a few type
fn foo<T>() -> f32 {
return 0.0;
}
So I want to define a function that accepts only certain types (e.g. i32, i64, f32) as template parameter.
For this case I hope to define only foo<...
0
votes
1
answer
162
views
How to use constexpr function template to re-write a very long switch-case efficiently?
To easily re-write a big switch-case, I'm trying constexpr function template like this (it's only a simplified version with just cout):
#include <iostream>
template<int T>
constexpr void ...
1
vote
2
answers
862
views
Template definition to wrap any callable: class member functions, among others
I looked up to this and this SO answers, but none solves my problem.
Short description: the template function, where much more should happen than just that of this example, should be able to wrap any ...
1
vote
1
answer
110
views
C++ function template specialization, function redefinition error
So, I have an assignment for college to create templates for these functions and ive done it by the instructions.. However, the fillDefault, which I had to make a specialization for int doesnt work. I ...
-2
votes
1
answer
45
views
Using function templates for function overloading
I'm trying to overload a function while adhering to the DRY principle. The only difference between overloads are argument types, so I chose to use templating. I came up with essentially the following ...
0
votes
1
answer
108
views
How to use functions of different types as template arguments? [duplicate]
That's my demo to illustrate:
#include <iostream>
#include <ostream>
#include <utility>
template <typename T>
bool f(int) {
__builtin_unreachable();
}
template <>
...
4
votes
2
answers
277
views
Which member functions can be templated in C++?
I was asked a question
Which class functions can be templated in C++? (constructor, destructor, const, static)
Did I understand correctly that all member functions (except destructor) can be ...
3
votes
1
answer
137
views
call template function for each template parameter in constexpr std::array<size_t,3>
Given a function print<size_t>(void) and a constexpr std::array<size_t,3> q={1,2,3}, I want a loop that calls print<qi> for each qi in q.
My minimal example looks like this:
#include&...
4
votes
1
answer
213
views
Non type template parameter in msvc does not compile
I studied about non-type template parameter and came to know that they must be compile time constant. Then I created the below given program that compiles with gcc as well as clang but fails to ...