D
duckfreezone
Hi,
I've got a small test program which behaves correctly (IMHO) on all
compilers that I have acccess to, with the exception of the gcc on
Macintosh OSX "gcc version 4.0.0 (Apple Computer, Inc. build 5026)"
Looks to be that the order of evaluation of the assignment on OSX is
different to other systems. I *think* it is wrong on OSX, or am I
simply relying on undefined behaviour?
On a "correct" system, I get this output:
before:
0 members
after:
0 : 'something'='something extra'
1 members
On OSX, I get this output:
before:
0 members
after:
0 : 'something'=''
1 members
I appreciate that this newsgroup doesn't cover platform-specific
issues, but my question is more one of trying to understand if this
program is relying on undefined behaviour.
Source follows:
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
static std::map<std::string,std::string> strings;
static void dump (const std::string& prefix)
{
std::cout << prefix << ":" << std::endl;
unsigned int count = 0;
for (std::map<std::string,std::string>::const_iterator si =
strings.begin (); si != strings.end (); ++si)
{
const std::string s1 (si -> first);
const std::string s2 (si -> second);
std::cout << " " << count++ << " : '" << s1 << "'='" << s2 << "'"
<< std::endl;
}
std::cout << count << " members" << std::endl;
}
static std::string findString (const std::string& which)
{
if (strings.find (which) != strings.end ()) return strings [which];
return which + " extra";
}
int main (int argc, char *argv[])
{
const std::string key ("something");
dump ("before");
strings [key] = findString (key);
dump ("after");
return EXIT_SUCCESS;
}
I've got a small test program which behaves correctly (IMHO) on all
compilers that I have acccess to, with the exception of the gcc on
Macintosh OSX "gcc version 4.0.0 (Apple Computer, Inc. build 5026)"
Looks to be that the order of evaluation of the assignment on OSX is
different to other systems. I *think* it is wrong on OSX, or am I
simply relying on undefined behaviour?
On a "correct" system, I get this output:
before:
0 members
after:
0 : 'something'='something extra'
1 members
On OSX, I get this output:
before:
0 members
after:
0 : 'something'=''
1 members
I appreciate that this newsgroup doesn't cover platform-specific
issues, but my question is more one of trying to understand if this
program is relying on undefined behaviour.
Source follows:
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
static std::map<std::string,std::string> strings;
static void dump (const std::string& prefix)
{
std::cout << prefix << ":" << std::endl;
unsigned int count = 0;
for (std::map<std::string,std::string>::const_iterator si =
strings.begin (); si != strings.end (); ++si)
{
const std::string s1 (si -> first);
const std::string s2 (si -> second);
std::cout << " " << count++ << " : '" << s1 << "'='" << s2 << "'"
<< std::endl;
}
std::cout << count << " members" << std::endl;
}
static std::string findString (const std::string& which)
{
if (strings.find (which) != strings.end ()) return strings [which];
return which + " extra";
}
int main (int argc, char *argv[])
{
const std::string key ("something");
dump ("before");
strings [key] = findString (key);
dump ("after");
return EXIT_SUCCESS;
}