A
alamaison
Hi all,
As rarely seen but variously recommended (see C++ Coding Standards,
Sutter and Alexandrescu for example) I'm trying to pass a vector of
items to a function that expects an array. The tricky part is that
the function expects the array to be passed half-open (pointer to a
[0], pointer to one-past-end) rather that &a[0] and a size. This
means that I'm trying to do the following:
vector<item> files;
....fill vector...
pobject->Init(&files[0], &files[0] + files.size(), ...);
This works fine unless the vector happens to be empty which, given
that this function initialises a type of domain-specific collection,
is perfectly reasonable. &files[0] triggers the STL debugging with an
out-of-bounds error. The Init function itself has no problem with the
situation. If it receives two identical pointers it simple creates an
empty collection.
Now, obviously, I can hack around this problem by passing Init a
pointer to anything twice if files.empty() but I would like to know
the 'proper' C++ way to do this. Can it be done gracefully?
Many thanks.
Alex Lamaison
-- http://swish.sourceforge.net
As rarely seen but variously recommended (see C++ Coding Standards,
Sutter and Alexandrescu for example) I'm trying to pass a vector of
items to a function that expects an array. The tricky part is that
the function expects the array to be passed half-open (pointer to a
[0], pointer to one-past-end) rather that &a[0] and a size. This
means that I'm trying to do the following:
vector<item> files;
....fill vector...
pobject->Init(&files[0], &files[0] + files.size(), ...);
This works fine unless the vector happens to be empty which, given
that this function initialises a type of domain-specific collection,
is perfectly reasonable. &files[0] triggers the STL debugging with an
out-of-bounds error. The Init function itself has no problem with the
situation. If it receives two identical pointers it simple creates an
empty collection.
Now, obviously, I can hack around this problem by passing Init a
pointer to anything twice if files.empty() but I would like to know
the 'proper' C++ way to do this. Can it be done gracefully?
Many thanks.
Alex Lamaison
-- http://swish.sourceforge.net