How to use a MongoDB C++ Driver instance.
Initialize the C++ Driver
Basic Usage
void example() {
{
EXPECT(&mongocxx::instance::current() == &instance);
}
}
With Static Lifetime
- Warning
- This pattern depends on an exit-time destructor with indeterminate order relative to other objects with static lifetime being destroyed.
void example() {
{
auto& instance = mongocxx::instance::current();
EXPECT(&mongocxx::instance::current() == &instance);
}
}
Errors
Instance Recreation
void example() {
{
EXPECT(&mongocxx::instance::current() == &instance);
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == mongocxx::error_code::k_cannot_recreate_instance);
}
EXPECT(&mongocxx::instance::current() == &instance);
}
}
Destroyed Instance
void example() {
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == mongocxx::error_code::k_cannot_recreate_instance);
}
try {
auto& instance = mongocxx::instance::current();
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == mongocxx::error_code::k_instance_destroyed);
}
}