![]() |
| | #166 | |||||
| Registered User Join Date: Jan 2008
Posts: 645
| Quote:
Code: #if defined(__GNUC__)
#define TYPE_FRIENDLY(Type_t) friend class typify<Type_t>::type
#endif
#if defined(_MSC_VER)
#if (_MSC_VER >= 1300)
#define TYPE_FRIENDLY(Type_t) friend typename Type_t
#endif
#endif
Quote:
Quote:
Quote:
Quote:
Soma | |||||
| phantomotap is offline | |
| | #167 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,785
| Thanks for the insights. Quote:
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| ||
| Elysia is offline | |
| | #168 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| There's no need for anything templated here. The iterators only need to be friends of the specific instantiation they belong to. Code: template <typename T>
class foo
{
class inner;
friend class inner;
class inner
{
friend class foo<T>;
};
};
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #169 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,785
| Only, the iterators are free classes and not nested and do not belong to any specific class. It's supposed to be a generic iterator. On a side note, does Microsoft's implementation of STL really not care at all between the difference of constant iterators and non-constant iterators? Code: _OutIt __CLRCALL_OR_CDECL _Copy_backward_opt(_InIt _First, _InIt _Last, _OutIt _Dest,
random_access_iterator_tag, _Nonscalar_ptr_iterator_tag, _Range_checked_iterator_tag)
{ // copy [_First, _Last) backwards to [..., _Dest), arbitrary iterators
// if _OutIt is range checked, this will make sure there is enough space for the copy
// Last and first iterators are constant, while _Dest is not! _Last - _First will produce a new const_iterator,
// which in turn cannot be subtracted from a non-constant iterator.
_OutIt _Result = _Dest - (_Last - _First);
_Copy_backward_opt(_First, _Last, _CHECKED_BASE(_Dest),
forward_iterator_tag(), _Nonscalar_ptr_iterator_tag(), _Range_checked_iterator_tag());
return _Result;
} What to do about this...?
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #170 | ||
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| Quote:
Quote:
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law | ||
| CornedBee is offline | |
| | #171 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,785
| Yep, that's what I was after. Quote:
Although I don't remember why I cancelled the plans to add an overloaded operator - that returns a uint32_t (in32_t). That seems kind of stupid. An iterator shouldn't return an integer (or should it?). I have a Iterator::Difference non-member for that. To find the difference between two iterators (or how far apart they are). Perhaps my iterators are just too incompatible, in case I could need to transform them into an STL iterator...
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| ||
| Elysia is offline | |
| | #172 | |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| Quote:
The requirement is that for Iterator it1, it2; it1 - it2 == distance between the iterators and typeof(it1 - it2) == iterator_traits<Iterator>::difference_type.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law | |
| CornedBee is offline | |
| | #173 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,785
| Quote:
I think I'm just going to make a member to create an STL iterator from it since STL's iterators are just so inheritably unsafe. Not to mention they'll be incompatible with my algorithms anyway. So conversion to/from STL iterators may be necessary... I wonder if just constructing iterator/const_iterator and giving it a pointer will suffice?
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| ||
| Elysia is offline | |
| | #174 | |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| Quote:
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law | |
| CornedBee is offline | |
| | #175 | |
| Registered User Join Date: Jan 2008
Posts: 645
| Quote:
Of course, I may have misunderstood what he wants. Soma | |
| phantomotap is offline | |
| | #176 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,785
| Quote:
Say that CStringEx::resize is called and resizes the internal buffer. The function will now attempt to notify any iterators bound to it of this change and allow that iterator to update its members to reflect that. Another example is that CIterator::operator * is called. The iterator then attempts to notify the base class that "we are going to change this for this." The base class will know what to do. Another example is specialization of std::backward_copy and std::copy. Instead of doing *dst++ = *src++; It could call CIterator::backward_copy or CIterator::copy, which in turn would call CStringEx::backward_copy or CStringEx::copy and let it copy the entire range at once. Presumably, this communication is done by calling protected or private members. Therefore, the classes may need to be friends of each other or specific functions. But... Code: template<typename T> void foo(T class)
{
friend class T; // Doesn't work
}
By doing this, I could specify the class that acts as proxy and call public methods in that class. CIterator::copy calls CProxy::copy. CProxy::copy could then call CStringEx::copy. If I wanted different behaviour, I could make a new class, derive from CProxy and overload the functions where I wanted different behaviour. This also adds an advantage that I can keep communication in free functions. Like CIterator::copy would call CProxy::copy which would call ::copy_helper. This would work better with free functions. But regarding the iterator, I need to do: Code: std::copy_backward(vIndex, vCopyEnd, vCopyStart); Code: std::copy_backward(vIndex.GetSTLIterator(), vCopyEnd.GetSTLIterator(), vCopyStart.GetSTLIterator()); Code: std::iterator<type> it(m_pPos); return it; Code: Template typename std::basic_string<VecT>::const_iterator CConstIteratorTmpl::GetSTLIterator()
{
std::basic_string<VecT> t;
typename std::basic_string<VecT>::const_iterator it = t.begin();
it._Myptr = this->m_pCurPos;
return it;
}
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| ||
| Elysia is offline | |
| | #177 | |
| Registered User Join Date: Jan 2008
Posts: 645
| O_o Anyway, as for your iterator problems: your conceptual iterator implementation is a problem because it violates the nature of iterators... primarily because you seem to be trying too hard to avoid traditional iterators. Quote:
In all honesty, you need to provide for the minimum of a traditional bidirectional iterator implementation for your class first; then you can write your global iterator, your "not a range iterator, ranged iterator", "bidirectional observer iterator thing", and others in terms of the traditional iterator and container interfaces. What exactly is incompatible about the default iterator returned when you use 'begin()'--whatever it may be called? Soma | |
| phantomotap is offline | |
| | #178 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,785
| Here is the base class for my iterators: Code: template<typename VecT, typename VecTraits, typename Base, typename BaseIterator, typename DerefType> class CIteratorBase:
public boost::addable<CIteratorBaseTmpl, uint32_t>,
public boost::subtractable<CIteratorBaseTmpl, uint32_t>,
public boost::addable<CIteratorBaseTmpl>,
public boost::subtractable<CIteratorBaseTmpl>
//public std::iterator<std::random_access_iterator_tag, VecT>
{
public:
CIteratorBase();
CIteratorBase(DerefType* pStartPos, const VecT* pRangeStart, const VecT* pRangeEnd);
CIteratorBase(const CConstIterator<VecT, VecTraits, Base>& rSrc);
CIteratorBase(const CIterator<VecT, VecTraits, Base>& rSrc);
CIteratorBase& operator ++ ();
CIteratorBase operator ++ (int);
CIteratorBase& operator -- ();
CIteratorBase operator -- (int);
CIteratorBase& operator += (uint32_t nPos);
CIteratorBase& operator -= (uint32_t nPos);
CIteratorBase& operator += (const CIteratorBaseTmpl& vPos);
CIteratorBase& operator -= (const CIteratorBaseTmpl& vPos);
DerefType& operator * () const;
CIteratorValue<uint32_t, DerefType, VecTraits, Base, CIteratorBase, DerefType>* operator -> ();
bool valid();
friend bool operator < <> (const CIteratorBaseTmpl& rLeft, const CIteratorBaseTmpl& rRight);
friend bool operator == <> (const CIteratorBaseTmpl& rLeft, const CIteratorBaseTmpl& rRight);
friend bool operator > <> (const CIteratorBaseTmpl& rLeft, const CIteratorBaseTmpl& rRight);
friend bool operator != <> (const CIteratorBaseTmpl& rLeft, const CIteratorBaseTmpl& rRight);
CIteratorBase& operator = (const CIteratorBase& rSrc);
void SetSafe();
friend uint32_t Difference <> (const CIteratorBaseTmpl& v1, const CIteratorBaseTmpl& v2);
protected:
/*template<typename T> */void Copy(const CIteratorBaseTmpl& rObj);
DerefType* m_pCurPos;
const VecT* m_pRangeStart;
const VecT* m_pRangeEnd;
bool bUnsafe;
CIteratorValue<uint32_t, DerefType, VecTraits, Base, CIteratorBaseTmpl, DerefType> m_Value;
#ifdef _DEBUG
void DebugChecks() const;
#endif
};
Code: _OutIt _Result = _Dest - (_Last - _First); OK, so it might be fine, but again, it's mixing iterators. _Last and _First are constant. They are incompatible with _Dest which is non-constant. STL is incredibly unsafe. It completely disregards the const and assigns anyway. Even trying to fit an iterator into this design gives incredible amount of headaches. I didn't even want a const_iterator at first but I had to due to const functions. There I need to provide conversion. STL is working in extremely unsafe ways that just won't be compatible with my implementations, I can almost guarantee that. And since you asked... my iterator functions: Code: // Iterators typedef Iterators::CIterator<T, Traits, CTmplStringBase> iterator; typedef Iterators::CConstIterator<T, Traits, CTmplStringBase> const_iterator; iterator begin(); iterator end(); //iterator EndOfString(); const_iterator const_begin() const; const_iterator const_end() const;
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
Last edited by Elysia; 05-15-2008 at 09:10 AM. | |
| Elysia is offline | |
| | #179 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,372
| Quote:
EDIT: Gah, I hate this poor terminology. What you mean is: "_Last and _First are iterators to const objects. They are incompatible with _Dest which is an iterator to a non-const object. STL is incredibly unsafe. It completely disregards the const and assigns anyway." But of course, this is still nonsense. The difference between two (random access) iterators is a distance (an integer). Therefore, it does not matter whether they point to const or non-const objects, so long as they point to objects in the same range (or one past the end of the range). Subtracting an integer from a (random access) iterator results in an iterator that is further back in the range. This is all consistent with pointer arithmetic. However, by defining an operator- that does not follow the normal semantics for iterators, you have introduced an incompatibility. It is not the STL that is unsafe, but your code that is surprising (or even shocking).
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way Last edited by laserlight; 05-15-2008 at 09:27 AM. | |
| laserlight is offline | |
| | #180 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,785
| Well, nevermind. It makes your head boggle any way you look at it. I'm still unsure of the recommended solution, however. An iterator is not a pointer, but the STL seems to treat them like it. It's just incompatible with my current implementation.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| WS_POPUP, continuation of old problem | blurrymadness | Windows Programming | 1 | 04-20-2007 06:54 PM |
| Laptop Problem | Boomba | Tech Board | 1 | 03-07-2006 06:24 PM |
| implementation file | bejiz | C++ Programming | 5 | 11-28-2005 01:59 AM |
| Sorting problem.. well actually more of a string problem | fatdunky | C Programming | 5 | 11-07-2005 11:34 PM |
| Memory Problem - I think... | Unregistered | C Programming | 4 | 10-24-2001 12:14 PM |