baking with function pointers

Posted by HippieHunter Fri, 23 Jan 2009 00:41:00 GMT

One of the more interesting features in C++ template(IMHO) is being able to bake a compile time constant value directly into a template instance. One nice thing about function pointers is that they have a constant for an address. I'm not 100% sure what I can really use this for but it certainly looks cool! Of certain interest is template deduction on a function pointer, I was very surprised(pleasantly) to find this code compile.

#include<iostream>
#include<string>

using namespace std;

template<typename T, T (*F)(T)>
class rawrg
{
public:
  rawrg(T val)
  {
    F(val);
  }
};

template<typename T>
T method(T t)
{
  cout<< t << endl;
  return t;
}

int main()
{
  rawrg<const string&, &method> rawr("trivial");
  rawrg<float, &method> rawrg(5.5);
}

Trackbacks

Use the following link to trackback from your own site:
http://www.archverse.com/typo/trackbacks?article_id=6

Comments

Leave a comment