|
Boost Users : |
Subject: Re: [Boost-users] Conditional function definition with boost::enable_if
From: Matthieu Brucher (matthieu.brucher_at_[hidden])
Date: 2009-02-10 15:34:20
Hi,
Would it be possible to derive from a class that has this function?
This way, the mother class is template, the template argument being
the child class (the curiously recursive template pattern). Then, in
the mother class, you can define f() and call i(). I don't know how
i() would be exactly called, but you get the idea.
Matthieu
2009/2/10 Matthias Vallentin <vallentin_at_[hidden]>:
> Hi folks,
>
> I would like to define a function in a class only if a child defines it.
> In the below example, I would like the function f() to exist only in an
> object bar if it defines a function i(). So far, my approach with
> boost::enable_if failed because bar is not a complete type during
> examination. T is clearly incomplete at the time when b is defined.
> What would be the correct approach to solve this problem?
>
> #include <boost/type_traits/is_function.hpp>
> #include <boost/utility/enable_if.hpp>
>
> template <typename T>
> struct foo
> {
> typename boost::enable_if<
> typename boost::is_function<typename T::i>::type, void
> >::type
> f() { }
> };
>
> struct bar : foo<bar>
> {
> void i() { }
> };
>
> int main()
> {
> bar b;
> b.f(); // should only compile since bar defines i()
>
> return 0;
> }
>
> enable_if.cc: In instantiation of 'foo<bar>':
> enable_if.cc:16: instantiated from here
> enable_if.cc:10: error: invalid use of undefined type 'struct bar'
> enable_if.cc:16: error: forward declaration of 'struct bar'
> enable_if.cc: In function 'int main()':
> enable_if.cc:25: error: 'struct bar' has no member named 'f'
>
> Matthias
> --
> Matthias Vallentin
> vallentin_at_[hidden]
> http://matthias.vallentin.cc
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Information System Engineer, Ph.D. Website: http://matthieu-brucher.developpez.com/ Blogs: http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn: http://www.linkedin.com/in/matthieubrucher
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