C++ :: Function With Template Arguments

May 26, 2013

I'm trying to create a callback wrapper for pointers to member functions of any class by using templates, std::function and std::bind. This will be used to send incoming sf::Event's to classes who register callbacks with an event manager. I based my code off of the example on this page: URL.....Here's what I have:

class EventCallback
{
std::function<bool(const sf::Event&)> func;

public:
template<typename T>
EventCallback(T* object, bool(T::*func)(const sf::Event&)) { func = std::bind(func, object, std::placeholders::_1); }
bool run(const sf::Event& evt) { return func(evt); }
};

[code]....

View 4 Replies


ADVERTISEMENT

C++ ::  Writing A Function Template With Template Arguments?

Mar 14, 2014

I have a function:

template<class Iterator, class T>
void a(Iterator, Iterator, const T&);

and I want to be able to simplify calls to 'a' with calls like

a(someIteratableContainer);

instead of having to call:

a(someIteratableContainer.begin(), someIteratableContainer.end(), valueOfTheContainersElementType);

I also want to be able to generalize the function to handle any of the standard iteratable contains: array, vector, deque, whatever.

I was under the impression I could write:

template<template<class T> class U> a(U<T>& container) {
a(container.begin(), container.end(), g(T()));
}

where 'g()' returns an object of the element type. However, the compiler is claiming, no matter how I write a call to the overload, the original template is selected and/or the overload is invalid, depending on the various ways I attempt to write said overload.

View 7 Replies View Related

C++ :: Circular Dependency In Template Arguments

Nov 7, 2013

Today I faced a problem where I had circular dependency in my template arguments. I was trying to make a class hierarchy similar to:

template<class BType>
class A_base {
public:
BType* getB();
};

[Code] .....

Basically I had objects that were of type A<B<A<B<...

Basically I have a tree like structure of heterogeneous types that must facilitate two-way interactions where A's can call B's and B's can call A's. This structure is useful in many contexts the difference is the methods A and B provide are different in each of these contexts. Instead of adding the getA and getB and all the other connectivity methods in every version of A and every version of B, I wanted to create a base class that managed this automatically.

Another piece of advice was break up your code so there is a forward-only and backwards-only dependent types. This is not a complete solution because the two cannot know about the other and this does not really facilitate arbitrary two-way communication (where A calls B then B calls A back). It also makes the code more complicated in that I have two sets of objects and interfaces.

So the solution was to make the template arguments specific to the things I wanted to be flexible. The connectivity interface of A_base and B_base should be constant. Hence that cannot be in the template parameter. It was merely the traits that I wanted to make flexible so... I came up with this solution:

#include <iostream>
template<class aTraitType,class bTraitType>
class A;
template<class aTraitType,class bTraitType>
class B;

[Code] ....

Now this compiles and works great. The problem is that aObj and bObj cannot call their opposite within a trait method because print() does not know anything about the connectivity. So the solution there was to make traits an abstract base class. Then magically everything works!

#include <iostream>
template<class aTraitType,class bTraitType>
class A_base;
template<class aTraitType,class bTraitType>
class B_base;

[Code] .....

So this outputs the following. Clearly there is two-way communication!

Class A is not connected to B
Class B is not connected to A
Class A at 0x7fff25d1aa10 reporting for duty
Class B at 0x7fff25d1aa00 reporting for duty
Class B at 0x7fff25d1aa00 reporting for duty
Class A at 0x7fff25d1aa10 reporting for duty
Class A at 0x7fff25d1aa10 reporting for duty
Class B at 0x7fff25d1aa00 reporting for duty

View 6 Replies View Related

C++ :: Enable If With Variadic Template Arguments

Sep 17, 2014

template <typename FIRST, typename... REST, typename std::enable_if<std::is_convertible<FIRST, Base*>::value>::type* = nullptr>
void foo (FIRST first, REST... rest) {}

that successfully allows me to enable the function foo() only if FIRST is convertible to Base*, but I also only want foo() enabled if each type in REST... meets the same condition. What is the syntax for that? If no such syntax exists, how to achieve that effect?

View 7 Replies View Related

C++ :: Expanding Variadic Template Arguments In Lambda

Feb 1, 2013

I'm trying to expand a template + argument parameter list inside a lambda function like this:

template <typename Class, typename ...Args>
static void create(Args ...args) {
// Perform pre-thread creation work.
std::thread([=]()

[Code] ....

But this does not work:

The compiler error is "error: parameter packs not expanded with ‘...’:|"

However, when I do the following:

template <typename Class, typename ...Args>
static void create(Args ...args) {
// Pre-thread work.
auto tthr = [](Args ...ar) -> void {

[Code] ....

It works just fine. That shows that lambda threads are able to take variadic arguments...

So here is my question; what is the correct capture clause for capturing the variadic object correctly?

View 3 Replies View Related

C++ :: Getting Type Alias From Template Class Without Specifying Arguments And Instantiating It

Feb 4, 2015

For example I want to get the type of std::vector<>::size_type without specifying a template argument.

Here's what I want to do:

std::vector<>::size_type x{5};

Is there any way this can be done?

View 4 Replies View Related

C++ ::  how To Declare Template Function Inside Template Class

Dec 5, 2013

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 ?

View 6 Replies View Related

C++ :: Function Does Not Take 3 Arguments

Apr 27, 2013

I am doing a problem where I need to use arrays of string objects that hold 5 student names, an array of one character to hold the five students' letter grades and five arrays of doubles to hold each student's set of test scores and average score.

When I try to run it, I get these five errors.

error C2660: 'getTestScore' : function does not take 3 arguments : line 39
error C2660: 'getTestScore' : function does not take 3 arguments : line 45
error C2660: 'getTestScore' : function does not take 3 arguments : line 51
error C2660: 'getTestScore' : function does not take 3 arguments : line 57
error C2660: 'getTestScore' : function does not take 3 arguments : line 63

what is wrong.

Here's my code.

View 4 Replies View Related

C :: Arguments When Declaring A Function?

Jun 5, 2013

I am a bit confused about how specific one must be with arguments when declaring a function. I'll show you two functions from the book I'm using to learn C to show you what I mean.

Example 1 (greatest common denominator function):

Code:
void gcd (int u, int v) {
int temp;
printf ( "

[Code] ....

So in that function, there are exactly two arguments, because that's how many arguments the algorithm to find the gcd takes. No problem there, makes sense to me. Then further in the chapter on functions I run into this,

Example 2 (square root function):

Code:
float absoluteValue (float x) {
if ( x < 0 )
x = -x;
return x;

[Code] ....

In this second example, we have a square root function that is preceded by an absolute value function. The absolute value function has the one argument, "float x", however when this function is called within the square root function, the arguments "guess * guess * -x" are passed to it. I'm confused how this absolute value function is working with all of that inside it, when it was originally declared with just "x." The only possibility I can think of is that this expression is treated as a single unit, but I'm not sure.

View 2 Replies View Related

C :: Function With Multiple Arguments

Jan 15, 2015

I am actually developing an nginx module in C.I am not to bad in C, but i got a big problem to pass argument to a vadiadic function.This function look like the well good old printf, but you put a buffer as first argument, the last address to stop to put data as second argument (in my case it is the last adress of disponible memory), a string that look like one in printf, an the other argument after.Here is the problem, the 4th last argument does not have the good value. In fact, It seem to be random value from memory. I Use gcc (Debian 4.9.1-19) 4.9.1.

Code:

/* ngx_html_log.h */
#ifndef NGX_HTML_LOG_H
#define NGX_HTML_LOG_H
#include <ngx_vasm.h>
}

[code]...

View 3 Replies View Related

C/C++ :: Pointer As Function Arguments

Dec 26, 2014

How this code work bcoz when pointer variable assigned in called function and how different values get as resultant output, ans 2 97 for below code. How the code wil execute so that i can validate ans

#include <stdio.h>
int main() {
int i = 97, *p = &i;
foo(&i);
printf("%d ", *p);

[Code] ....

View 6 Replies View Related

C++ :: Using Template Function Inside Class In Separate Function?

Mar 26, 2014

i want to use a class to print data stored as vector or array with different data types. i also want the print function two take more than one vector or array or combination of both so that they can be written to file as two columns. so i wrote the following class:

right now it has only one member function for printing two vectors. later i'll add additional functions as required.

note: there has to be template functions inside the class
i also want the object to be global so that i need not pass it as an argument to other calling functions

class printdata
{
public:
template<typename T1,typename T2>
void SaveData( vector<T1> &data1,vector<T2> &data2, std::string var)
{

[Code]....

then i want to call this template function in another ordinary function written in a seperate cpp file

these function declarations are put in a header file. so i need know whether i should put the declaration of the template function in the header to use the function in different functions

View 4 Replies View Related

C/C++ :: Using Template Function Inside A Class In Separate Function?

Mar 26, 2014

i want to use a class to print data stored as vector or array with different data types.

i also want the print function two take more than one vector or array or combination of both so that they can be written to file as two columns.so i wrote the following class:

right now it has only one member function for printing two vectors. later i'll add additional functions as required.

note: there has to be template functions inside the class / i also want the object to be global so that i need not pass it as an argument to other calling functions

class printdata {
public:
template<typename T1,typename T2>
void SaveData( vector<T1> &data1,vector<T2> &data2, std::string var){
std::ofstream myfile;
std::string filename;

[code].....

then i want to call this template function in another ordinary function written in a seperate cpp file these function declarations are put in a header file. so i need know whether i should put the declaration of the template function in the header to use the function in different functions.

View 1 Replies View Related

C :: Function Fails On Even Number Of Arguments

Nov 3, 2013

What I have is a main function that takes input characters from the command prompt during the main function call, and coverts it to an integer array a using atoi. (starting at the 2nd character - the 1st is reserved for another call that I plan to reference later, and the 0th is obviously the ./function). A function is then called to find the mode of an array (the range of values in the array is 1-30). Now, when I run the whole thing, I get a segmentation fault (core dumped) for even number of arguments. It's late and I've been staring at it for too long...

Code:
#include <stdio.h>
#include <stdlib.h>
int get_mode(int a[], int count);

[Code]......

View 1 Replies View Related

C :: How To Call Function With Correct Arguments

Sep 13, 2013

I'm not a programmer, at least, not a good one. I'm a researcher and I need to implement this function and test it and use it for my research. I tested some clustering methods in JAVA and Matlab and also I want to test it on C. I don't know too much about programming, especially about C, I know nothing. I tried to implement some basic methods but I failed.

It's all about K-Means Algorithm. I'm working on a disease and I'm trying to find ways to early diagnosis. Anyway, these are details. The thing is, I found a 'free to use' function but I don't know how can I use it. I tried to learn something from Net, I downloaded a compiler, I paste the code and I get many errors... And I heard that I have to do some "calling function" stuff but I don't know how to..

The code is in the link below: URL....It's not imperative that using this function, it can be another one but it had to written in C.

View 13 Replies View Related

C++ ::  Calculator Program - Function Does Not Take 0 Arguments

Jan 29, 2014

I am supposed to get 2 numbers then have the person choose what they want to do to the numbers. Here is the code:

//Mike Karbowiak
//12/24/13
//Mathematics
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>

[Code] ....

The errors I am getting so far are:

error C2660: 'Add' : function does not take 0 arguments
error C2660: 'Subtract' : function does not take 0 arguments
error C2660: 'Multiply' : function does not take 0 arguments
error C2660: 'Divide' : function does not take 0 arguments

View 3 Replies View Related

C++ :: Call Function With Arguments From Java

Sep 19, 2013

I am working on a project in a group at a university. We are creating a GUI in Java where the user can enter parameters. Then we want to be able to pass these parameters to a function inside our C/C++ program, that does the rest. How do I do this? So far I have managed to call a simple helloworld-function by using JNI, dll and a javah tool that create a header file from a java file. I define the helloworld-function in C and call it from Java.

Java file:

package helloworld;
public class HelloWorld {
private native void print();
public static void main(String[] args) {

[Code] ....

My problem is that I do not know how to call the helloworld-function with parameters. I guess that this is a special case when using JNI and a . dll. How do I pass simple char- and int-arguments from the Java class to the helloworld-C-function?

View 5 Replies View Related

C++ :: How To Calculate Number Of Arguments In A Function

Aug 19, 2013

i'm trying building my how Write() function:

template <typename T>
....
Write(T Argument1[,T ArgumentX])

how can i calculate the numeber of Arguments added? (in C we used an argument for tells how many we putted in a function, but not in these case)

View 2 Replies View Related

C++ :: Pow - No Overloaded Function Takes 1 Arguments

Oct 4, 2013

I keep getting this error in my code. I believe it is because to use pow(x,y) both x and y have to be double, but how do i put that into my formula under calculations?

#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main() {
// Declaration section: Declaring all variables.

[Code] ....

View 4 Replies View Related

C Sharp :: Procedure Or Function Has Too Many Arguments Specified

Apr 1, 2014

I have a button that calls a delete stored procedure with only two parameters. I can run the stored procedure via SQL manager and know it works. But, when I call the stored procedure from a sqldatasource on the .aspx page I get this error: Procedure or function has too many arguments specified.

Here is my code:

<asp:SqlDataSource runat="server" ID="sqlAuthorsInfo" ConnectionString="<%$ ConnectionStrings:Authors %>" 
ProviderName="<%$ ConnectionStrings:Authors.ProviderName %>" 
SelectCommand="FindAuthors" SelectCommandType="StoredProcedure" DeleteCommandType="StoredProcedure" 
DeleteCommand="Remove_Authors">
    <SelectParameters>

[Code] ....

Here is how the stored procedure looks - it is very simple:

DECLARE @authorcode int, @authorid int  
DELETE FROM Requests
WHERE authorcode = @authorcode
AND authorid = @authorid  

What is wrong? How do I resolve this???

View 1 Replies View Related

C++ :: Function Pointer Typedef With Same Type Arguments

Mar 6, 2015

I need to create the following brain damaging abomination:

I need a function pointer type to a function that has an argument of the same function pointer type and returns the same function pointer type.

The purpose is to enable a type of subroutine threading scheme for a small application specific scripting language. The question could just as well have been posted to the C forum.

This syntax works, but Payload is a generic type which I can coerce into the right pointer type via a cast. This is ugly IMHO. I could also hide it as a pointer in the FlipState class since I've forward declared this.

But this is an extra indirection in a performance critical part of the code, and also ugly.

Code:
class FlipState ;
typedef PayLoad (*FuncPtr) (FlipState *fs, PayLoad P) ; This syntax blows chunks using gcc on the other hand. Code: class FlipState ;
typedef FuncPtr (*FuncPtr) (FlipState *fs, FuncPtr P) ;

[Code] .....

This is hardly surprising. The compiler could not possibly understand what I was defining in the typedef. I think what I need is some kind of way to forward declare a function pointer type and then redefine it properly.

Is such a think even possible or am I just SOL? This one is mind boggling. We know how to do this with classes or other complex data types, but the syntax eludes me for both C++ and C.

View 4 Replies View Related

C :: How To Handle Enum Checks In Function Arguments

Aug 31, 2013

As we know in C there is no checking if values passed to a function that takes enum are correct, that is if they have been defined in this enum. Example from Enums in C | Occasionally sane ([code] tags don't work on my fx 18.0.1 this is why I put in on pb): [URL] ......

Here c - How to check if an enum variable is valid? - Stack Overflow they say that common convention is add check whether value passed as the parameter is not bigger than the maximum value in enum. But how about situations when enum is composed of numbers from 1-20 and then from 500-510?

View 4 Replies View Related

C :: Prototype For A Function Accepting Variable Arguments

Dec 6, 2014

what should be the prototype for the following function.

Code:void addition(int x, ...);

I am getting compilation errors. I have written the prototype as :

Code: void addition(int, va_list);

View 3 Replies View Related

C++ :: Left Over Arguments When Calling Function Pointers

May 5, 2014

Say I have a function pointer with this definition:

void ( *pressFunc ) ( void*, void* );

And i did this function:

void functionWithOneArg ( void* testPtr );

And i did this

pressFunc = &functionWithOneArg;

One. Would C actually let me do this? ( Assigning a function with one argument to a function with two )

Two. If so, what would happen to the second argument that is passed the function when its called? Does it just get 'cut off' and only the first argument is passed?

View 2 Replies View Related

C++ :: Creating A Variable Inside Function Arguments

May 15, 2013

In the following code:

#include <iostream> // For stream I/O
using namespace std;
int function(int a) {
return a;
}
int main() {
function(int b);
}

Why is creating a variable inside the function argument list not allowed. Any reason other then for the language syntax or just for the language syntax?

View 19 Replies View Related

C++ :: Using Pointers With Function Both As Arguments And Return Type

Jan 29, 2015

I always have confusions while using pointers with functions both as arguments and as return type.

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved