C++ pass function with arguments as template2019 Community Moderator ElectionStoring C++ template function definitions in a .CPP fileWhat are the differences between a pointer variable and a reference variable in C++?Storing C++ template function definitions in a .CPP fileHow can I profile C++ code running on Linux?The Definitive C++ Book Guide and ListWhy can templates only be implemented in the header file?Where and why do I have to put the “template” and “typename” keywords?What is the effect of extern “C” in C++?What is the “-->” operator in C++?Why do we need virtual functions in C++?Why is reading lines from stdin much slower in C++ than Python?

From an axiomatic set theoric approach why can we take uncountable unions?

Would an aboleth's Phantasmal Force lair action be affected by Counterspell, Dispel Magic, and/or Slow?

Having the player face themselves after the mid-game

Plausibility of Mushroom Buildings

Is it possible that a question has only two answers?

I can't die. Who am I?

Specifying a starting column with colortbl package and xcolor

What is Earthy controling in the ISS cupola?

Does "Until when" sound natural for native speakers?

How does Ehrenfest's theorem apply to the quantum harmonic oscillator?

Trig Subsitution When There's No Square Root

In the late 1940’s to early 1950’s what technology was available that could melt a LOT of ice?

Is it safe to abruptly remove Arduino power?

Proving a statement about real numbers

Haman going to the second feast dirty

Do I really need to have a scientific explanation for my premise?

Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?

How exactly does an Ethernet collision happen in the cable, since nodes use different circuits for Tx and Rx?

Power Strip for Europe

Windows Server Datacenter Edition - Unlimited Virtual Machines

What problems would a superhuman have who's skin is constantly hot?

Is it possible to find 2014 distinct positive integers whose sum is divisible by each of them?

How to draw dashed arc of a circle behind pyramid?

Can we track matter through time by looking at different depths in space?



C++ pass function with arguments as template



2019 Community Moderator ElectionStoring C++ template function definitions in a .CPP fileWhat are the differences between a pointer variable and a reference variable in C++?Storing C++ template function definitions in a .CPP fileHow can I profile C++ code running on Linux?The Definitive C++ Book Guide and ListWhy can templates only be implemented in the header file?Where and why do I have to put the “template” and “typename” keywords?What is the effect of extern “C” in C++?What is the “-->” operator in C++?Why do we need virtual functions in C++?Why is reading lines from stdin much slower in C++ than Python?










0















i got a Problem with a template
I created a generic class whcih stores global available Information.
This class holds a private mutex to manage the access to the global info.



template<typename Mutex_Type_T, typename Struct_Type_T>
class CGlobal_Struct

public:
/**
* Exports data from this class to target
* @param target the actual target
* @param mutex_timeout Mutex wait time
* @return true in case of success
*/
bool Export(Struct_Type_T& target, const uint32_t mutex_timeout = 100);
/**
* Imports data to this class
* @param source The data to store in this class
* @param mutex_timeout Wait Time for Mutex
* @return true in case of success
*/
bool Import(const Struct_Type_T& source, const uint32_t mutex_timeout = 100);
/**
* 1) Loads Data to Buffer
* 2) performs user defined Operation by calling func_T(data, args)
* 3) stores back the data
* @param User defined function
* @param values class data to modify
* @param mutex_timeout Mutex wait time
* @return true in case of success
*/
template<typename func_T, typename func_arg_t>
bool Replace(func_T(Struct_Type_T& values, const func_arg_t args), const func_arg_t func_args,const uint32_t mutex_timeout = 100);

private:
mutex _mutex;



This implementation Looks like this



template<typename Mutex_Type_T, typename Struct_Type_T>
template<typename func_T, typename func_arg_t>
bool CGlobal_Struct<Mutex_Type_T, Struct_Type_T>::Replace(func_T(Struct_Type_T& values, const func_arg_t args),const func_arg_t func_args, const uint32_t mutex_timeout)

CLock_Guard lock(mutex);

//Lock access
if(false == lock.Lock(mutex_timeout))

//Locking failed
return false;

//Replace Data
func_T(data, func_args);

//Mutex is released automatically when we leave this function
return true;



Now first Question: is this template implementation correct?



Second: How would i call this replacement function from outside this class?
Could you give me some help please?










share|improve this question






















  • Possible duplicate of Storing C++ template function definitions in a .CPP file

    – Samer Tufail
    Mar 6 at 14:49











  • Now first Question: is this template implementation correct? Does it compile? Did you tried calling it?

    – Guillaume Racicot
    Mar 6 at 14:50











  • Your comment say "calling func_T(data, args)" but in your template, func_T is a typename, not a functor.

    – Raymond Chen
    Mar 6 at 14:52















0















i got a Problem with a template
I created a generic class whcih stores global available Information.
This class holds a private mutex to manage the access to the global info.



template<typename Mutex_Type_T, typename Struct_Type_T>
class CGlobal_Struct

public:
/**
* Exports data from this class to target
* @param target the actual target
* @param mutex_timeout Mutex wait time
* @return true in case of success
*/
bool Export(Struct_Type_T& target, const uint32_t mutex_timeout = 100);
/**
* Imports data to this class
* @param source The data to store in this class
* @param mutex_timeout Wait Time for Mutex
* @return true in case of success
*/
bool Import(const Struct_Type_T& source, const uint32_t mutex_timeout = 100);
/**
* 1) Loads Data to Buffer
* 2) performs user defined Operation by calling func_T(data, args)
* 3) stores back the data
* @param User defined function
* @param values class data to modify
* @param mutex_timeout Mutex wait time
* @return true in case of success
*/
template<typename func_T, typename func_arg_t>
bool Replace(func_T(Struct_Type_T& values, const func_arg_t args), const func_arg_t func_args,const uint32_t mutex_timeout = 100);

private:
mutex _mutex;



This implementation Looks like this



template<typename Mutex_Type_T, typename Struct_Type_T>
template<typename func_T, typename func_arg_t>
bool CGlobal_Struct<Mutex_Type_T, Struct_Type_T>::Replace(func_T(Struct_Type_T& values, const func_arg_t args),const func_arg_t func_args, const uint32_t mutex_timeout)

CLock_Guard lock(mutex);

//Lock access
if(false == lock.Lock(mutex_timeout))

//Locking failed
return false;

//Replace Data
func_T(data, func_args);

//Mutex is released automatically when we leave this function
return true;



Now first Question: is this template implementation correct?



Second: How would i call this replacement function from outside this class?
Could you give me some help please?










share|improve this question






















  • Possible duplicate of Storing C++ template function definitions in a .CPP file

    – Samer Tufail
    Mar 6 at 14:49











  • Now first Question: is this template implementation correct? Does it compile? Did you tried calling it?

    – Guillaume Racicot
    Mar 6 at 14:50











  • Your comment say "calling func_T(data, args)" but in your template, func_T is a typename, not a functor.

    – Raymond Chen
    Mar 6 at 14:52













0












0








0








i got a Problem with a template
I created a generic class whcih stores global available Information.
This class holds a private mutex to manage the access to the global info.



template<typename Mutex_Type_T, typename Struct_Type_T>
class CGlobal_Struct

public:
/**
* Exports data from this class to target
* @param target the actual target
* @param mutex_timeout Mutex wait time
* @return true in case of success
*/
bool Export(Struct_Type_T& target, const uint32_t mutex_timeout = 100);
/**
* Imports data to this class
* @param source The data to store in this class
* @param mutex_timeout Wait Time for Mutex
* @return true in case of success
*/
bool Import(const Struct_Type_T& source, const uint32_t mutex_timeout = 100);
/**
* 1) Loads Data to Buffer
* 2) performs user defined Operation by calling func_T(data, args)
* 3) stores back the data
* @param User defined function
* @param values class data to modify
* @param mutex_timeout Mutex wait time
* @return true in case of success
*/
template<typename func_T, typename func_arg_t>
bool Replace(func_T(Struct_Type_T& values, const func_arg_t args), const func_arg_t func_args,const uint32_t mutex_timeout = 100);

private:
mutex _mutex;



This implementation Looks like this



template<typename Mutex_Type_T, typename Struct_Type_T>
template<typename func_T, typename func_arg_t>
bool CGlobal_Struct<Mutex_Type_T, Struct_Type_T>::Replace(func_T(Struct_Type_T& values, const func_arg_t args),const func_arg_t func_args, const uint32_t mutex_timeout)

CLock_Guard lock(mutex);

//Lock access
if(false == lock.Lock(mutex_timeout))

//Locking failed
return false;

//Replace Data
func_T(data, func_args);

//Mutex is released automatically when we leave this function
return true;



Now first Question: is this template implementation correct?



Second: How would i call this replacement function from outside this class?
Could you give me some help please?










share|improve this question














i got a Problem with a template
I created a generic class whcih stores global available Information.
This class holds a private mutex to manage the access to the global info.



template<typename Mutex_Type_T, typename Struct_Type_T>
class CGlobal_Struct

public:
/**
* Exports data from this class to target
* @param target the actual target
* @param mutex_timeout Mutex wait time
* @return true in case of success
*/
bool Export(Struct_Type_T& target, const uint32_t mutex_timeout = 100);
/**
* Imports data to this class
* @param source The data to store in this class
* @param mutex_timeout Wait Time for Mutex
* @return true in case of success
*/
bool Import(const Struct_Type_T& source, const uint32_t mutex_timeout = 100);
/**
* 1) Loads Data to Buffer
* 2) performs user defined Operation by calling func_T(data, args)
* 3) stores back the data
* @param User defined function
* @param values class data to modify
* @param mutex_timeout Mutex wait time
* @return true in case of success
*/
template<typename func_T, typename func_arg_t>
bool Replace(func_T(Struct_Type_T& values, const func_arg_t args), const func_arg_t func_args,const uint32_t mutex_timeout = 100);

private:
mutex _mutex;



This implementation Looks like this



template<typename Mutex_Type_T, typename Struct_Type_T>
template<typename func_T, typename func_arg_t>
bool CGlobal_Struct<Mutex_Type_T, Struct_Type_T>::Replace(func_T(Struct_Type_T& values, const func_arg_t args),const func_arg_t func_args, const uint32_t mutex_timeout)

CLock_Guard lock(mutex);

//Lock access
if(false == lock.Lock(mutex_timeout))

//Locking failed
return false;

//Replace Data
func_T(data, func_args);

//Mutex is released automatically when we leave this function
return true;



Now first Question: is this template implementation correct?



Second: How would i call this replacement function from outside this class?
Could you give me some help please?







c++ templates






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 6 at 14:46









JHeniJHeni

134




134












  • Possible duplicate of Storing C++ template function definitions in a .CPP file

    – Samer Tufail
    Mar 6 at 14:49











  • Now first Question: is this template implementation correct? Does it compile? Did you tried calling it?

    – Guillaume Racicot
    Mar 6 at 14:50











  • Your comment say "calling func_T(data, args)" but in your template, func_T is a typename, not a functor.

    – Raymond Chen
    Mar 6 at 14:52

















  • Possible duplicate of Storing C++ template function definitions in a .CPP file

    – Samer Tufail
    Mar 6 at 14:49











  • Now first Question: is this template implementation correct? Does it compile? Did you tried calling it?

    – Guillaume Racicot
    Mar 6 at 14:50











  • Your comment say "calling func_T(data, args)" but in your template, func_T is a typename, not a functor.

    – Raymond Chen
    Mar 6 at 14:52
















Possible duplicate of Storing C++ template function definitions in a .CPP file

– Samer Tufail
Mar 6 at 14:49





Possible duplicate of Storing C++ template function definitions in a .CPP file

– Samer Tufail
Mar 6 at 14:49













Now first Question: is this template implementation correct? Does it compile? Did you tried calling it?

– Guillaume Racicot
Mar 6 at 14:50





Now first Question: is this template implementation correct? Does it compile? Did you tried calling it?

– Guillaume Racicot
Mar 6 at 14:50













Your comment say "calling func_T(data, args)" but in your template, func_T is a typename, not a functor.

– Raymond Chen
Mar 6 at 14:52





Your comment say "calling func_T(data, args)" but in your template, func_T is a typename, not a functor.

– Raymond Chen
Mar 6 at 14:52












1 Answer
1






active

oldest

votes


















0















Now first Question: is this template implementation correct?




Does it compile when you try calling it?




Second: How would i call this replacement function from outside this class?




The template arguments are deduced from a function pointer. Where func_T is the return type. I would advice against it and recommend a simpler template parameter:



template<typename Struct_Type_T>
struct CGlobal_Struct

template<typename func_T, typename func_arg_t>
bool Replace(func_T function, const func_arg_t func_args,const uint32_t mutex_timeout = 100)
// function(struct_type_value, func_args);

;


Both version are valid and are called like this:



struct A ;

void func(A&, int)

int main()
CGlobal_Struct<A> glob;
glob.Replace(func, 1);



The version I recommend can also support lambdas:



glob.Replace([](A&, int) /* code */ , 1);



Reading your implementation, it won't work with your current version:



//Replace Data
func_T(data, func_args);


The problem it that func_T is the type of the return type of the function sent as parameter. It is the same as this:



void(data, func_args);


Or more evil:



struct evil evil(A&, int) ;

// ...

evil(data, func_args);


This will call the constructor of evil and never call a function.



It you look closely, your parameter has no name:



 bool Replace(
func_T(Struct_Type_T&, const func_arg_t),
/* other params */ );


To give it a name, the sytax would be:



 bool Replace(
func_T(*function)(Struct_Type_T&, const func_arg_t),
/* other params */ );


Then call function(...)






share|improve this answer

























  • Could you help me out: What does this: glob.Replace([](A&, int) /* code */ , 1); mean? What are the []? The stuff in () is probably the types of both Arguments Right?

    – JHeni
    Mar 7 at 8:16











  • @JHeni Search for lambda functions

    – Guillaume Racicot
    Mar 7 at 13:35










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55025810%2fc-pass-function-with-arguments-as-template%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0















Now first Question: is this template implementation correct?




Does it compile when you try calling it?




Second: How would i call this replacement function from outside this class?




The template arguments are deduced from a function pointer. Where func_T is the return type. I would advice against it and recommend a simpler template parameter:



template<typename Struct_Type_T>
struct CGlobal_Struct

template<typename func_T, typename func_arg_t>
bool Replace(func_T function, const func_arg_t func_args,const uint32_t mutex_timeout = 100)
// function(struct_type_value, func_args);

;


Both version are valid and are called like this:



struct A ;

void func(A&, int)

int main()
CGlobal_Struct<A> glob;
glob.Replace(func, 1);



The version I recommend can also support lambdas:



glob.Replace([](A&, int) /* code */ , 1);



Reading your implementation, it won't work with your current version:



//Replace Data
func_T(data, func_args);


The problem it that func_T is the type of the return type of the function sent as parameter. It is the same as this:



void(data, func_args);


Or more evil:



struct evil evil(A&, int) ;

// ...

evil(data, func_args);


This will call the constructor of evil and never call a function.



It you look closely, your parameter has no name:



 bool Replace(
func_T(Struct_Type_T&, const func_arg_t),
/* other params */ );


To give it a name, the sytax would be:



 bool Replace(
func_T(*function)(Struct_Type_T&, const func_arg_t),
/* other params */ );


Then call function(...)






share|improve this answer

























  • Could you help me out: What does this: glob.Replace([](A&, int) /* code */ , 1); mean? What are the []? The stuff in () is probably the types of both Arguments Right?

    – JHeni
    Mar 7 at 8:16











  • @JHeni Search for lambda functions

    – Guillaume Racicot
    Mar 7 at 13:35















0















Now first Question: is this template implementation correct?




Does it compile when you try calling it?




Second: How would i call this replacement function from outside this class?




The template arguments are deduced from a function pointer. Where func_T is the return type. I would advice against it and recommend a simpler template parameter:



template<typename Struct_Type_T>
struct CGlobal_Struct

template<typename func_T, typename func_arg_t>
bool Replace(func_T function, const func_arg_t func_args,const uint32_t mutex_timeout = 100)
// function(struct_type_value, func_args);

;


Both version are valid and are called like this:



struct A ;

void func(A&, int)

int main()
CGlobal_Struct<A> glob;
glob.Replace(func, 1);



The version I recommend can also support lambdas:



glob.Replace([](A&, int) /* code */ , 1);



Reading your implementation, it won't work with your current version:



//Replace Data
func_T(data, func_args);


The problem it that func_T is the type of the return type of the function sent as parameter. It is the same as this:



void(data, func_args);


Or more evil:



struct evil evil(A&, int) ;

// ...

evil(data, func_args);


This will call the constructor of evil and never call a function.



It you look closely, your parameter has no name:



 bool Replace(
func_T(Struct_Type_T&, const func_arg_t),
/* other params */ );


To give it a name, the sytax would be:



 bool Replace(
func_T(*function)(Struct_Type_T&, const func_arg_t),
/* other params */ );


Then call function(...)






share|improve this answer

























  • Could you help me out: What does this: glob.Replace([](A&, int) /* code */ , 1); mean? What are the []? The stuff in () is probably the types of both Arguments Right?

    – JHeni
    Mar 7 at 8:16











  • @JHeni Search for lambda functions

    – Guillaume Racicot
    Mar 7 at 13:35













0












0








0








Now first Question: is this template implementation correct?




Does it compile when you try calling it?




Second: How would i call this replacement function from outside this class?




The template arguments are deduced from a function pointer. Where func_T is the return type. I would advice against it and recommend a simpler template parameter:



template<typename Struct_Type_T>
struct CGlobal_Struct

template<typename func_T, typename func_arg_t>
bool Replace(func_T function, const func_arg_t func_args,const uint32_t mutex_timeout = 100)
// function(struct_type_value, func_args);

;


Both version are valid and are called like this:



struct A ;

void func(A&, int)

int main()
CGlobal_Struct<A> glob;
glob.Replace(func, 1);



The version I recommend can also support lambdas:



glob.Replace([](A&, int) /* code */ , 1);



Reading your implementation, it won't work with your current version:



//Replace Data
func_T(data, func_args);


The problem it that func_T is the type of the return type of the function sent as parameter. It is the same as this:



void(data, func_args);


Or more evil:



struct evil evil(A&, int) ;

// ...

evil(data, func_args);


This will call the constructor of evil and never call a function.



It you look closely, your parameter has no name:



 bool Replace(
func_T(Struct_Type_T&, const func_arg_t),
/* other params */ );


To give it a name, the sytax would be:



 bool Replace(
func_T(*function)(Struct_Type_T&, const func_arg_t),
/* other params */ );


Then call function(...)






share|improve this answer
















Now first Question: is this template implementation correct?




Does it compile when you try calling it?




Second: How would i call this replacement function from outside this class?




The template arguments are deduced from a function pointer. Where func_T is the return type. I would advice against it and recommend a simpler template parameter:



template<typename Struct_Type_T>
struct CGlobal_Struct

template<typename func_T, typename func_arg_t>
bool Replace(func_T function, const func_arg_t func_args,const uint32_t mutex_timeout = 100)
// function(struct_type_value, func_args);

;


Both version are valid and are called like this:



struct A ;

void func(A&, int)

int main()
CGlobal_Struct<A> glob;
glob.Replace(func, 1);



The version I recommend can also support lambdas:



glob.Replace([](A&, int) /* code */ , 1);



Reading your implementation, it won't work with your current version:



//Replace Data
func_T(data, func_args);


The problem it that func_T is the type of the return type of the function sent as parameter. It is the same as this:



void(data, func_args);


Or more evil:



struct evil evil(A&, int) ;

// ...

evil(data, func_args);


This will call the constructor of evil and never call a function.



It you look closely, your parameter has no name:



 bool Replace(
func_T(Struct_Type_T&, const func_arg_t),
/* other params */ );


To give it a name, the sytax would be:



 bool Replace(
func_T(*function)(Struct_Type_T&, const func_arg_t),
/* other params */ );


Then call function(...)







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 6 at 15:07

























answered Mar 6 at 15:01









Guillaume RacicotGuillaume Racicot

14.9k53468




14.9k53468












  • Could you help me out: What does this: glob.Replace([](A&, int) /* code */ , 1); mean? What are the []? The stuff in () is probably the types of both Arguments Right?

    – JHeni
    Mar 7 at 8:16











  • @JHeni Search for lambda functions

    – Guillaume Racicot
    Mar 7 at 13:35

















  • Could you help me out: What does this: glob.Replace([](A&, int) /* code */ , 1); mean? What are the []? The stuff in () is probably the types of both Arguments Right?

    – JHeni
    Mar 7 at 8:16











  • @JHeni Search for lambda functions

    – Guillaume Racicot
    Mar 7 at 13:35
















Could you help me out: What does this: glob.Replace([](A&, int) /* code */ , 1); mean? What are the []? The stuff in () is probably the types of both Arguments Right?

– JHeni
Mar 7 at 8:16





Could you help me out: What does this: glob.Replace([](A&, int) /* code */ , 1); mean? What are the []? The stuff in () is probably the types of both Arguments Right?

– JHeni
Mar 7 at 8:16













@JHeni Search for lambda functions

– Guillaume Racicot
Mar 7 at 13:35





@JHeni Search for lambda functions

– Guillaume Racicot
Mar 7 at 13:35



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55025810%2fc-pass-function-with-arguments-as-template%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко