Files
swift-mirror/test/Interop/Cxx/stdlib/Inputs/std-optional.h
Egor Zhdan dcc3842535 [cxx-interop] Instantiate std::optional value constructors
This improves support for initializing instances of `std::optional` from Swift. Previously only a null optional could be initialized directly from Swift. Now instantiations of `std::optional` will get a Swift initializer that takes the wrapped value as a parameter.

rdar://118026392
2025-07-03 14:00:27 +01:00

36 lines
1.3 KiB
C++

#ifndef TEST_INTEROP_CXX_STDLIB_INPUTS_STD_OPTIONAL_H
#define TEST_INTEROP_CXX_STDLIB_INPUTS_STD_OPTIONAL_H
#include <optional>
#include <string>
using StdOptionalInt = std::optional<int>;
using StdOptionalBool = std::optional<bool>;
using StdOptionalString = std::optional<std::string>;
using StdOptionalOptionalInt = std::optional<std::optional<int>>;
struct HasConstexprCtor {
int value;
constexpr HasConstexprCtor(int value) : value(value) {}
constexpr HasConstexprCtor(const HasConstexprCtor &other) = default;
constexpr HasConstexprCtor(HasConstexprCtor &&other) = default;
};
using StdOptionalHasConstexprCtor = std::optional<HasConstexprCtor>;
struct HasDeletedMoveCtor {
int value;
HasDeletedMoveCtor(int value) : value(value) {}
HasDeletedMoveCtor(const HasDeletedMoveCtor &other) : value(other.value) {}
HasDeletedMoveCtor(HasDeletedMoveCtor &&other) = delete;
};
using StdOptionalHasDeletedMoveCtor = std::optional<HasDeletedMoveCtor>;
inline StdOptionalInt getNonNilOptional() { return {123}; }
inline StdOptionalInt getNilOptional() { return {std::nullopt}; }
inline bool takesOptionalInt(std::optional<int> arg) { return (bool)arg; }
inline bool takesOptionalString(std::optional<std::string> arg) { return (bool)arg; }
#endif // TEST_INTEROP_CXX_STDLIB_INPUTS_STD_OPTIONAL_H