FCF 2.0 development in progress...
> > > > > >
[News] [C++ Libraries API] [C++ Downloads] [Donate to the project] [Contacts]

operator!() method from fcf::Error class

bool operator!() const

Class: fcf::Error

Package: fcfBasis

File: bits/PartException/ErrorDecl.hpp ( link_to_the_declaration_in_the_file )

Implementation file: bits/PartException/Error.ipp ( link_to_the_implementation_in_the_file )

Available from version: 1.0.1

Checks if the error object is in a clean (success) state.

The operator! method provides a negated check for the presence of an error. It is the logical inverse of operator bool.

When called, it returns true if the error object is in a clean state (the error member is null), and false if an error is detected.

Result
bool
- Returns true if there is no error, false if an error is present.

Example: Negated error check

Demonstrating the use of operator ! to simplify conditional logic when checking for successful operations.

#define FCF_BASIS_IMPLEMENTATION #include <fcfBasis/basis.hpp> #include <iostream> int main() { // 1. Initialize an error object in a clean state fcf::Error err; // Using operator ! to check if the operation was successful if (!err) { std::cout << "Operation was successful (no error detected)." << std::endl; } // 2. Trigger an error fcf::Exception ex; err = fcf::Error(&ex); err.raise<fcf::LogicException>(__FILE__, __LINE__, "An error occurred"); // 3. Check for error presence using operator ! if (err) { std::cout << "Error detected: " << err.error << std::endl; } else { // This block will NOT be executed in this example std::cout << "Success!" << std::endl; } // 4. Using the negated check directly in a conditional if (!err) { std::cout << "Should not happen in this case." << std::endl; } else { std::cout << "Operation failed (error was detected)." << std::endl; } return 0; }

Output:

Operation was successful (no error detected). Error detected: FCF_LOGIC Operation failed (error was detected).

The operator ! method is particularly useful in if statements where the logic follows the pattern "if not error, then proceed", making the code more readable and concise.