[iterator adaptor] requires default constructor?

It seems an iterator requires a default constructor in order to be adapted by iterator adaptor. This is due to (iterator_adaptor.hpp):279 iterator_adaptor() {} Is this really necessary?

On 6/11/2010 3:38 PM, Neal Becker wrote:
It seems an iterator requires a default constructor in order to be adapted by iterator adaptor.
This is due to (iterator_adaptor.hpp):279
iterator_adaptor() {}
Is this really necessary?
I believe if it never gets (explicitly or implicitly) instantiated, you're okay. E.g., on MSVC9, the following compiles, even though X< int& > isn't default constructible. #include <boost/call_traits.hpp> template< class T > struct X { X() { } X(typename boost::call_traits<T>::param_type x) : m_x(x) { } T m_x; }; int main(int argc, char* argv[]) { X<int> x; X< int& > y(x.m_x); return 0; } However, an explicit instantiation of X< int& > *will* generate a compiler error, but I imagine explicit instantiations of iterators are infrequent at best. All of the STL containers (AFAIK) declare a default constructor as iterator_adaptor does, regardless of their Allocator template parameter, even if the underlying allocator isn't default constructible, so it seems to be a rather pervasive situation. - Jeff

IIUC, in typical usage, the default constructor will be called. Here is my example: template<typename BaseIterator, typename offset_t> class cycle_iterator : public boost::iterator_adaptor<cycle_iterator<BaseIterator, offset_t>, BaseIterator > {... typedef typename boost::iterator_adaptor<cycle_iterator<BaseIterator, offset_t>, BaseIterator > super_t; explicit cycle_iterator (BaseIterator const& _b, BaseIterator const& _e, offset_t offset=0) : base(_b), size (std::distance (_b, _e)) { SetPos (offset); } ... BaseIterator base; }; This is a bit confusing. IIUC, the issue is that we did not explicitly invoke the super_t constructor, so we got the default constructor, which in turn calls (iterator_adaptor.hpp:280) iterator_adaptor() {} Adding an explicity super_t constructor call fixes it: explicit cycle_iterator (BaseIterator const& _b, BaseIterator const& _e, offset_t offset=0) : super_t (_b), base(_b), size (std::distance (_b, _e)) { SetPos (offset); } But is not very obvious.

On 6/11/2010 6:14 PM, Neal Becker wrote:
IIUC, in typical usage, the default constructor will be called. Here is my example: [...] This is a bit confusing. IIUC, the issue is that we did not explicitly invoke the super_t constructor, so we got the default constructor, which in turn calls (iterator_adaptor.hpp:280)
iterator_adaptor() {}
Adding an explicity super_t constructor call fixes it: [...] But is not very obvious.
How so? Makes sense to me that you should be conscious of how the base class is constructed (assuming it is nontrivial) in all the derived class' constructors, regardless of the context... - Jeff

Jeffrey Lee Hellrung, Jr. wrote:
On 6/11/2010 6:14 PM, Neal Becker wrote:
IIUC, in typical usage, the default constructor will be called. Here is my example: [...] This is a bit confusing. IIUC, the issue is that we did not explicitly invoke the super_t constructor, so we got the default constructor, which in turn calls (iterator_adaptor.hpp:280)
iterator_adaptor() {}
Adding an explicity super_t constructor call fixes it: [...] But is not very obvious.
How so? Makes sense to me that you should be conscious of how the base class is constructed (assuming it is nontrivial) in all the derived class' constructors, regardless of the context...
- Jeff
I see my confusion now. My iterator_adaptor was maintaining it's own base iterator, instead of just using the base() provided by super_t.

AMDG Neal Becker wrote:
It seems an iterator requires a default constructor in order to be adapted by iterator adaptor.
This is due to (iterator_adaptor.hpp):279
iterator_adaptor() {}
Is this really necessary?
I would have thought that this constructor wouldn't be instantiated if it weren't used. I believe that the standard requires a default constructor for Forward Iterators. In Christ, Steven Watanabe
participants (4)
-
David Abrahams
-
Jeffrey Lee Hellrung, Jr.
-
Neal Becker
-
Steven Watanabe