Skip to content

Commit 933a28b

Browse files
authored
Fix delegates with return value passed as fptr or {p,mfptr} (#966)
1 parent 186f53e commit 933a28b

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

strings/base_delegate.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ namespace winrt::impl
162162
{}
163163

164164
template <typename F> delegate_base(F* handler) :
165-
delegate_base([=](auto&& ... args) { handler(args...); })
165+
delegate_base([=](auto&& ... args) { return handler(args...); })
166166
{}
167167

168168
template <typename O, typename M> delegate_base(O* object, M method) :
169-
delegate_base([=](auto&& ... args) { ((*object).*(method))(args...); })
169+
delegate_base([=](auto&& ... args) { return ((*object).*(method))(args...); })
170170
{}
171171

172172
template <typename O, typename M> delegate_base(com_ptr<O>&& object, M method) :

test/test/delegate.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,48 @@ TEST_CASE("delegate")
6969
delegate<int(int, int)> d = [](int a, int b) {return a + b; };
7070
REQUIRE(d(4, 5) == 9);
7171
}
72+
73+
// void(int*) with function pointer
74+
{
75+
struct S
76+
{
77+
static void Invoke(int* p) { *p = 123; }
78+
};
79+
int value = 0;
80+
delegate<void(int*)> d = &S::Invoke;
81+
d(&value);
82+
REQUIRE(value == 123);
83+
}
84+
85+
// void(int*) with object and method pointer
86+
{
87+
struct S
88+
{
89+
void Invoke(int* p) { *p = 123; }
90+
} s;
91+
delegate<void(int*)> d{ &s, &S::Invoke };
92+
int value = 0;
93+
d(&value);
94+
REQUIRE(value == 123);
95+
}
96+
97+
// int() with function pointer
98+
{
99+
struct S
100+
{
101+
static int Value() { return 123; }
102+
};
103+
delegate<int()> d = &S::Value;
104+
REQUIRE(d() == 123);
105+
}
106+
107+
// int() with object and method pointer
108+
{
109+
struct S
110+
{
111+
int Value() { return 123; }
112+
} s;
113+
delegate<int()> d{ &s, &S::Value };
114+
REQUIRE(d() == 123);
115+
}
72116
}

0 commit comments

Comments
 (0)