|
Boost : |
From: Fernando Cacciola (fernando.cacciola_at_[hidden])
Date: 2008-05-02 15:14:09
Hi Luke,
>
> The following does not compile (I wish it did):
>
> const orientation_2d horizontal_constant;
>
> template <orientation2d orient>
> coordinate get(const point& pt) { return coord_[orient.to_int()]; }
>
> coordinate value = get<horizontal_constant>(my_point);
>
> because orient has to be a built in type.
>
Acutally, that to_int() is getting back into runtime, so this wouldn't count
as a compile-time access even if it compiled.
OTOH, this could be made to compile if desired:
struct horizontal{ static const int value = 0 ; }
template <class orient>
coordinate get(const point& pt) { return pt[orient::value]; }
coordinate value = get<horizontal>(my_point);
Having said that, if the library aims to educate users not to do something
as terrible as
if(condition)
point.x() = value;
else
point.y() = value;
then it should advocate higher level abstractions:
vector delta = condition ? vector(value,0) : vector(0,value) {}
translate modify (delta);
point = modify(point);
which, being essentially coordinate-free, hides the coordinate-access issue
from the user pushing into the library implementation.
Best
-- Fernando Cacciola SciSoft http://scisoft-consulting.com http://fcacciola.50webs.com http://groups.google.com/group/cppba
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk