Boost logo

Boost Users :

Subject: Re: [Boost-users] Conditional function definition with boost::enable_if
From: Matthias Vallentin (vallentin_at_[hidden])
Date: 2009-02-20 12:32:58


Hi Matthieu,

On Wed, Feb 11, 2009 at 09:22:47AM +0100, Matthieu Brucher wrote:
> template<class Child>
> struct MyfStruct
> {
> typename boost::enable_if<has_i<Child>::type, ...>::type f()
> {
> // Do some stuff, but I don't know how to call i(), you will have to test :|
> }
> };
>
> where has_i<> tests if the child has the method i().
>
> Then, if you find how to call i() from the f() method, you just have to write:
>
> class MyClass: public MyfStruct<MyClass>
> {
> void i(); // f() will be callable now
> };

Thanks for your example. Yet I believe, that the solution you propose does
not solve the underlying problem: as the compiler errors indicate,
struct bar (or in your example, class MyClass) is incomplete at the time
when has_i<Child> [*] is called. Hence, bar/MyClass cannot use CRTP to
automatically derive functions from foo/MyfStruct. I will have to use a
different approach to inject functions conditionally.

   Matthias

[*] This would factor out the code that checks whether T has i():

    template <typename T>
    struct has_i
      : boost::mpl::bool_<boost::is_function<typename T::i>::value> {};

-- 
Matthias Vallentin
vallentin_at_[hidden]
http://matthias.vallentin.cc

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