Comments (3)
This had been under consideration in the initial design stages of the library, but I decided on pass-by-reference for the following reasons:
-
Consistent with
scanf
andstd::cin
-
Consistent with
{fmt}
/std::format
Argument handling/type-erasure facilities from std::format
could be utilized in the future.
- Using tuples as return values in clunky (pre-C++17)
The library supports C++11 and 14. Without structured bindings, returning a tuple would, in my opinion, be worse than passing by reference.
int i;
scn::result<int> r;
std::tie(r, i) = scn::input<int>("{}");
This doesn't remove the problem of default-constructing the arguments before the call.
But I want to scan non-default-constructible values!
This would prevent us from scanning multiple values with a single call.
Consider this example:
auto stream = scn::make_stream("foo");
// Going to reach EOF mid-operation
auto [err, str, i] = scn::scan<std::string, int>(stream, "{} {}");
// err.error() == scn::error::end_of_stream
// str == "foo"
// i is default constructed
If a scanning operation of multiple values fails, the arguments that couldn't be read must be default-constructed.
This could be worked around of with the pass-by-reference API, by implementing a optional-like type, that only constructs the underlying non-default-constructible value on assignment. I cannot think of a way to go around this with a returning API.
// Note: this would need scn::scanner<std::optional<T>> to be specialized
std::optional<non_default_constructible> val(std::nullopt);
scn::input("{}", val); // val is only going to be assigned to if the scan succeeds
An additional advantage of the pass-by-reference API is the ability to have default values for scanned values in case of an error, because the value is not going to be modified in case of an error.
int i{42};
auto ret = scn::input("{}", i);
if (!ret) {
assert(i == 42);
} else {
// whatever we got from stdin
}
from scnlib.
An alternative API, returning a std::tuple
, is available on the branch return-api
.
from scnlib.
return-api
-branch has been merged. The alternative API causes a ~5% slowdown, and requires C++17 to be any usable, but it's there.
from scnlib.
Related Issues (20)
- scan_list_ex: unable to use separator when scanning list of strings HOT 1
- Capture matches in tuple HOT 1
- `scn::basic_string_view::substr` does not compile. HOT 1
- scn::scan_tuple compilation error with custom type HOT 1
- Cmake <3.5 deprecation warning HOT 1
- Quadratic Complexity Issue with scnlib's scan Function for Long Texts HOT 5
- Wich packages does this come with ? HOT 1
- Dead documentation link HOT 1
- Scan format "{} = ({}, {})" with std::string not properly parsed HOT 2
- issue with scn::getline on file input HOT 2
- Can't compile scnlib/2.0.0 on MSVC HOT 4
- Library not working in Godbolt Compiler explorer HOT 11
- cmake: Rejects compilation with simdutf-5.0.0 HOT 4
- Potential regression from v1.1.3 to v2.0.2 HOT 3
- No libc++ Support On Windows 11 HOT 1
- Failure scanning negative floating-point decimals HOT 3
- Single Header HOT 1
- how to parse string HOT 1
- Clang (clang-scan-deps) emits error about c++14 digit separator in preprocessor constants
- Case-insensitive mode?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scnlib.