[omniORB] String Sequence subscript operator

Steven W. Brenneis brennes1@rjrt.com
Wed, 20 Oct 1999 09:25:41 -0400


Is there a specific reason why the overloaded subscript operators for
_CORBA_Sequence__String return by value rather than by reference?  I
noted that this changed from version 2.7.1 to 2.8.0.  Here is the
problem it caused for us:

In our IDL:

typedef sequence<string> StringSequence;

In our implementation we restore a number of StringSequences from
persistent store using std::fstream.  Under 2.7.1 the code looked like
this:

StringSequence seq;
std::ifstream in;
...
int nCount;
in >> nCount;
seq.length(nCount);
for (int n = 0; n < seq.length(); ++n)
  in >> seq[n];

We provided overloaded insertion and extraction operators for
CORBA::String_member.

In version 2.8.0, MSVC allowed this, however Compaq, correctly so, did
not (the r-value is actually an unnamed temporary).  The following code
worked in some places on MSVC and Compaq, but not in others:

int nCount;
in >> nCount;
seq.length(nCount);
for (int n = 0; n < seq.length(); ++n){
  CORBA::String_member tmp(seq[n]);
  in >> tmp;
}

The final solution was as follows:

int nCount;
in >> nCount;
seq.length(nCount);
for (int n = 0; n < seq.length(); ++n){
  CORBA::String_member tmp;
  in >> tmp;
  seq[n] = tmp;
}

This is a lot of extra code to accomplish the end result.  The method
whereby the sequence element comes to contain the restored string can be
confusing since the actual assignment relies on an unsafe transfer of
char* ownership.

Thanks
Steve Brenneis