Jump to content
  • Advertisement
Sign in to follow this  
hplus0603

[C++] Inspecting function pointer template argument

This topic is 4237 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to write a template like so:
void MyFunc();
double OtherFunc(double, double);

StuffWithFunc<&MyFunc> MyFuncStuff("stuff");
StuffWithFunc<&OtherFunc> OtherFuncStuff("other stuff");
Note that the instances MyFuncStuff and OtherFuncStuff that matter, so boost::bind won't do what I want. At least not on its own. However, I can't find a good way to make that happen, because I can't find a way to go between the literal and the type of the literal. I don't want to have to say something like:
StuffWithFunc<void,&MyFunc> MyFuncStuff;
StuffWithFunc<double,double,double,&OtherFunc> OtherFuncStuff;
I've tried reflecting through a function template that takes the function pointer (and make it a direct member instead of a type member), but I can't seem to be able to pick apart the function signature sufficiently.
template<typename T> BaseClass *MakeStuff(T func) {
  if (traits<func>::n_args == 0) {
    return new StuffWithFunc<typename traits<func>::ret_type>(func);
  }
  else if (traits<func>::n_args == 1) { 
    ...
  }
}
It might be possible to get that approach to work -- can someone show me how? It's pretty inelegant, though -- the top would be much better.

Share this post


Link to post
Share on other sites
Advertisement
Im not sure if what your after is actually possible, theres a similar problem in this thread but I dont think the technique used there would work in your situation. Theres a couple of other threads lying around with problems like this but I have to go so I cant dig them up right now, if I remember correctly the conclusion has always been that you need to feed the the function in twice, once to deduce the type and once to actually pass the function in but that isnt possible with the return type example you gave.

Share this post


Link to post
Share on other sites
I actually found a solution that works for me, by pairing a function template and a class template.

class Base { public: Base(char const *stuff) ... };

template<typename FuncType> class FuncBase {
public:
FuncBase(FuncType func, char *stuff) : Base(stuff), func_(func) {}
...
};

template<typename FuncType>
Base *NewFunc(FuncType func, char const *stuff) {
return new FuncBase<FuncType>(stuff);
}


Base &FuncInfo = *NewFunc(&Func, "func stuff");
Base &OtherInfo = *NewFunc(&Other, "other stuff");


I'll live with the memory "leak" as this is all allocated during static init (I don't think "leaks" happen during static init, if it's data that's used throughout your application). I could also use a smart pointer, I suppose.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!