Click here to Skip to main content
15,923,142 members

Survey Results

C++ compliance and your code   [Edit]

Survey period: 3 Dec 2001 to 9 Dec 2001

If a future release of the VC++ compiler went from 89% compliance to 97% compliance but may not compile 100% of existing code (due to it being non-conformant) then how much of your code would you be willing to change in order to make it standard compliant? (suggested by Nick Hodapp, Microsoft)

OptionVotes% 
No lines10517.24
10 lines out of every 15,0006210.18
20 lines out of every 15,000579.36
50 lines out of every 15,0009415.44
More than 100 lines in every 15,000 lines29147.78



 
GeneralRe: Which 11% isn't compliant already? Pin
Chris Losinger3-Dec-01 12:38
professionalChris Losinger3-Dec-01 12:38 
GeneralRe: Which 11% isn't compliant already? Pin
Christian Graus3-Dec-01 12:46
protectorChristian Graus3-Dec-01 12:46 
GeneralRe: Which 11% isn't compliant already? Pin
Anna-Jayne Metcalfe7-Dec-01 0:32
Anna-Jayne Metcalfe7-Dec-01 0:32 
GeneralRe: Which 11% isn't compliant already? Pin
3-Dec-01 12:47
suss3-Dec-01 12:47 
GeneralRe: Which 11% isn't compliant already? Pin
Giles4-Dec-01 9:05
Giles4-Dec-01 9:05 
GeneralRe: Which 11% isn't compliant already? Pin
Jonathan Gilligan4-Dec-01 13:20
Jonathan Gilligan4-Dec-01 13:20 
GeneralRe: Which 11% isn't compliant already? Pin
Chris Losinger5-Dec-01 4:04
professionalChris Losinger5-Dec-01 4:04 
GeneralRe: Which 11% isn't compliant already? Pin
Jonathan Gilligan10-Dec-01 10:18
Jonathan Gilligan10-Dec-01 10:18 
Chris Losinger wrote:
I can't use std::list to sort a list by arbitrary predicates

???

of course you can.
list::sortvoid sort();template void sort(greater pr);


just because the parameter is called "greater" on the function prototype doesn't mean it can't be comparing in whatever direction you want. if fact, the sample for list::sort shows how to sort with a custom predicate.


There is a bug in the STL as delivered by Microsoft. The STL example you cite does not run as it is supposed to. The basic problem is that while you can subclass struct greater<t>, when you run list<t>::sort(specialized_greater_class<t>), it will call greater<t>::operator(), not specialized_greater_class<t>::operator(), so you can't sort on multiple predicates after all. P.J. Plauger confirmed this as a bug in 1999 and offered a solution (see http://groups.google.com/groups?hl=en&selm=7t385o%24t6d%241%40news.ime.net), but Microsoft, despite having released a couple of service packs since then, has not patched this STL bug.

Consider the following code:
#include <iostream>
#include <list>
 
using namespace std;
 
struct Foo
{
 int m_nValue;
 Foo(int val = 0) : m_nValue(val) {}
 bool operator >(const Foo& rhs_) const;
 bool operator <(const Foo& rht_) const;
 bool greater_mag(const Foo& rht_) const;
};
 
bool Foo::operator <(const Foo& rhs_) const
{
 return m_nValue < rhs_.m_nValue;
}
 
bool Foo::operator >(const Foo& rhs_) const
{
 cerr << "Called >" << endl;
 return (m_nValue > rhs_.m_nValue);
}
 
bool Foo::greater_mag(const Foo& rhs_) const
{
 cerr << "Called greater_mag" << endl;
 return abs(m_nValue) > abs(rhs_.m_nValue);
}
 
template <class T_> struct greater_magnitude : public greater<T_>
{
 bool operator()(const T_& x, const T_& y) const {
  return (x.greater_mag(y));
  }
};
 
int main(int argc, char* argv[])
{
 list<Foo> flist;
 flist.push_back(Foo(1));
 flist.push_back(Foo(-2));
 flist.push_back(Foo(3));
 
 flist.sort(greater_magnitude<Foo>());
 list<Foo>::const_iterator it = flist.begin();
 while (it != flist.end())
  cout << it++ ->m_nValue << " ";
 cout << endl;
 return 0;
}

Compile it with the STL that ships with MSVC (patched to SP5). When you run the program it outputs
Called >
Called >
3 1 -2

Note that the function is not calling the subclass's greater_magnitude::operator(), but the parent class's greater::operator().

Now patch the STL list header, as P.J. Plauger recommends (http://groups.google.com/groups?hl=en&selm=7t385o%24t6d%241%40news.ime.net) and the program runs properly.
Called greater_mag
Called greater_mag
3 -2 1

GeneralRe: Which 11% isn't compliant already? Pin
Chris Maunder3-Dec-01 3:42
cofounderChris Maunder3-Dec-01 3:42 
GeneralRe: Which 11% isn't compliant already? Pin
Chris Losinger3-Dec-01 4:10
professionalChris Losinger3-Dec-01 4:10 
GeneralRe: Which 11% isn't compliant already? Pin
Navin3-Dec-01 8:33
Navin3-Dec-01 8:33 
GeneralRe: Which 11% isn't compliant already? Pin
3-Dec-01 12:27
suss3-Dec-01 12:27 
GeneralRe: Which 11% isn't compliant already? Pin
Tim Smith3-Dec-01 13:11
Tim Smith3-Dec-01 13:11 
GeneralRe: Which 11% isn't compliant already? Pin
Christian Graus3-Dec-01 10:13
protectorChristian Graus3-Dec-01 10:13 
GeneralNon-Standards Compliance Pin
Fazlul Kabir3-Dec-01 3:04
Fazlul Kabir3-Dec-01 3:04 
GeneralTo what end... Pin
Tim Smith3-Dec-01 2:22
Tim Smith3-Dec-01 2:22 
GeneralRe: To what end... Pin
Christian Graus3-Dec-01 10:06
protectorChristian Graus3-Dec-01 10:06 
GeneralMany coders aren't aware of used extentions. Pin
2-Dec-01 20:52
suss2-Dec-01 20:52 
GeneralWell...... Pin
Christian Graus2-Dec-01 18:57
protectorChristian Graus2-Dec-01 18:57 
GeneralRe: Well...... Pin
Paul Selormey3-Dec-01 0:09
Paul Selormey3-Dec-01 0:09 
GeneralRe: Well...... Pin
CodeGuy3-Dec-01 7:03
CodeGuy3-Dec-01 7:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.