Boost logo

Boost :

From: Simonson, Lucanus J (lucanus.j.simonson_at_[hidden])
Date: 2008-05-06 13:42:30


I found a way to work around the compiler error for wrapping compile
time accessors defined in traits classes.

//demo point type
struct point_data {
        int coords_[2];
        int get(int orient) const { return coords_[orient]; }
        typedef int coordinate_type;
};

template <typename T>
struct point_traits {
  point_traits() {} //needs a default constructor now
  typedef typename T::coordinate_type coordinate_type;
  template<int orient>
  static inline coordinate_type get(const T& point) {
    return point.get(orient);
  }
};

//would not compile
//template <int orient, typename T>
//typename point_traits<T>::coordinate_type get(const T& point) {
// return point_traits<T>::get<orient>(point);
//}

//will compile when written this way
template <int orient, typename T>
typename point_traits<T>::coordinate_type get(const T& point) {
        point_traits<T> traits_instance;
        return traits_instance.get<orient>(point);
}

int main() {
        point_data pt;
        //this line compiles
        int value0 = point_traits<point_data>::get<0>(pt);
        //this line now compiles
        int value = get<0>(pt);
        return 0;
}

But it still bugs me that I don't know why the compiler thought that
point_traits<T>::get was a function pointer and tried to apply the
operator< with orient, but does the right thing when I instantiate the
traits struct and use the . operator to access the same function. The .
operator could also produce a function pointer.

Thanks,
Luke


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk