function-templates

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 function PyArg_ParseTuple(args, "O&O&:...

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 <typename T, typename U, typename V> 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,...

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

Given the following function templates: #include <vector> #include <utility> struct Base { }; struct Derived : Base { }; // #1 template <typename T1, typename T2> void f(const T1& a, const T2& b) { }; // #2 template <typename T1, typename T2> void f(const std::vector<std::pair<T1, T2> >& v, Base* p) { }; Why is it that the followin...

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 least they all derive from one base class)...

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 0; } The problem is ...

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 <class T> void showabs(T number) { if (number < 0 ) number = -number; cout << number << endl; return 0; } int main() { int num1 = -4; ...

Function template overloading: link error

I'm trying to overload a "display" method as follows: template <typename T> void imShow(T* img, int ImgW, int ImgH); template <typename T1, typename T2> 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 g...

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 <iostream> using namespace std; int nothing(int a) { return a; } template<typename T> T func(int *L, int &M, char *K, int (*P)(int)) { cout << L <<...

How to use class templates as function arguments?

I have a class declared along the lines of template<int a, int b> class C { public: array[a][b]; } and I want to use it as argument in a function like this: bool DoSomeTests(C &c1, C &c2); but when I compile, it tells me 'use of class template requires template argument list.' I tried template<int a, int b> bool DoSomeTests(C...

Template meta-programming with member function pointers?

Is it possible to use member function pointers with template meta-programming? Such as: class Connection{ public: string getName() const; string getAlias() const; //more stuff }; typedef string (Connection::*Con_Func)() const; template<Con_Func _Name> class Foo{ Connection m_Connect; public: Foo(){ cout << (m_...

Template function in C# - Return Type?

It seems that c# does not support c++ like templates. For example template <class myType> 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 almost same purpose? ...

Is there a C++ equivalent to Java's Collection interface for STL container classes?

I would like to pass arbitrary container as an argument of function and iterate over it (no erasing nor pushing elements). Unfortunately it looks like there is no standard way of doing this. First solution which comes to my mind is an interface (let's call it CollectionInterface) implemented by classes that will wrap STL containers. so...

Error in template function (using Boost.Tuples)

#include <list> #include <boost/tuple/tuple.hpp> template<class InputIterator> void f(InputIterator it) { typedef boost::tuple<typename InputIterator::value_type, int> Pair; std::list<Pair> paired; typename std::list<Pair>::const_iterator output; for(output=paired.begin(); output!=paired.end(); ++output) { ou...

Function Templates - Explicit specialisation vs Global Functions (C++)

I know that Function Templates are used so as to make the functions portable and so that they could be used with any data types. Also Explicit Specialization of templates is done if we have a more efficient implementation for a specific data type. But then instead of Explicit Specialization we could also just code a Nontemplate Functio...

Logical error in Function template.

My professor has given me this assignment. Implement a generic function called Max, which takes 3 arguments of generic type and returns maximum out of these 3. Implement a specialized function for char* types. Here's my code : #include <iostream> #include <string> using namespace std; template<typename T> T Max(T first,T ...

Templated Functions.. ERROR: template-id does not match any template declaration

I have written a function template and an explicitly specialized templated function which simply takes in 3 arguments and calculates the biggest among them and prints it. The specialized function is causing an error,whereas the template works fine. But I want to work with char* type. This is the error I get=> error: template-id ‘Max<>...

Determination of type in function template

Hi all! 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 template from funct...