Boost logo

Boost Users :

From: joaquin_at_[hidden]
Date: 2008-08-25 05:41:18


Hello, although this is not strictly a Boost-related question I wonder
whether
some of the utilities in Boost (enable_if, type_traits) can help me
solve the problem:

Say I've got the following template function

  template<typename T>
  void call_f(int x)
  {
    T t;
    t.f(x);
  }

whose purpose is to invoke t.f(x) on an object of type T:

  struct foo
  {
    void f(int){}
  };

  struct bar
  {
    static void f(int){};
  };

  ...

    call_f<foo>(5);
    call_f<bar>(5);

And now I'd like to optimize call_f's implementation by avoiding the
creation of the T object when it is not necessary (as in bar, whose memfun
f is static). The following will do for simple cases like foo and bar above:

  template<typename T,typename R,typename A>
  void call_f_impl(int x,R(*)(A))
  {
    T::f(x);
  }
 
  template<typename T,typename Q>
  void call_f_impl(int x,Q)
  {
    T t;
    t.f(x);
  }
 
  template<typename T>
  void call_f(int x)
  {
    call_f_impl<T>(x,&T::f);
  }

but fails when f is a memfun template:

  struct baz
  {
    template<typename T>
    void f(T){}
  };

  ...

  call_f<baz>(5);
 
For instance, Comeau says:

  "ComeauTest.c", line 17: error: no instance of overloaded function "call_f_impl"
            matches the argument list
              The argument types that you used are: (int, <unknown-type>)
      call_f_impl<T>(x,&T::f);
      ^
            detected during instantiation of "void call_f<T>(int) [with T=baz]"
                      at line 42
  
  1 error detected in the compilation of "ComeauTest.c".

Any idea about how to solve this in a more comprehensive way?

Thank you in advance,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net