It used to work: Reading from std::cin
to a dynamically allocated char
array - or to an array passed in as a parameter (see MCVE below).
#include <iostream>
void read (char str[256]) //or omit the # between the []'s -- no difference
{
std::cin >> str; //error
}
int main ()
{
char s1 [256];
char* s2 = new char[256];
char s3 [256];
std::cin >> s1; //OK
std::cin >> s2; //error
read (s3);
return 0;
}
I believe the problem is a C++20 update called Fixing operator>>(basic_istream&, CharT*)
. Its purpose is to prevent std::cin
from overflowing the string it's reading into, and cppreference.com does list new prototypes for std::istream
's operator>>
that now have CharT (&s)[N]
, not CharT* s
, as the argument taken.
Confirmation: this MCVE works with g++ 10 (which doesn't support this), and with Visual Studio using Default as the language flag; but using /std:c++latest
, VS complains:
binary '>>': no operator found which takes a right-hand operand of type 'char *' (or there is no acceptable conversion)
So I totally get what the compiler's complaining about. But how do I use std::cin
with arrays passed in as parameters, or dynamic arrays?
(Also: how does C++ know, when it's ready to read, whether an array was created dynamically or statically? It's not supposed to be able to draw that distinction after declaration!)