Commit 144546e1 authored by Evgeny Yakimov's avatar Evgeny Yakimov Committed by GitHub

Add std::optional declarations for C++17 (GH-3294)

Closes https://github.com/cython/cython/issues/3293
parent 4da6a663
from libcpp cimport bool
cdef extern from "<optional>" namespace "std" nogil:
cdef cppclass nullopt_t:
nullopt_t()
cdef nullopt_t nullopt
cdef cppclass optional[T]:
ctypedef T value_type
optional()
optional(nullopt_t)
optional(optional&) except +
optional(T&) except +
bool has_value()
T& value()
T& value_or[U](U& default_value)
void swap(optional&)
void reset()
T& emplace(...)
T& operator*()
#T* operator->() # Not Supported
optional& operator=(optional&)
optional& operator=[U](U&)
bool operator bool()
bool operator!()
bool operator==[U](optional&, U&)
bool operator!=[U](optional&, U&)
bool operator<[U](optional&, U&)
bool operator>[U](optional&, U&)
bool operator<=[U](optional&, U&)
bool operator>=[U](optional&, U&)
optional[T] make_optional[T](...) except +
# ticket: 3293
# mode: run
# tag: cpp, cpp17, werror
from cython.operator cimport dereference as deref
from libcpp.optional cimport optional, nullopt, make_optional
from libcpp.string cimport string
from libcpp.pair cimport pair
def simple_test():
"""
>>> simple_test()
"""
cdef optional[int] o
assert(not o.has_value())
o = 5
assert(o.has_value())
assert(o.value()==5)
o.reset()
assert(not o.has_value())
def operator_test():
"""
>>> operator_test()
"""
cdef optional[int] o1, o2
# operator bool
assert(not o1)
o1 = 5
assert(o1)
# operator *
assert(deref(o1) == 5)
# operator =,==,!=,>,<,>=,<=
assert(not o1 == o2)
assert(o1 != o2)
o2 = o1
assert(o1 == o2)
assert(not o1 > o2)
assert(not o1 < o2)
assert(o1 >= o2)
assert(o1 <= o2)
# operators =,== for other types (all related operators are identical)
o1 = 6
assert(o1 == 6)
o2 = nullopt
assert(o2 == nullopt)
def misc_methods_test():
"""
>>> misc_methods_test()
"""
# make_optional
cdef optional[int] o
o = make_optional[int](5)
assert(o == 5)
# swap
o.swap(optional[int](6))
assert(o == 6)
# emplace
cdef optional[pair[int,int]] op
cdef pair[int,int]* val_ptr = &op.emplace(1,2)
assert(op.has_value())
assert(op.value() == pair[int,int](1,2))
assert(&op.value() == val_ptr) # check returned reference
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment