Questions tagged [function-templates]

-1

votes
0

answer
21

Views

std::string out of range eventhough I am not using any index

I get the following error message: string subscript out of range. I believe it has more to do with my templates definition because I am not using any string subscripts anywhere. I have not been able to figure this out, I have tried defining and implementing my templates in various ways but the resul...
Daniel Duque
1

votes
0

answer
14

Views

Is a fully specialized template function the same as a regular function?

If I have: template bool name (std::string); template bool name(std::string); What is the difference between the fully specialized function and my other regular functions. For example in the header I would have to have these declarations plus the template definition; nevertheless I can have the sp...
Daniel Duque
2

votes
0

answer
22

Views

using-declaration for overloaded inherited function template leads to “ambiguous call to overlaoded function”

I have a base class C that implements multiple overloads of a function c(), of which one is a function template, and a sub class D that re-implements the function template. D::c() causes all overloads of C::c() to be hidden in D. As I want the overloads that are not re-implemented by D to not be hid...
Kaiserludi
1

votes
4

answer
986

Views

How to distinguish data structure template and function template in C++?

I have learned that data structures can be created using templates in the following way: template struct X { T weight; int age; }; The functions can also use templates in the following way: template T func_name(int age, T human) { something_here; } One of the difference s is that in the first case...
Roman
1

votes
1

answer
214

Views

implementing a generic binary function with a class and functor as template parameters

I am trying to wrap some templated functions into some binary functors like below. When I try to compile the code I have the error error: no match for call to ‘(QtyAsc) (myobj&, myobj&) I thought that being operator() in QtyAsc a function in a class, the template deduction mechanism would have wor...
1

votes
3

answer
131

Views

Disallow function template instantiation with iterator parameter

I have a function template which takes a templated parameter: template RefT make_ref(R& res) { return RefT(&res); } I either want to prevent R from being any kind of iterator, or, if this is easier, I want to have a overload that the compiler will prefer to use for iterators which calls make_ref ag...
Lynden Shields
2

votes
2

answer
1.4k

Views

How to select the right overloaded function template at compile-time?

I'm trying to understand how to select the right overloaded function template at compile-time, but the compiler is giving me a hard time. I can make it work, but I don't understand what is going on. Let me explain. I have two structs A and B like below. One has a special function and the other a nor...
murrekatt
2

votes
1

answer
315

Views

“No matching function call” in template function

I keep getting the following error when trying to write a template function: main.cpp|17|error: no matching function for call to ‘dotproduct(vector&, vector&)’| I've searched on the error and found some other cases where a non-type template parameter could prove problematic if the parameter was...
Maarten
17

votes
7

answer
65.4k

Views

Template function in C# - Return Type?

It seems that c# does not support c++ like templates. For example template myType GetMax (myType a, myType b) { return (a>b?a:b); } I want my function to have return type based on its parameters, how can i achieve this in c#? How to use templates in C# EDIT: Can i use object and getType for the alm...
SMUsamaShah
2

votes
1

answer
49

Views

Function template overloading with reference collapsing

In addition to this question, Function template deduction l-value reference and universal reference, I have another question. Note you do not have to read this post to understand this, but it might be helpful. This post does by the way not show the ideal solution to the problem I believe, but that's...
Lourens Dijkstra
6

votes
3

answer
445

Views

Why do two functions have the same address?

Consider this function template: template unsigned long f(void *) { return 0;} Now, I print the addresses of f and f as: std::cout
Nawaz
5

votes
3

answer
315

Views

Template parameter default to a later one

This link doesn't answer my question so I'll ask it here: Basically I want to write a template function template Out f(In x); Here I always need to specify Out when calling f. I don't want to do it every time, so I basically want template Out f(In x); Which means if I don't specify Out, it will...
Kan Li
2

votes
3

answer
155

Views

templates of functions

I'm told to create template of function , that will take 4 arguments : pointer reference pointer to array pointer to function How to perform this task ? I was trying : #include using namespace std; int nothing(int a) { return a; } template T func(int *L, int &M, char *K, int (*P)(int)) { cout
anotr67
3

votes
3

answer
637

Views

Delayed function template instantiation

In this rather contrived example, I'm trying to pass a function template to my function, and want my function to instantiate the function template internally. In essence, I don't want the user to know the types, and workings of my function, but just be able to pass a function template for me to inst...
21

votes
4

answer
2.1k

Views

Partial ordering with function template having undeduced context

While reading another question, i came to a problem with partial ordering, which i cut down to the following test-case template struct Const { typedef void type; }; template void f(T, typename Const::type*) { cout
2

votes
3

answer
303

Views

Determination of type in function template

I would like to ask you for an advice about function template. I have a function that adds some data into buffer. But I need also to add an information about data type into the buffer. The type of data is a following enum: enum ParameterType { UINT, FLOAT, DOUBLE }; And I need to create a function t...
Bhiefer
5

votes
1

answer
372

Views

D function templates and type inference

Consider the following code: module ftwr; import std.regex; import std.stdio; import std.conv; import std.traits; S consume (S) (ref S data, Regex ! ( Unqual!(typeof(S.init[0])) ) rg) { writeln (typeid(Unqual!(typeof(S.init[0])))); auto m = match(data, rg); return m.hit; } void main() { auto data =...
vines
5

votes
3

answer
325

Views

Why is the compiler not selecting my function-template overload in the following example?

Given the following function templates: #include #include struct Base { }; struct Derived : Base { }; // #1 template void f(const T1& a, const T2& b) { }; // #2 template void f(const std::vector& v, Base* p) { }; Why is it that the following code always invokes overload #1 instead of overload #2...
Steve Guidi
3

votes
4

answer
418

Views

How to get an object of a unknown class with given classname

I am searching for a way to determine at runtime, which type of object should be alloced (based on a given class name, which is of type const char*). Well the simplest way of course is to use loads of ifs /else ifs, but that doesnt seem applicable, because i have > 100 different classes(well at leas...
smerlin
2

votes
4

answer
2.4k

Views

class T in c++ (your definition)

The one advantage of using class T in c++ is to reduce the time to redefine data types in a function, if those data types are defined in other function, for example, in int main. template void showabs(T number) { if (number < 0 ) number = -number; cout
CppLearner
3

votes
1

answer
1.6k

Views

Calling static function template within dependent scope

Suppose I have a static function template template void ft() within a struct template template S, and I want to call ft from another function template template void g(), passing the bool template parameter from g to S: template struct S { static void f() { } template static void ft() { } }; template...
Ose
3

votes
4

answer
72

Views

Which of two overloaded templates will be called?

I am still trying to figure out templates. I have read about the specialization rules and don't understand what is happening here. I have defined the following in templates.h: #include template void f(foo p) { std::cout
Sarien
2

votes
1

answer
639

Views

How to modify each element of a parameter pack, and create a tuple from those?

I'm running into an issue with a variadic function template. I need to examine each element of a parameter pack, package the element, then stuff all the packaged elements into a tuple and return that. Here's the general idea of what I'd like to do (return types are just placeholders, not sure what...
Brett Rossier
23

votes
2

answer
6.2k

Views

How to pass a template function in a template argument list

Suppose I have a template function: template T produce_5_function() { return T(5); } How can I pass this entire template to another template? If produce_5_function was a functor, there would be no problem: template struct produce_5_functor { T operator()() const { return T(5); } }; template struct c...
2

votes
3

answer
367

Views

Template argument deduction (using both explicit and implicit arguments in same call)

I have three template arguments to a function and am having troubles with (I think) the compiler deducing which template argument is which. The template function is: #include #include namespace cliques { template P find_optimal_partition_louvain(cliques::Graph &my_graph, QF quality_function) { P...
zenna
2

votes
2

answer
81

Views

Addresses of template function instantiations over T and T const

Is it ever possible that in the following setup template inline void id() { //... } template bool check() { return &id == &id; } check will return true for some T? Does it depend on what is being done inside id? What does the standard have to say about this?
yuri kilochek
23

votes
2

answer
1.2k

Views

Will specialization of function templates in std for program-defined types no longer be allowed in C++20?

Quote from cppreference.com: Adding template specializations It is allowed to add template specializations for any standard library |class (since C++20)| template to the namespace std only if the declaration depends on at least one program-defined type and the specialization satisfies all requiremen...
Daniel Langr
12

votes
2

answer
8.3k

Views

Can I use (boost) bind with a function template?

Is it possible to bind arguments to a function template with (boost) bind? // Define a template function (just a silly example) template ARG1 FCall2Templ(ARG1 arg1, ARG2 arg2) { return arg1 + arg2; } // try to bind this template function (and call it) ... boost::bind(FCall2Templ, 42, 56)(); // This...
Martin Ba
1

votes
3

answer
185

Views

What is the preferred design of a template function that requires a default parameter value?

I'm currently working on cleaning up an API full of function templates, and had a strong desire to write the following code. template void doWork(const T& arg1, const U& arg2, V* optionalArg = 0); When I invoke this template, I would like to do so as follows. std::string text("hello"); doWork(100,...
Steve Guidi
4

votes
2

answer
2k

Views

Function template with return type T doesn't compile

The following code compiles fine: template void f(const T &item) { return; } int main() { f("const string literal"); } Compilation succeeded at ideone : http://ideone.com/dR6iZ But when I mention the return type, it doesn't compile: template T f(const T &item) { return item; } int main() { f("cons...
Nawaz
2

votes
1

answer
1.7k

Views

Comparing A Data Type in an If Statement

Is there a way to test a parameter's data type in an if statement? Here is an example of source code: it will not compile but it used to present my intentions. typedef char cInt[sizeof(int)]; typedef char cFloat[sizeof(float)]; typedef char cDouble[sizeof(double)]; template char* convertToCharStr( c...
Francis Cugler
6

votes
2

answer
241

Views

Argument-dependent lookup and function templates [duplicate]

This question already has an answer here: Why doesn't ADL find function templates? 3 answers Here is an example: #include #include #include using std::string; int main() { string str = "This is a string"; // ok: needn't using declaration, ADL works auto it = find(str.begin(), str.end(), 'i'); //...
chihyang
1

votes
1

answer
859

Views

ambiguous error: template C++

I've tried almost everything imaginable (apart from the right thing of course), but still can't see why I'm getting an ambiguous error. I am fairly certain it's something really silly but I just can't see it! My compiler shows warnings with the insertion operators and I know they're both being calle...
Savvy
3

votes
1

answer
80

Views

explicit specialization for function template c++

template bool validate(const T& minimum, const T& maximum, const T& testValue) { return testValue >= minimum && testValue = b && a
Andy
7

votes
1

answer
629

Views

C++ about generic initialization in templates

I am writing a generic function like below. template void foo(Iterator first, Iterator last) { T a; cout
1

votes
1

answer
104

Views

Same code blocks about function template compiled ok under g++ but error under VC6, why?

I am reading the chapter "function templates" of C++ Primer 3rd edition, and when I tried to follow the example, I found the code almost the same as the book has encountered an error during compilation under VC6 but everything is ok under g++. I don't know why? Here is the code: #include using name...
OriginalWood
2

votes
1

answer
238

Views

C++: Issue with Function Template

I'm having an issue revising a c++ function, so that it works with any data type. Any and all help would be hugely appreciated. Below is my code, and an error I keep running into. // This program demonstrates the QuickSort Algorithm. #include #include using namespace std; //***********************...
Adam_G
2

votes
2

answer
253

Views

Template function specialization symbol matching across libraries

So far, I had a setup where a certain function template getF was declared like this in the headers template F* getF(); leaving the function body undefined. Then on a shared library, getFhas some specializations.. template F* getF() { static int r = 42; static Finstance(r); return &Finstance; } temp...
lurscher
2

votes
1

answer
281

Views

C++ Weird Type Conversion Error wtih Function Template

I have these C++ codes: In "header1.h" template int f1(const T& t1) { return 0; } extern const int p[]; in cpptests.cpp #include "header1.h" int _tmain(int argc, _TCHAR* argv[]) { f1(p); return 0; } And in VC++2010, I got this compile error: c:\work\cpptests\cpptests\cpptests.cpp(10): error C266...
JavaMan
5

votes
1

answer
97

Views

C++ Function Template instantiaion with implicit parameters

I can't figure out why the following code compiles fine: #include void bar(int x) { std::cout
kirakun

View additional questions