3 ms·
> In C++ the type of "Hacker News" is char * In fact it is not: int main() { auto&& x ="Hacker News"; x.foo(); } <source>:6:7: error: request
by gpderetta 4y ago
> In C++ the type of "Hacker News" is char *
In fact it is not:
int main() {
auto&& x ="Hacker News";
x.foo();
}
<source>:6:7: error: request for member 'foo' in 'x', which is of non-class type 'const char [12]'
6 | x.foo();
They are an unstable type (an array) that, because of its C affinity, tends to quickly decay to pointers, hence the need to use ̵a̵ ̵p̵a̵r̵t̵i̵c̵l̵e̵ ̵a̵c̵c̵e̵l̵l̵e̵r̵a̵t̵o̵r̵ the universal reference to observe its fleeting lifetime.
edit: can't speak English
- tialaramex 4y agoGood point, I should have remembered these are actually immutable arrays of char (with a bonus 0 byte at the end). The main thrust of my point though is that this type is terrible - even before it decays, and you don't get a std::string_view which is what you probably actually ought to get in modern software.