[numeric_traits] How to define integer difference_types

Dear all, Yesterday I had run into issues compiling boost::counting_iterator under Visual Studio. The reason is integer_traits<Integer>::difference_type which is used as a default difference_type for the counting_iterator. This has already been discussed here: http://groups.google.de/group/boost-list/browse_thread/thread/79b5a5bb13 f37576/7773443825609bb1 But I don't think the discussion went far enough: - First of all, this is not just an issue concerning compiler warnings about 64 bit conversion issues (/Wp64) as Orhun suggested in the above thread. - Trying to convert __int64 to int is a level 1 compiler warning (http://msdn2.microsoft.com/en-us/library/aa984663(VS.71).aspx) and in my opinion, the compiler should warn about implicit down-casts. - David suggested in the thread, that (quote) "These 'integer size warnings' are nothing more than a nuisance in most generic code. IMO they should be turned off on the command line." To this, I strongly object. The integer size warning is correct and may be a simple programming error that a developer should at least look at. It is the supposed greater generality of defining integer_traits<int> to be __int64 that causes a problem here. - Defining integer_traits<int>::difference_type as __int64 is first of all pretty inconsistent - integer_traits<__int64>::difference_type of course also is __int64 for lack of a larger type - *Most importantly, no one defines std::ptrdiff_t as __int64 although the same reasoning would apply* Integer values may overflow. Even __int64 values may overflow. There may be cases where you need (int - int) to be a __int64 just like (int * int) should be __int64. In general however, I would like int + (int - int) to be an int again. As a developer I know this can overflow but int = int + __int64(int - int) will overflow too and if I do __int64 + (__int64 - __int64) then this may overflow too! (Besides, in the context of iterators it is extremely weird for an iterator on ints to have an operation advance(__int64).) There's no general solution preventing ints from overflowing. The numeric_traits implementation forces me to disable perfectly valid static type checks for no apparent value at all. Let me take care of overflowing problems where *I* think they may occur. Let the compiler do proper type checks. Regards and I'm looking forward to comments on this, Sebastian -- Sebastian Theophil . stheophil@think-cell.com Software Engineer think-cell Software GmbH . Invalidenstr. 34 . 10115 Berlin, Germany http://www.think-cell.com . phone +49-30-666473-10 . fax +49-30-666473-19 Geschaftsfuhrer: Dr. Markus Hannebauer, Arno Schoedl . Amtsgericht Charlottenburg, HRB 85229
participants (1)
-
Sebastian Theophil