Re: Member function pointer template argument

From: Rolf Magnus (ramagnus_at_t-online.de)
Date: 03/20/04


Date: Sat, 20 Mar 2004 11:13:03 +0100

Michael Koehmstedt wrote:

> Hi,
> I have a class as such:
>
> template< typename T, T > class Foo;
>
> template< typename R, typename C, typename P1, R (C::*mfp)(P1) >
> class Foo< R (C::*)(P1), mfp >
> {
> };

What do you need the last template parameter for? Write:

template< typename R, typename C, typename P1)
class Foo
{
//...
private:
    R (C::* thepointer)(P1);
};

> My problem lies in the instantiation of the class. What I'd like to do
> is something like this, but it will not compile because func is a
> local variable:
>
> template< typename T >
> void makeFoo( T func )
> {
> Foo< T, func > foo;
> }

Right, func is a variable, but Foo expects a type as second template
parameter. Btw: please always include the error messages in your
postings.

> class Bar
> {
> void target( )
> {
> makeFoo( target );
> }
> };
>
> Basically, I want to be able to construct a Foo object without having
> to explicitly state the type of my member function.

Do it as I wrote above and add a constructor to Foo that takes the
function pointer. Then you should be able to just write:

class Bar
{
     void target( )
     {
         Foo foo(&target);
     }
};



Relevant Pages

  • Re: template and disambiguation
    ... > template keyword. ... The GCC example follows: ... the template and typename keywords are only need inside ... that the attempt to use a template parameter (or member of a template ...
    (comp.lang.cpp)
  • Re: Templates and Copy Constructors. Again.
    ... Now, what is it that is NOT a copy constructor, that has everyone so ... I read in the standard in 12.8 that a "template ... template <typename T> ... class Foo { ...
    (comp.lang.cpp)
  • template template argument from specialized instance
    ... template < typename T> ... void bar() ... My problem is, is there a way to call foo from bar, supposing that a ...
    (comp.lang.cpp)
  • Re: Template instanciation Problem
    ... template member inside the class: ... template <typename T> ... It has the unfortuante side effect of making foo inline, ...
    (comp.lang.cpp)
  • Re: OT: C++ Template Functions
    ... I'm currently trying to implement a C++ member function template, ... functions is only discriminated by return type (i.e., the template parameter ... template <typename U> ...
    (freebsd-hackers)