Request Short Link
C++ 98
C++ 11
C++ 14
C++ 17
C++ 20
C++ 23
C++ 2c
for-loops as while-loops
array subscription
Show all implicit casts
Show all template parameters of a CallExpr
Use libc++
Transform std::initializer_list
Show noexcept internals
Show padding information
Show coroutine transformation
Show C++ to C transformation
Show object lifetime
Default
15
18
20
22
26
More
GitHub
Patreon
Patreon
Issues
About
Policies
Examples
C++ Insights @ YouTube
Settings
Version
New C++ Insights Episode
×
Sponsors:
Made by
Andreas Fertig
Source:
#include <filesystem> #include <optional> #include <iostream> #include <string> #include <fstream> #include <string_view> // Source - https://stackoverflow.com/q/79826110 // Posted by sam, modified by community. See post 'Timeline' for change history // Retrieved 2025-11-21, License - CC BY-SA 4.0 namespace FS = std::filesystem; using OptionalString = std::optional<std::string>; OptionalString readFileContents(const FS::path& filePath) { std::ifstream inputStream(filePath); if (!inputStream) { return std::nullopt; } std::string fileContents{ std::istreambuf_iterator<char>{inputStream},{}}; // version A return fileContents; // version B // return std::move(fileContents); } int main() { std::string_view filePath{ "test.txt" }; auto maybeFileContents = readFileContents(filePath); }
Insight:
Console: