迅速 objective-c++ 使用 std::variant
swifty objective-c++ using std::variant
一般来说,我正在尝试使用 std::variant 创建声明性 "union enum" 类型,我需要它来描述列表中的位置 (UITableView/UICollectionView et.al)。
在 Swift 中,这正是我需要做的:
enum Location {
case Header
case Footer
case Index(NSIndexPath)
}
我的 API 允许使用 "header" 位置(部分和第 0 行的任意组合)、页脚(以及部分和根据部分变化的动态行号的组合)进行调用) 或特定的索引路径。
在 c++ 中,我相信有一个 std::variant
http://en.cppreference.com/w/cpp/utility/variant 允许这种行为:
我需要在 Objective-C++ 中执行此操作,但遇到编译器错误
enum class Location {
Header,
Footer
};
std::variant<Location,Index> location ; (NSIndexPath *)
当我尝试包含 #include (or #import) <variant>
时,出现编译器错误 variant file not found
in XCode.
有什么办法吗?
std::variant
是即将发布的 C++17 标准中包含的一项功能,该标准正在定稿中。目前有些编译器支持此功能,但您必须检查一下。
如果您的编译器当前支持它,您可能会在 std::experimental
命名空间中找到它,而不是直接在 std
.
中找到它
一般来说,我正在尝试使用 std::variant 创建声明性 "union enum" 类型,我需要它来描述列表中的位置 (UITableView/UICollectionView et.al)。
在 Swift 中,这正是我需要做的:
enum Location {
case Header
case Footer
case Index(NSIndexPath)
}
我的 API 允许使用 "header" 位置(部分和第 0 行的任意组合)、页脚(以及部分和根据部分变化的动态行号的组合)进行调用) 或特定的索引路径。
在 c++ 中,我相信有一个 std::variant
http://en.cppreference.com/w/cpp/utility/variant 允许这种行为:
我需要在 Objective-C++ 中执行此操作,但遇到编译器错误
enum class Location {
Header,
Footer
};
std::variant<Location,Index> location ; (NSIndexPath *)
当我尝试包含 #include (or #import) <variant>
时,出现编译器错误 variant file not found
in XCode.
有什么办法吗?
std::variant
是即将发布的 C++17 标准中包含的一项功能,该标准正在定稿中。目前有些编译器支持此功能,但您必须检查一下。
如果您的编译器当前支持它,您可能会在 std::experimental
命名空间中找到它,而不是直接在 std
.