From: Vladimir Grigoriev on 12 Jan 2010 08:53 Igor, in context of what you have said I do not understand existence of std::less_equal and std::greater_equal in C++. If I may not to use them with algorithms why do they exist? And I do not see any reasonable explanation why they may not be used for example in std::min or in std::min_element. Vladimir Grigoriev .. "Igor Tandetnik" <itandetnik(a)mvps.org> wrote in message news:O%23pZa14kKHA.4872(a)TK2MSFTNGP05.phx.gbl... Vladimir Grigoriev wrote: > In fact without any serious reason the Microsoft compiler prevents from > compiling of a valid code. Yours is not a valid code, it exhibits undefined behavior. The C++ standard requires that the predicate be a strict weak ordering, and less_equal isn't. > In my opinion it is one mode serious bug of the > Microsoft compiler. It's not a bug, it's a feature. > I have not seen any reasonable explanation why this code > may not be compiled. To be precise, it compiled - it failed at run time. Anyway, MSVC's STL implementation is doing you a favor by turning an instance of undefined behavior into a hard error, so you can find and fix your bug more easily. > It seems that Microsoft inserted this check everywhere > in its library without any thought about consequences No - it inserted this check _because_ it thought about consequences. -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Igor Tandetnik on 12 Jan 2010 09:01 Vladimir Grigoriev wrote: >>> int a[] = { 6, 1, 7, 1, 8 }; >>> int *p = std::min_element( a, a + sizeof( a ) / sizeof( *a ), >>> std::less_equal() ); >> >> Use a reverse iterator to search from the end. All search functions return >> the first match, not the last. >> > > How to use reverse iterators with arrays? typedef std::reverse_iterator<int*> rev_it; int a[] = { 6, 1, 7, 1, 8 }; int *p = std::min_element( rev_it(a + sizeof( a ) / sizeof( *a )), rev_it(a), std::less() ).base(); >> Nope, the strict ordering comes from the C++ standard which in turn took >> it >> from the STL. You're barking up the wrong tree. BTW: I'd ask this on >> comp.lang.c++.moderated, you have typically more C++ experts there. > > Yes, I do not know the standard You can get a copy of C++98 here: http://www-d0.fnal.gov/~dladams/cxx_standard.pdf C++03 here: http://openassist.googlecode.com/files/C%2B%2B%20Standard%20-%20ANSI%20ISO%20IEC%2014882%202003.pdf and the most recent draft of C++0x here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n3000.pdf > but I think that strict ordering requirments > is applied not to all algorithms. Perhaps, but it does apply to std::min and std::min_element. -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Jeff Flinn on 12 Jan 2010 09:29 Vladimir Grigoriev wrote: > Igor, in context of what you have said I do not understand existence of > std::less_equal and std::greater_equal in C++. If I may not to use them with > algorithms why do they exist? And I do not see any reasonable explanation > why they may not be used for example in std::min or in std::min_element. They certainly are usable with algorithms not requiring strict weak ordering such as std::find_if. Jeff
From: Igor Tandetnik on 12 Jan 2010 09:28 Vladimir Grigoriev wrote: > Igor, in context of what you have said I do not understand existence of > std::less_equal and std::greater_equal in C++. Here's an example that uses less_equal: http://www.cplusplus.com/reference/std/functional/less_equal/ > If I may not to use them with algorithms why do they exist? You cannot use them with _some_ algorithms, but can with others. > And I do not see any reasonable explanation > why they may not be used for example in std::min or in std::min_element. Suppose we want to relax the specification, and not require strict weak ordering. On the other hand, you can't just allow any odd predicate: at the very least, you would need transitivity (otherwise, if a < b and b < c and c < a, which of the three is smallest? ) So now you have to think of the minimal set of requirements, and define a new class of predicates just for min/max family of algorithms. Yes, it's all possible - but is it worth the trouble? What precisely do you gain from this added complexity that you can't achieve by other means? In any case, if you feel strongly about it, please feel free to submit a formal proposal to the C++ standardization committee. -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Vladimir Grigoriev on 12 Jan 2010 09:29 Thanks, Igor. This is very useful information. I did not even know till now that I can use reverse iterators directly without their connection with containers. However in your example p points to 8. So I need to do additional manipulations with the reverse iterator to get correct pointer. With using std::less_equal directly in the std::min_element I have not such difficulties and can use raw integer pointer directly.:) Why to make things complicated when a direct simple method may be used? Vladimir Grigoriev "Igor Tandetnik" <itandetnik(a)mvps.org> wrote in message news:%23szEw%234kKHA.5604(a)TK2MSFTNGP04.phx.gbl... Vladimir Grigoriev wrote: >>> int a[] = { 6, 1, 7, 1, 8 }; >>> int *p = std::min_element( a, a + sizeof( a ) / sizeof( *a ), >>> std::less_equal() ); >> >> Use a reverse iterator to search from the end. All search functions >> return >> the first match, not the last. >> > > How to use reverse iterators with arrays? typedef std::reverse_iterator<int*> rev_it; int a[] = { 6, 1, 7, 1, 8 }; int *p = std::min_element( rev_it(a + sizeof( a ) / sizeof( *a )), rev_it(a), std::less() ).base(); >> Nope, the strict ordering comes from the C++ standard which in turn took >> it >> from the STL. You're barking up the wrong tree. BTW: I'd ask this on >> comp.lang.c++.moderated, you have typically more C++ experts there. > > Yes, I do not know the standard You can get a copy of C++98 here: http://www-d0.fnal.gov/~dladams/cxx_standard.pdf C++03 here: http://openassist.googlecode.com/files/C%2B%2B%20Standard%20-%20ANSI%20ISO%20IEC%2014882%202003.pdf and the most recent draft of C++0x here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n3000.pdf > but I think that strict ordering requirments > is applied not to all algorithms. Perhaps, but it does apply to std::min and std::min_element. -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: volatile keyword and memory barriers Next: Can not deduse template arguments std::rel_ops |