Using std::expected from C++23 -- Bartlomiej Filipek

c++stories-usingstd.pngIn this article, we’ll go through a new vocabulary type introduced in C++23. std::expected is a type specifically designed to return results from a function, along with the extra error information.

Using std::expected from C++23

by Bartlomiej Filipek

From the article:

Imagine you’re expecting a certain result from a function, but oops… things don’t always go as planned:
/*RESULT*/ findRecord(Database& db, int recordId) {
    if (!db.connected())
        return /*??*/
   
    auto record = db.query(recordId);
    if (record.valid) {
        return record;
	
    return /*??*/
}
What can you do? What should you return or output through /*RESULT*/ ?

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.