C
cinsk
Hi.
On the Gentoo Linux 2.6.28, GCC 4.4.3,
In the class, I want to create a member function 'isspace(char)', so I
tried like:
class parser {
public:
parser() : loc_(std::locale()) {}
bool isspace(char ch) { return std::isspace(ch, loc_); }
...
private:
const std::locale &loc_;
};
parser p;
p.isspace('a'); // throws std::bad_cast or segmentation fault.
Q. Could you explain why it throws a bad_cast?
Then, I modified above constructor in following way:
parser(const std::locale &loc = std::locale()) : loc_(loc) {}
Q. Then, the problem gone. Could you explain why it succeed? What's
the difference from the former?
If I make a global function like std::isspace() using the second
constructor style, it works again.
isspace(char ch, const std::locale &loc = std::locale()) {
return std::isspace(ch, loc);
}
isspace('a'); // works.
On the Gentoo Linux 2.6.28, GCC 4.4.3,
In the class, I want to create a member function 'isspace(char)', so I
tried like:
class parser {
public:
parser() : loc_(std::locale()) {}
bool isspace(char ch) { return std::isspace(ch, loc_); }
...
private:
const std::locale &loc_;
};
parser p;
p.isspace('a'); // throws std::bad_cast or segmentation fault.
Q. Could you explain why it throws a bad_cast?
Then, I modified above constructor in following way:
parser(const std::locale &loc = std::locale()) : loc_(loc) {}
Q. Then, the problem gone. Could you explain why it succeed? What's
the difference from the former?
If I make a global function like std::isspace() using the second
constructor style, it works again.
isspace(char ch, const std::locale &loc = std::locale()) {
return std::isspace(ch, loc);
}
isspace('a'); // works.