C++ Logo

std-proposals

Advanced search

Re: Making C++ easier for new students

From: Bill Kerney <bkerney_at_[hidden]>
Date: Thu, 5 Aug 2021 07:35:56 +0000
>Yes, that's possible. It'd require a return value which uses a
template conversion operator to do some kind of input reading, so that
it knows what type to read.

Yep! That's how it is implemented. You can look at the code here (https://github.com/ShakaUVM/read) if you're curious.

>Also, it totally doesn't work if you do `auto x = read();`. What's
worse is that this will *compile*, but fail to work when you use `x`
with a likely confusing error message.

Yeah, you do need to have the type somewhere, for it to know what to do. It looks like this in the current implementation if you want to use auto -

auto x = read<int>();

>But even if you could, what happens when you need to do more complex
reading, like formatted reading?

Iomanip, setf(), etc., all still work as you'd expect. Readlib sits on top of iostreams, so it doesn't lose any power.

For your example -
std::cin >> std::hex;
int x = read();

>Your `read` function is good for simple stuff, but it doesn't scale at all.

What other use cases are you thinking of? We've had hundreds of students using it for about 18 months now and it really does seem to be a lot nicer (faster development, fewer bugs, cleaner code) even for larger projects.

-Bill Kerney

Received on 2021-08-05 02:37:09