Pedro.--
You received this message because you are subscribed to the Google Groups "mongodb-dev" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mongodb-dev/-/8PEJEqNCSdQJ.
To post to this group, send email to mongo...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-dev...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-dev?hl=en.
-Andy
To unsubscribe from this group, send email to mongodb-dev+unsubscribe@googlegroups.com.
The portable and safer alternative is doing something like this:int asInt() {uint8_t* p = (uint8_t*)data;uint32_t res = 0;res = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | (p[0]);return static_cast<int>(res);}At -O2 and -O3 it gets optimized away and inlined, doesn't look like it will be slower than the previous cast.
I'm starting to sound like a broken record, but this is actually also
solved in https://github.com/skrabban/mongo-nonx86 .
It would be nice if the maintainers actually took a serious look at
this, because you would get the solution to SERVER-1625, SERVER-1811
and now also SERVER-5634.
Apart from that you would get a nice uniform way of casting, for example:
- int& dataAsInt() {
- return *((int *) _data);
+ little<int>& dataAsInt() {
+ return little<int>::ref( _data );
}
- return *reinterpret_cast< const int* >( value() );
+ return little<int>::ref( value() );
- return (reinterpret_cast < const PackedDouble* >(value ()))->d != 0
+ return little<double>::ref( value() ) != 0;
- *((int*)data) = size;
+ little<int>::ref( data ) = size;
- return (unsigned&) x[0];
+ return little<unsigned>::ref( x );
On x86/amd64 this will compile to the same as the original cast.
A lot of iterations have been made to make it as simple and uniform as possible.
I suggest you look at the master branch, but the v1.8 branch is the stable one.
/pi
The master branch is currently also almost up to date, but there is
some issue in snappy.
Bson should be complete though.
> I will have a look at your solution with the little template so maybe I can
> adapt it to the C++ driver I'm trying to use, since I'm using the one from
> the master branch as it has fixes that prevents me from using an older one.
> We treat all warnings as errors in our project and I wouldn't like to be
> more liberal about it only for the mongo driver, anyway some warnings are
> coming also from headers, such as the strict aliasing topic discussed here.
> If it could actually produce a real bug by means of broken optimization etc.
> Could the problem with strict aliasing really produce undefined behaviour?
Yes. You could get all sorts of results if you remove
-fno-strict-aliasing when the
project usually is built with that flag.
/pi
Yes, but the union only solves this particular instance of the
warning. I think I saw more when I used -fstrict-aliasing.
/pi
--
You received this message because you are subscribed to the Google Groups "mongodb-dev" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mongodb-dev/-/ubp_GyIe1UIJ.
-Andy
To unsubscribe from this group, send email to mongodb-dev+unsubscribe@googlegroups.com.