Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2007-11-02 23:18:58


on Wed Oct 31 2007, "Ovanes Markarian" <om_boost-AT-keywallet.com> wrote:

> Ok! Thanks! I got it... The problem is, that I thought that SFINAE would work from the template
> class in the template function which does not depend on the argument.
>
> is_const is a member of the traits_class which this type is derived from... Pretty much like:
>
> template<class T>
> struct traits_class
> {
> // some type calcs here
> typedef ... result_type;
> enum { is_const = false };
> };
>
> template<class T>
> struct traits_class<const T>
> {
> // some type calcs here
> typedef ... result_type;
> enum { is_const = true };
> };
>
> template<class T>
> struct some_class : traits_class<T>
> {
>
> template<class LookUpType_>
> typename boost::disable_if_c<is_const, result_type>::type
> at(LookUpType_ const& key)
> {
> return has_key(view_, key); //has key is some external function, which receives the
> result_type from the view_
> }
>
> };
>
> My idea was to enable only
> result_type at(LookUpType_ const& key) const;
> if T is const
>
> OR
> enable non-const version.
> result_type at(LookUpType_ const& key);
> if T is not const
>
> On the other hand Compiler does this for me automatically if T is const (but if T is non-const I
> would like to disable the const access).
>
> Anyway in that case I thought SFINAE should work, but it did not. Do
> I really need a type to be a template param of the member function,
> even if the class is a template itself?

No, it needs to be _dependent_ on a template parameter of the member
function. Here's an evil trick you can use:

template <bool b, class Ignored>
struct make_dependent
{
     enum { value = b };
};

template<class T>
struct some_class : traits_class<T>
{

  template<class LookUpType_>
       typename boost::disable_if_c<
           make_dependent<some_class::is_const, LookUpType_>::value,
           result_type
>::type
           at(LookUpType_ const& key)
       {
           return has_key(view_, key); //has key is some external function, which receives the
result_type from the view_
       }

};

Enjoy,

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

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