Re: Typedef'ing a subtype of a template
* Ney Andr9 de Mello Zunino:
red floyd wrote:
I see nothing wrong with your code. Is this the entire fragment? What
compiler are you using?
The compiler is g++ (GCC) 4.1.3. And here's a complete program. In its
current form, it _will_ compile. However, substituting the second
typedef for the third one will cause the compilation error when the
priority_queue /Word_Queue/ is instantiated in main().
#include <string>
#include <map>
#include <queue>
#include <vector>
namespace {
using namespace std;
typedef map<string, int> Word_Map;
// typedef Word_Map::value_type Word_Map_Pair;
The commented definition would effectively define (note the const, which
makes the type non-assignable)
typedef pair< string const, int > Word_Map_Pair;
By the way, note that your naming convention is formally invalid.
An underscore can't be followed by an uppercase letter, it is reserved
for implementation names.
typedef pair<string, int> Word_Map_Pair;
struct Word_Map_Pair_Comparator {
bool operator()(const Word_Map_Pair& pair_1,
const Word_Map_Pair& pair_2) {
return pair_1.second >= pair_2.second;
}
};
typedef priority_queue<Word_Map_Pair, vector<Word_Map_Pair>,
Word_Map_Pair_Comparator> Word_Queue;
}
int main(int argc, char* argv[]) {
Please don't include things such as unused arguments that cause warnings.
Aim for 100% clean compiles.
Word_Queue word_queue;
}
This is OK.
However, using the non-assignable version of Word_Map_Pair as a
container element type is formally Undefined Behavior, and a good
compiler + library implementation will then issue diagnostics.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?