MongoDB C++ Driver 4.2.0
Loading...
Searching...
No Matches
Instance

How to use a MongoDB C++ Driver instance.

Initialize the C++ Driver

Basic Usage

void example() {
// Do not use mongocxx library interfaces at this point!
{
// Initialize the MongoDB C++ Driver by constructing the instance object.
// Use mongocxx library interfaces at this point.
// Cleanup the MongoDB C++ Driver by destroying the instance object.
}
// Do not use mongocxx library interfaces at this point!
}

Errors

Instance Recreation

void example() {
{
try {
mongocxx::instance another_instance; // Throws.
EXPECT(false && "should not reach this point");
} catch (mongocxx::exception const& ex) {
EXPECT(ex.code() == mongocxx::error_code::k_cannot_recreate_instance);
}
}
}

Destroyed Instance

void example() {
// Initialize and cleanup.
{
}
try {
mongocxx::instance instance; // Throws.
EXPECT(false && "should not reach this point");
} catch (mongocxx::exception const& ex) {
EXPECT(ex.code() == mongocxx::error_code::k_cannot_recreate_instance);
}
try {
mongocxx::instance instance; // Throws.
EXPECT(false && "should not reach this point");
} catch (mongocxx::exception const& ex) {
EXPECT(ex.code() == mongocxx::error_code::k_cannot_recreate_instance);
}
}