| 390 | // --------------------------------------------------------------------------------------------------- |
| 391 | |
| 392 | void test_emplace() |
| 393 | { |
| 394 | struct A |
| 395 | { |
| 396 | A() = default; |
| 397 | explicit A(int i, float f, const std::string& str) : i_{i}, f_{f}, str_{str} {} |
| 398 | |
| 399 | int i_{0}; |
| 400 | float f_{0.0f}; |
| 401 | std::string str_; |
| 402 | }; |
| 403 | |
| 404 | dlib::optional<A> o1(dlib::in_place, 1, 2.5f, "hello there"); |
| 405 | DLIB_TEST(o1); |
| 406 | DLIB_TEST(o1->i_ == 1); |
| 407 | DLIB_TEST(o1->f_ == 2.5f); |
| 408 | DLIB_TEST(o1->str_ == "hello there"); |
| 409 | |
| 410 | dlib::optional<A> o2; |
| 411 | o2.emplace(2, 5.1f, "general kenobi"); |
| 412 | DLIB_TEST(o2); |
| 413 | DLIB_TEST(o2->i_ == 2); |
| 414 | DLIB_TEST(o2->f_ == 5.1f); |
| 415 | DLIB_TEST(o2->str_ == "general kenobi"); |
| 416 | |
| 417 | auto o3 = dlib::make_optional<A>(3, 3.141592f, "from a certain point of view"); |
| 418 | DLIB_TEST(o3); |
| 419 | DLIB_TEST(o3->i_ == 3); |
| 420 | DLIB_TEST(o3->f_ == 3.141592f); |
| 421 | DLIB_TEST(o3->str_ == "from a certain point of view"); |
| 422 | |
| 423 | dlib::optional<std::string> o4(dlib::in_place, {'a', 'b', 'c'}); |
| 424 | DLIB_TEST(o4); |
| 425 | DLIB_TEST(*o4 == "abc"); |
| 426 | DLIB_TEST(o4 == "abc"); |
| 427 | |
| 428 | dlib::optional<std::string> o5; |
| 429 | o5.emplace({'a', 'b', 'c'}); |
| 430 | DLIB_TEST(o5); |
| 431 | DLIB_TEST(*o5 == "abc"); |
| 432 | } |
| 433 | |
| 434 | // --------------------------------------------------------------------------------------------------- |
| 435 | |