Похожие чаты

That stateful behavior is caused by the fact that cin

and cout maintain synchronization with the C I/O functions, right? Or is it just their implementation? Because if it's the former, there is a solution.

1 ответов

6 просмотров

Interface is not implementation. Interface denotes what service a library provides, and implementation is how the service is performed internally. A stateless interface can have a stateful implementation. Consider a read-only caching database driver. A stateful interface can have a stateless implementation. Consider printf — the function alters the underlying IO state, while the implementation of printf itself is stateless [1]. Back to the problem, cin syncs with stdio by default, which means the *implementation* of cin is stateful. However, as an external user, you don't really care about the internal things [2]. What you actually care is the interface. cin introduces iostate, which effectively make the *interface* stateful. Now the state is exposed to external users, which means you have to think about the state between calls. To recapitulate, states are bad, but states are unavoidable when performing IO. As an I/O library author, you can add internal states to achieve syncing/caching or whatever. However, it's not wise to add states to your external interface to torture the library users. [1]: okay, I know that FILE has internal caching, but that's working at a lower layer. For the layer of formatting text, printf is stateless. [2]: unless you're not satisfied with the performance, see std::ios_base::sync_with_stdio.

Похожие вопросы

Обсуждают сегодня

А чем вам питонисты не угодили?😂
.
79
or any website to buy prepaid card with xmr that's not trocador that's down?
Umbrella Party Partner
18
Всем привет, написал код ниже, но он выдает сегфолт, в чем причина? #include <stdio.h> #include <stdlib.h> #include <string.h> struct product { char *name; float price; };...
buzz базз
75
Hi, I can't understand promises in JavaScript and what we should use them for (maybe the teacher didn't teach well XD). Do you have a solution for this? And are promises used...
A
29
Ещё такой вопрос. Мне необходимо хранить пароль пользователя локально. Для этого планирую использовать ini файл. Это для автозаполнения полей логин и пароль при авторизации. Е...
Евгений
19
всем доброго времени суток! имею вопрос: как понять ТОЧНО, что на нексус производится атака или он перегружен? исходные данные: - Nexus OSS 3.67.1-01 на OrientDB - Total co...
Michael Kostelcev
3
Ты просто гитлеровскую эстетику плохо понимаешь. Он же всё под Цезаря делал. А это как бы запрещённый приём в политике. Пиджаки они зачем все носят? Чтобы показать что они тип...
Ivan Kropotkin
4
Xem delist ho rha hai agr naa bhechu toh kya hoga after 1 july?
ABHI
27
i need usdt exchanged to xmr without kyc any site there ?
Certained
12
Did you guys see the latest tweet from TonGifts? 🚀
Mike
44
Карта сайта