auto keyword as platform porting helper?

Do you see anything wrong with this code?


vector<int> v;

// …

int l = v.size();


 

Well, it seems that a lot of ISV’s don’t as I’ve encountered it many times.

If you compile for 32-bit at warning level 4, you don’t get any message. But if you target 64-bit, you get:


warning C4267: ‘initializing’ : conversion from ‘size_t’ to ‘int’, possible loss of data


 

size_t is 64-bit on 64-bit platform but an int is still 32-bit! Not only that but size_t is always unsigned. From CRTDefs.h:


#ifdef  _WIN64

typedef unsigned __int64    size_t;

#else

typedef unsigned int   size_t;

#endif


 

If we would have had C++ 0x sooner, more people would have just used the auto keyword and that problem would have mostly gone away…

The wcslen() function and the sizeof operator are other examples that return an instance of size_t.

I’d rather have a good look at the API documentation and still use auto. You might differ though. If so, drop me a comment.

Bonne nuit.

Advertisements
This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s