C++ :: Passing A Function Pointer As Template Argument To A Class
Aug 15, 2012
I have in the past written code for templated functions where one function argument can be either a function pointer or a Functor. Works pretty straightforward.
Now I am in a situation where I am actually trying to pass a function pointer as template argument to a class. Unfortunately this does not work, I can pass the Functor class but not the function pointer. Below code illustrates the issue:
The idea is to have the definition of the Record class simple and readable and have a maintainable way to add auto-conversion functions to the class. So the lines I commented out are the desirable way how I want my code to look. Unfortunately I could not come up with any way that was close to readable for solving this.
Based on the arguments passed to foo, the compiler can deduct the type T. But on the other hand, when we use a class template, we always need to specify the type, for example,
Code: template<class T> struct sum { static void foo(T t1, T t2) { } }; sum<int>::foo(1,3);
Here we can't call sum::foo(1,3), otherwise we get compiler errors. My question is why the compiler can't deduct the type based on the arguments passed to foo? In addition, if we call function template foo like this,
Code: foo(1, '3');
Then we get compiler errors. We need to specify the type like foo<int>(1.'3'). Since '3' can be always treated as integer, why we need to specify the type here?
I'm trying to pass the pointer of a dynamic array into a template function, but it keeps telling me there is no matching function to call because the parameters I'm passing in are wrong. how to make the function accept the pointer.
//main int main() { srand(unsigned(time(NULL))); int size; int *list; int *listCopy;
Basically I'm trying to pass an object as a reference to the template function, rather than a copy as it's seeing. I'm needing to do this without editing Obj::Call to accommodate a reference as its first parameter, as it'd break other calls.
You'll notice in the following code the object will be destroyed upon passing, while the object defined is still in-scope due to the infinite end loop.
#include <iostream> #include <string> using namespace std; class Obj { public: string name; Obj(string name): name(name) {cout << "create " << this << endl;}
[code]....
In the past I tried ref(), which appeared to stop this happening, however it created a blank copy of the object instead.
Error1error C2955: 'DoubleLinkedListInterface' : use of class template requires template argument listdoublelinkedlist.h10 Error2error C2244: 'DoubleLinkedList<T>::DoubleLinkedList' : unable to match function definition to an existing declaration doublelinkedlist.cpp7
Error3 .cpperror C2244: 'DoubleLinkedList<T>::~DoubleLinkedList' : unable to match function definition to an existing declaration 12
.h
#pragma once #include "DoubleLinkedListInterface.h" #include "Node.h" #include <iostream>
I am having trouble modifying a linked list. I am writing a function to delete the last node from the linked list, but it gave me incompatible types error.Here is my struct:
It gives me the following errors: initialization from incompatible pointer type at line: PCB_p temp = process_list assignment from incompatible pointer type at line: process_list = temp
How to pass an int that I got from user input into a function to use it. I am trying to print out the words to a string of numbers.
I got the input from user. I got an absolute value of the input. I then separate the string into individual digits and name them. I can print these out. Then I started my if statement by checking if the original input was zero, and if it is, printing zero and exiting. Then I an trying to pass the digits into a switch function and this is where I go off the rails.
how can i pass an array as an argument to the function? in getCoin() fcn, I am supposed to pass coins array as an argument to the function. fcn prompts user to enter coin(Date, Type and Country). values entered by user are read and assigned to the coins array. I tried the code below.
//# include "Coins.h"; #include <iostream> #include <string> using namespace std;
I am trying to pass a class as a type to a template class. This class's constructor needs an argument but I cannot find the correct syntax. Is it possible?
Here is an example of what I described above. I did not compiled it, it is for illustrative purpose only. And of course argument val of the myData constructor would be doing something more useful than simply initializing an int....
template <class T> class templateClass { templateClass() {};
[Code]....
My real code would only compile is I add the myData constructor:
myData () {};
and gdb confirmed that it is this constructor that get called, even with dummy(4).
I am using a small robotic-car that is controlled by writing C/C++ codes under Linux. I need to use a particular function from the library provided by the manufacturer. The relevant API documentation for the function is:
Returns: BASE_OK RS232 data acquisition success BASE_BASE_232_GETDATA_ERR RS232 data acquisition failure
I have trouble writing the relevant code in the main program that invokes this function. Here is a snippet of what I have tried:
# include "Baseboard.h" int main () { Baseboard _Baseboard; // Class name is Baseboard char *msg ;
[Code] ......
The part where I am uncertain is how to handle the char pointer "msg" in the declaration, function call and referencing. According to the documentation, the char pointer "msg" is the output of the function so I presume that is is somehow dynamically allocated. Am I handling the char pointer properly in the declaration, function call and referencing parts?
Another related question I have is: I am printing out the value of the variable "dummy". I always get 0 for it. Since the variable "dummy" is an enum of type BASEBOARD_ERROR_KIND which can take on two values (first value represents success and the second failure), it is alright to get a integer value of 0 for it if the function call was successful ? (I do not have much experience with using enums so this is a enum-related question on whether we can get an integer value representing the first enum value) .
Why does the following code compile and execute without any error? I mean, the function compareid should get 2 arguments so why does the compiler not complaining, is it because of the type of arguments?
Code: #include <stdio.h> int compareid(void* info, int value); // ansi declaration int compareid(void* info, int value)
I am trying to pass function as argument to another function. My idea is to write function that can works with any type of array, and for it to be able to compare array items I'd like to use my own compareTo function. But I need to be able to pass function to use for comparing argument.
To say it short I am trying to write my own qsort that would take compareTo as one argument just like original qsort does.
Here is my code
// test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <windows.h> template <class T> int compareTo( T a,T b){
[code]....
and errors
1>d:my documentsvisual studio 2012projects est est est.cpp(29): error C2896: 'void DoSomething(T,int (__cdecl *)(T,T))' : cannot use function template 'int cmp(T,T)' as a function argument 1> d:my documentsvisual studio 2012projects est est est.cpp(8) : see declaration of 'cmp' 1>d:my documentsvisual studio 2012projects est est est.cpp(29): error C2784: 'void DoSomething(T,int (__cdecl *)(T,T))' : could not deduce template argument for 'T' from 'int [3]' 1> d:my documentsvisual studio 2012projects est est est.cpp(21) : see declaration of 'DoSomething'
I'm trying to implement a simple template array class, but when i came into the operator< i actually have to use a template :
my code is something like :
template<typename _Type, std::size_t _Size> class array { public :
[Code] ......
but i am having an error of shadows template param 'class _Type' is it w/ the name conflict between the array template parameter and the function template parameter ?
I was just wondering how is this generally resolved. Let say you have this large function that runs in two modes. In the first mode it evaluates the data passed to a function as a map the the second mode it fills the map. example:
Code: template <typename Map, typename Int> void func(Map & map, Int i){ int z = 0; string zz;
[Code] ....
The point is i do not want to write a large function just to include different modes so i decided to set "i" to be a mode identifier. However when i want to compile my function given two modes i get an error since the modes are not recognized (obviously). if i pass map as
Code: map<int,int> and mode 1 i get an error here : Code: map[z] = z; besause map Code: map[z] expects z to be an int not string and the other way around (though in practice this cannot happen since i set the modes). So am i restricted to writing my function for both modes separately (polimorf.) or there is a way to make my example work.
Pseudocode: template<typename T /*, some parameter for member_function */> class Foo { public: void someFunction() { T t; t.member_fuction(...); } }
I'm trying to make the call to T::member_function a templated value because member_function might vary by name in my scenario. Since std::mem_fn isn't a 'type', i can't do something like Foo<std::string, std::mem_fn(&std::string::clear)> foo;
I also want to take into account that member_function might have more than one parameter. That is, the first parameter will always be something known but there might be other defaulted parameters.
The only thing I can think of is to make a proxy structure, something like this:
I am facing a real-life problem, it can be simplified as below:
#include <iostream> using namespace std; class B; class A { public: void f1(A a) {} void f2(B b) {}
[Code]...
There is no problem at all with the f1(), it compiles and executes without any problem. But f2() gives compilation error. How to solve this?
The error message is: error: 'b' has incomplete type This is just to define the function f2() in a class, that uses an instance of its child class as one of its arguments.
I'm having some problems in understanding how the code below works and why it produces the output it produces.. What I'd expect is that both functions, namely `add_1' and `add_2', would print the same output; but I've been proven wrong :/ So why does the second one get different memory addresses for the same variable?
Code should be self-explaining:
Code: template<typename... Types> void add_1(Types&&... values) { // by the way: why do i have to use `const int' instead of `int'? std::vector<std::reference_wrapper<const int>> vector{ std::forward<Types>(values)...}; std::cout << "add_1:" << std::endl; for (const auto& value:vector) { std::cout << &value.get() << std::endl;
How do I prevent user passing a class or a structure or aanoter function to my function print. I mean i know if a wrong thing is passed that i'll get an error eventually but is there a way to explicitly check what has been passed. How is this done usually ?
Is it possible to pass a class pointer as memory buffer across the socket? The above code is just an example. My question in general is, whether it's possible to pass any Classes pointer as a memory buffer across sockets.
I am trying to wright a program that takes student grade data from a command line file, calculates a final grade, and copies the final grades to an output file. So far I have two functions, one that creates a student structure by allocating memory for it and returning its address, and one that should take that address and fill it with data from a line from the input file. My ultimate goal is to use a linked list to connect all the structs, but for now I just want to get the functions working. When I run what I have so far, I get an error C2440 (using visual 2010) that says "cannot convert from 'cStudent *', to 'cStudent', and points to the line where I call my fill function. How should structure pointers be passed?