tags:

views:

106

answers:

3

<> - read this as a template;

I can do this:

void f() {}

//Here I'm declaring a fnc as a <> param
template<void (*fnc)()>
struct Factor { };

int main()
{
  Factor<f> fac;
  return 0;
}

but I cannot do this:

#include <sstream>

template<class R, class T>
R make_(T first, T second)
{
    std::stringstream interpreter;
    R result = R();
    interpreter << first << '.' << second;
    interpreter >> result;
    return result;
}

//Here I'm (trying) to declare fnc <> as a <> param
template<template<class T,class R> R (*fnc)(T,T)>
struct Factor { };

int main(int argc, char* argv[])
{
  Factor<make_> fac;
  return 0;
}

The BIG Q is: How (if possible) can I declare as a template parameter a fnc template?

Edit

Providing that I've used Armen's advise: I would like to be able to do something like this (in main):

Factor<f<"1","02">> m;

Then in m I could make a double type out of those args ("1", "02")

+1  A: 

There is no syntax for that in C++. What you should do is instead of function template use functor template, which would fit as a template template parameter.

E.G.

template <class R, class T>
struct f
{
    R operator () (T const&)
    {
        //blah
    }
};

template <template<class R, class T> class F >
struct foo
{
    ///...
};

int main()
{
    foo<f> d;
}
Armen Tsirunyan
@Armen yo my men, would you care to edit your answer with... ahem... appropriate example so my simple, murky brain can be enlightened?
There is nothing we can do
@There: yo yo ... done
Armen Tsirunyan
There is nothing we can do
@Armen could you plese update your answer and show how can I pass args to f::operator() ?
There is nothing we can do
@There: I don't quite understand what you mean. In your code I don't see where you pass args. Where do you want to pass args?
Armen Tsirunyan
@Armen I want to pass args to f
There is nothing we can do
@There: You create an instance of F, like F f; and call f(42).
Armen Tsirunyan
@Armen you mean create an instance of F inside foo? if that's how it is it won't work for me because I need to specify those args when creating foo.
There is nothing we can do
@There: I still don't understand what you want to do. Can you provide a (notcompilable) example of what you are trying to do? Including passing args?
Armen Tsirunyan
@Armen I'll update my original post.
There is nothing we can do
+1  A: 

Your syntax has some issues. What you do at the end with Factor<make_> fac; is similar to declaring vector<map> v; You need to provide parameters to the template to make it concrete: Factor<make_<int,int> > fac;. But that isn't the whole issue; there are many.

What you're doing with your function isn't quite right. You are providing a specific function (f in the first example), which can be done as a constructor parameter. You should reevaluate your design.

JoshD
Templates do more than "allow types". Quite a few template metaprogramming techniques depend on the flexibility of templates in this regard.
Harper Shelby
@Harper Shelby: yes, I wasn't clear about that. My point was that having a template for a function pointer is misguided. That type of thing can easily be done via a parameter to a constructor. If he were to provide a template parameter that could specify a functor class, that would be more proper (in my opinion).
JoshD
While I can see your syntax argument, suppose the OP wants to have `Factor` "remember" that template in order to allow it to call that? Seems like a worthy goal to me.
sbi
@sbi: I'm not sure I follow. Could you elaborate? Are you speaking about `Factor<make_>` specifically?
JoshD
@JoshD: Yes. Suppose `Factor` needs to invoke a function template with different template arguments. Which one should be fixed at compilation. That seems to be his use case.
sbi
@sbi that's exactly what I'm trying to do.
There is nothing we can do
A priori template templates are valid, you do *not* need to “provide parameters to the template to make it concrete.” However, perhaps this only works with class templates, not with function templates (I don’t know). With class templates, the syntax `template <template <typename T> class C>` would be correct, so OP’s attempt at least looks consistent.
Konrad Rudolph
@sbi, @There: I see. So it's much like simply having an templated function, but it is desired to have that held in Factor. Could you get the desired results by simply having a templated function? What is Factor meant to be used to do?
JoshD
@JoshD see my answer to GMan
There is nothing we can do
It sounds like you could accomplish what you want through constructor parameters to either your map or your policy. Have you explored that option? Or optionally, could you provide those values when you declare your map?
JoshD
@JoshD could you please provide an example how I'm suppose to pass a args to a Policy ctor? Thanks
There is nothing we can do
A: 

From looking at your make_() function template, it seems that what you actually want is boost::lexical_cast<>(). It does what your make_() does, only better. (For starters, your conversion doesn't check for errors at all.)

sbi