
David Abrahams wrote:
John Torjo <john.lists@torjo.com> writes:
Matthew Vogt wrote:
Like the following:
I guess it depends on how much you're willing to pay for syntactic sugar...
we're talking the same language ;) It seems that we posted our solutions at about the same time ;) My solution however does not involve any virtual call.
Calling through internally stored function pointers may be a minor improvement over using virtual functions, but that's not crystal clear to me. It seems likely that compilers might optimize away the virtual calls because all vtables are known to be strictly constant, but the other optimization seems less likely.
Just a late thought: what if we were to make the internal data of crange<> const? I guess this should give an extra hint to the compiler: template< class type> struct crange { protected: typedef crange<type> self_type; typedef void (*incrementer)(const self_type&); typedef bool (*at_end)(const self_type &); typedef type & (*deref)(const self_type&); crange( incrementer inc_func, at_end at_end_func, deref deref_func) : m_inc_func(inc_func), m_at_end_func(at_end_func), m_deref_func(deref_func) {} public: operator bool() const { return !m_at_end_func(*this); } const self_type & operator++() const { m_inc_func(*this); return *this; } type & operator*() const { return m_deref_func(*this); } private: const incrementer m_inc_func; const at_end m_at_end_func; const deref m_deref_func; }; What do you think? Best, John John Torjo Freelancer, builder.com C++ article writer -- mailto:john@torjo.com -- http://www.torjo.com/logview/EasyLogView.exe viewing/filtering logs can't get any easier than this!