1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
# mode: run
# tag: cpp, cpp11, no-cpp-locals
from libcpp.utility cimport move
cdef extern from *:
"""
#include <utility>
const char* f(int& x) {
(void) x;
return "lvalue-ref";
}
const char* f(int&& x) {
(void) x;
return "rvalue-ref";
}
template <typename T>
const char* foo(T&& x)
{
return f(std::forward<T>(x));
}
"""
const char* foo[T](T&& x)
cdef extern from *:
"""
#include <utility>
template <typename T1>
const char* bar(T1 x, T1 y) {
return "first";
}
template <typename T1, typename T2>
const char* bar(T1&& x, T2 y, T2 z) {
return "second";
}
"""
const char* bar[T1](T1 x, T1 y)
const char* bar[T1, T2](T1&& x, T2 y, T2 z)
def test_forwarding_ref():
"""
>>> test_forwarding_ref()
"""
cdef int x = 1
assert foo(x) == b"lvalue-ref"
assert foo(<int>(1)) == b"rvalue-ref"
assert foo(move(x)) == b"rvalue-ref"
def test_forwarding_ref_overload():
"""
>>> test_forwarding_ref_overload()
"""
cdef int x = 1
cdef int y = 2
cdef int z = 3
assert bar(x, y) == b"first"
assert bar(x, y, z) == b"second"
|