|
Boost : |
Subject: Re: [boost] enable_if and non-template member functions?
From: Jeffrey Lee Hellrung, Jr. (jeffrey.hellrung_at_[hidden])
Date: 2011-08-06 05:18:01
On Sat, Aug 6, 2011 at 1:38 AM, John Maddock <boost.regex_at_[hidden]> wrote:
> Folks, I have a problem with enable_if: it works just dandy for template
> member functions in my class, but I have some *non-template* member
> functions and constructors that I want to enable only if the template is
> instantiated with template args that meet certain criteria. Seems like I
> can't do this with enable_if as it then generates an invalid class
> definition. I guess I could use base classes, and/or partial
> specialization, but both involve a lot of code duplication I'd rather not
> have. Anyone any ideas?
>
One thing I've done in this situation is the following. Let's say we want
to provide 2 different definitions of a member function f within class X<T>,
depending on if T satisfies some metafunction cond. Then you can do
something like:
template< class T >
struct X
{
struct dummy_t { };
typedef /*...*/ f_arg_type; // f's argument type
typedef typename boost::mpl::if_< cond<T>, f_arg_type, dummy_t >::type
cond_true_f_arg_type;
typedef typename boost::mpl::if_< cond<T>, dummy_t, f_arg_type >::type
cond_false_f_arg_type;
void f(cond_true_f_arg_type) { /*...may assume T does satisfy cond...*/
}
void f(cond_false_f_arg_type) { /*...may assume T does not satisfy
cond...*/ }
};
I think this is fine as long as you don't declare explicit instantiations of
X (at least, I don't recall ever having any problems with this technique).
Is that kind of what you're looking for?
- Jeff
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk