|
Boost : |
From: Markus Werle (numerical.simulation_at_[hidden])
Date: 2003-07-03 05:53:28
Jaakko Jarvi wrote:
> Dear Boosters,
>
> The enable_if library defines the enable_if and disable_if templates,
> which are tools for controlling which function templates are included
> in the overload resolution set based on the properties of the argument
> types.
>
> The following example demonstrates their use:
>
> template <class T>
> typename enable_if<boost::is_arithmetic<T>::value, void>::type foo(T t) {
> std::cout << "An arithmetic type\n";
> }
>
> template <class T>
> typename disable_if<boost::is_arithmetic<T>::value, void>::type foo(T t) {
> std::cout << "Not an arithmetic type\n";
> }
Wow! C++ always has a solution for its own problems:
I always wondered how I could prevent Daixtrose template operators
get applied to other user's or builtin types and decided to put
template <class T1, class T2>
SomeComplicatedType operator+(T1 const &, T2 const &)
et. al. into their own namespace DefaultOps.
As of today Daixtrose users are explicitly forced to say
using namespace Daixt::DefaultOps if they want their code to compile.
And still may run into disambiguities when they include other beasts
of that kind. This is annoying.
With enable_if this can be reduced to say it once and for all times:
Users just have to specialize a traits class
template <class T> struct DaixtroseTraits
{
enum { use_default_ops = false };
};
and I change my operators to
template <class T1, class T2>
enable_if
<
(
(DaixtroseTraits<T1>::use_default_ops == true)
&&
(DaixtroseTraits<T2>::use_default_ops == true)
),
SomeComplicatedType
>::type
operator+(T1 const &, T2 const &);
and move them from namespace Daixtrose::Defaultops to namespace Daixtrose.
What do You think? Did I miss any side effects?
Thank You, Jaakko!
Markus
-- Build your own Expression Template Library with Daixtrose! Visit http://daixtrose.sourceforge.net/ (the sandbox for boost::etl?)
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk