473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Default constructed random access iteratators

Hi,

I need to store of parts of containers. The interface to the store could
look something like this (the code in this post serves as illustrations,
it is not meant to be 100% correct):

template<class RanIt>
class substreams
{
public:
void add(RanIt begin, RanIt end);
};

and used like this:

std::vector<int> vector(20);
substreams<std::vector<int>::iterator> streams;
//...
streams.add(vector.begin(), vector.begin()+5);
streams.add(vector.begin()+5, vector.begin()+10);
streams.add(vector.begin()+15, vector.end());

where I'm adding parts of a vector to the store (streams).

Now, I know add() will be called at most k times, so I would like to
implement it like this:

template<class RanIt>
class substreams
{
std::pair<RanIt,RanIt>* parts_begin;
std::pair<RanIt,RanIt>* parts_end;
std::pair<RanIt,RanIt>* parts_last;
public:
substreams(size_t k)
: parts_begin(new std::pair<RanIt,RanIt>[k]),
parts_end(parts_begin),
parts_last(parts_begin+k) { }
virtual ~substreams()
{
delete[] parts_begin;
}

void add(RanIt begin, RanIt end)
{
*parts_end++ = std::make_pair(begin, end);
}
};

Suppose I want to count the total number of elements in the container
parts added. I could do something like this:

size_t size() const
{
size_t total = 0;
for( std::pair<RanIt,RanIt>* it=parts_begin;
it!=parts_last; ++it )
total += (it->second-it->first);
return total;
}

(strictly, I guess I should use typename
std::iterator_traits<RanIt>::difference_type instead of size_t). Now you
are thinking why not stop at it==parts_end? Let me just say the real
problem is not computing the size(), but something less trivial than a
for(...), so I would like to avoid involving parts_last. And that's the
problem:

Will the size() function as implemented above (using parts_last) always
work, even if add() has not been called k times (for the vector example,
size() should return 15 for all k>3)?
IIRC, the standard specifically takes care of the case where RanIt is a
pointer by requiring a default constructed pointer to be NULL, thereby
guaranteeing new std::pair<RanIt,RanIt>[k] evaluates to an array of
pairs of NULL pointers, in turn guaranteeing (it->second-it->first) is
zero for the pairs between parts_end and parts_last (those not added).
Correct me if I'm wrong.

But what if RanIt is not a pointer? As far as I can see the standard
only requires random access iterators to be default constructable but
states nothing about the semantics of such an iterator, specifically
nowhere that

template<class RanIt>
size_t diff()
{
RanIt b();
RanIt e();
return e-b;
}

should always return 0, or equivalently

template<class RanIt>
bool empty()
{
RanIt b();
RanIt e();
return e == b;
}

always returning true. Have I just missed something, or should I rethink
my implementation?

Thanks for your input,
Kristoffer Vinther
Jul 23 '05 #1
1 1764
Kristoffer Vinther wrote:
But what if RanIt is not a pointer? As far as I can see the standard
only requires random access iterators to be default constructable but
states nothing about the semantics of such an iterator, specifically
nowhere that

template<class RanIt>
size_t diff()
{
RanIt b();
RanIt e();
return e-b;
}

should always return 0, or equivalently

template<class RanIt>
bool empty()
{
RanIt b();
RanIt e();
return e == b;
}

always returning true. Have I just missed something, or should I rethink
my implementation?

You haven't missed anything. While the standard requires default
construction of an iterator to be legal, the use of that default
constructed value is undefined behavior. It neither compares to
0 or NULL nor deterministicly to any other object (including other
default constructed iterators).
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Dan Cernat | last post by:
Hi there, A few threads I had a little chat about default values. I am starting this thread because I want to hear more opinions about the default values of function parameters. Some say they...
13
by: DaKoadMunky | last post by:
I recently came across some code in a template that default constructed an object of type T to pass to another function... SomeFunction(T()); The code that instantiates that template specifies...
7
by: Paul | last post by:
Hi, I have a problem with the C++ spec 14.7.3.15 (see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14761 for relevant discussion). It seems that the code (below) is impossible to get right, as...
0
by: Clemens Hintze | last post by:
Hello, I have a question concerning the usage of default constructed std::slice instances. Our company currently validate the GNU-G++ 3.4 compiler against the ISO/IEC 14882:2003 standard for...
25
by: Mike MacSween | last post by:
Yes, I've downloaded and read the FAQ. And searched the archives. I'm just treading a little carefully here because of the caveats about locking myself out of the db forever. Some answers I could...
12
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include...
5
by: Diwa | last post by:
Does the "value" type (value as in key-value pair )of "std::map" require a default ctor even if it is not used ? If I comment out Line 1 in the code attached later, i.e remove the default ctor...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
8
by: t | last post by:
The stack container adaptor uses deque as its default underlying sequential container. I don't understand why since we only add elements and remove elements from one side of a stack. Why isn't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.