Questions tagged [function-templates]

1

votes
2

answer
48

Views

Overload of template function with one that accepts lambdas

Apologies for jumping here, but my google-foo is not up to snuff for template functions (or function templates?) and their overloads with function templates (or template functions?). I'm trying to overload a function template with another function template that can take a lambda and I can't figure o...
xaviersjs
1

votes
0

answer
40

Views

Deduce the template parameter of an outer class in a nested class function call?

Is there a way to deduce the outer template in a nested class? template struct A{ struct B{}; }; template void f(typename A::B b){} // hard to deduce T? int main(){ A::B b; f(b); // no matching function for call to 'f(A::B&)', meant to call f(b); } Or I am forced to declare the nested class outside?...
alfC
1

votes
0

answer
89

Views

Subscript operator and implicit conversion to a pointer type

I'm reading C++ Templates - The Complete Guide, 2nd Edition, and B.2.1 tells about implicit conversion of the implied 'this' argument. Same example here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1592.pdf Depending on the typedef of ptrdiff_t, the compiler may deduce that there is an...
Chabster
1

votes
1

answer
49

Views

How to organize template-functions and functions, in headers and source files

I am learning to use template functions and organize my code in multiple files. I looked at Why can templates only be implemented in the header file? where they point out I should implement my template-functions in the header; I also looked at C++ inline functions: declare as such, define as such, o...
Daniel Duque
1

votes
2

answer
128

Views

Same type for template function inside template class

I have the following code: #include using namespace std; template class myclass { public: T data; myclass(T const & _data = T()) : data(_data) {} template myclass & operator=(myclass const & rhs) { cout
lexsintra
1

votes
1

answer
1.3k

Views

template constructor overloading issue

I have a class template with three constructors and one of them is function template. template class TemplateOverLoading { public: TemplateOverLoading(void){}; ~TemplateOverLoading(void){}; //constructor that take reference TemplateOverLoading(std::string& qName, T& qValue ) : mName(qName), mValue...
jazaman
1

votes
1

answer
105

Views

boost::function with function templates

#include #include #include 'boost/function.hpp' template static void FOREACH (T1 cont, boost::function callback) { typename T1::iterator it = cont. begin (); for ( ; it != cont. end(); it++ ) { callback (*it); } } static void Print (int number) { std:: cout
Vishnu Kanwar
1

votes
1

answer
1.3k

Views

nodejs: function template without return value

I followed the instruction of node.js to implement factory wrapped objects. So far it works. But what I dont get to work is a function without a return value. I.e.: (extension of the example in the link) In myObject.h: tpl->PrototypeTemplate()->Set(String::NewSymbol('some'), FunctionTemplate::New(...
baam
1

votes
1

answer
42

Views

Function template deduction l-value reference and universal reference

Let's say I have the function copy: template void copy( Buf&& input_buffer, Buf& output_buffer) {} In which input_buffer is a universal reference and output_buffer is an l-value reference. Reference collapsing rules make sure input_buffer is indeed, regardless of the deduced type of Buf, an univer...
Lourens Dijkstra
1

votes
1

answer
0

Views

Overloaded function is not always selected before template instantiation

I have created some code that uses a function template and an overload (not a specialization) of that function. When I call the function with an object of a class that is derived from the parameter in the it uses the template resulting in a compile error. I have been reading http://www.gotw.ca/publi...
user1777224
1

votes
1

answer
0

Views

Conversion operator overloading

I want to distinguish a template conversion operator between &, * and values: struct S { template constexpr operator T() { return value; } template constexpr operator T&() { return value; } template constexpr operator T*() { return &value; } int value; } s{5}; int main() { uint32_t ui = s; // err...
JulianH
1

votes
2

answer
0

Views

C++ Function Pointer issue

For some reason trying to pass a pointer to this function to a varadic function produces the following error: 1>c:\... : error C2664: 'PyArg_ParseTuple' : cannot convert parameter 3 from 'int (__cdecl *)(PyObject *,void *)' to '...' 1> Context does not allow for disambiguation of overloaded f...
Fire Lancer
1

votes
1

answer
0

Views

Using typedefs from a template class in a template (non-member) function

The following fails to compile (with gcc 4.2.1 on Linux, anyway): template< typename T > class Foo { public: typedef int FooType; }; void ordinary() { Foo< int >::FooType bar = 0; } template< typename T > void templated() { Foo< T >::FooType bar = T( 0 ); } int main( int argc, char **argv ) { return...
atomicpirate
1

votes
2

answer
0

Views

C++ unexplained variations in template function matching with enums

I have the following code containing a template function. When I am calling this function with second parameter being an enum: in some cases it finds the template specialization and in some cases it does not. I had verified that enums are the same enums in both cases (e.g there are no redefinitions...
zzz777
1

votes
3

answer
71

Views

How to specify template argument for a function in a child?

So I try: class data_ppp { public: template virtual boost::shared_ptr getData() { return boost::shared_ptr(new T()); } }; class data_child : public data_ppp { public: template getData(); }; but cant get desired effect - I want to have in class data_child getData function that would only return boos...
myWallJSON
1

votes
1

answer
393

Views

Function template with explicit specification of type

I'm trying to understand below sample code I have. I know we can explicitly specify data type but not sure what 'int, double' and 'int, int' means. And why do we write function template that way instead of T tripleit (T Val) { T temp = val * 3; }? Thanks in advance. #include using namespace std; /...
user23
4

votes
1

answer
84

Views

Can C++ free functions be aliased?

I have a namespace with one highly templated free function, such as: namespace a { template void f(T t, K k, std::vector h_vec = {}) { /* body */ } } Inside another namespace, for convenience I would like to have some aliases of it for a bunch of specific usages, such has: namespace b { using my_spe...
nyarlathotep108
1

votes
1

answer
386

Views

Specify return type based on other template argument

I'd like to specify my templated function's return type, by an other template argument. All of this inside a class. In the header file: class MyClass { template RT myfunc(); }; In the .cpp something like this: template int MyClass::myfunc() { return 2; } template double MyClass::myfunc() { return 3....
Sipka
1

votes
2

answer
106

Views

Is There a Way to Declare a typename for a Templatized Function?

So, I have this templatized function (which I know is ugly to look at.) My intention was not to default the template parameter though, my intention was to create a typename derived from T that could be used in caster that the user could not assign to. My question is how do I create a typename for a...
Jonathan Mee
1

votes
1

answer
56

Views

When do I need to specify the type a template

I've written a simple template to find the smallest number in a list of arguments. template T smallerList(T a, T b) { std::cout
Maurice
1

votes
2

answer
267

Views

Function template overloading: link error

I'm trying to overload a 'display' method as follows: template void imShow(T* img, int ImgW, int ImgH); template void imShow(T1* img1, T2* img2, int ImgW, int ImgH); I am then calling the template with unsigned char* im1 and char* im2: imShow(im1, im2, ImgW, ImgH); This compiles fine, but i get a...
matt
1

votes
1

answer
1.7k

Views

Passing template typedef as argument to a function template

I am trying to pass a template typedef as argument to a function template. However I get following errors: TestTemplates.cpp:11: error: expected unqualified-id before ‘&’ token TestTemplates.cpp:11: error: expected unqualified-id before ‘&’ token TestTemplates.cpp:11: error: expected initial...
nurabha
1

votes
1

answer
21

Views

C++ static template class member as friend template function default parameter

Why does using static template class member as friend template function default parameter give me compile error in c++? How to slove? Here is the code: #include template void func(T n); template class MyClass { private: static T statTemp; public: friend void func(T n); }; template T MyClass::statTe...
so_Orange
-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
994

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
215

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

View additional questions