Skip to content

Commit 7e730e7

Browse files
authored
Revert "Prevent inadvertent assignment to temporary object" (#976)
1 parent 634d627 commit 7e730e7

File tree

10 files changed

+22
-136
lines changed

10 files changed

+22
-136
lines changed

cppwinrt/code_writers.h

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,10 +2371,6 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
23712371
{
23722372
%(std::nullptr_t = nullptr) noexcept {}
23732373
%(void* ptr, take_ownership_from_abi_t) noexcept : winrt::Windows::Foundation::IInspectable(ptr, take_ownership_from_abi) {}
2374-
%(% const&) noexcept = default;
2375-
%(%&&) noexcept = default;
2376-
%& operator=(% const&) & noexcept = default;
2377-
%& operator=(%&&) & noexcept = default;
23782374
%% };
23792375
)";
23802376

@@ -2384,14 +2380,6 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
23842380
bind<write_interface_requires>(type),
23852381
type_name,
23862382
type_name,
2387-
type_name, // %(T const&)
2388-
type_name, // T(% const&)
2389-
type_name, // %(T&&)
2390-
type_name, // T(%&&)
2391-
type_name, // %& operator=(T const&)
2392-
type_name, // T& operator=(% const&)
2393-
type_name, // %& operator=(T&&)
2394-
type_name, // T& operator=(%&&)
23952383
bind<write_interface_usings>(type),
23962384
bind<write_interface_extensions>(type));
23972385
}
@@ -2406,10 +2394,6 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
24062394
{%
24072395
%(std::nullptr_t = nullptr) noexcept {}
24082396
%(void* ptr, take_ownership_from_abi_t) noexcept : winrt::Windows::Foundation::IInspectable(ptr, take_ownership_from_abi) {}
2409-
%(% const&) noexcept = default;
2410-
%(%&&) noexcept = default;
2411-
%& operator=(% const&) & noexcept = default;
2412-
%& operator=(%&&) & noexcept = default;
24132397
%% };
24142398
)";
24152399

@@ -2421,14 +2405,6 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
24212405
bind<write_generic_asserts>(generics),
24222406
type_name,
24232407
type_name,
2424-
type_name, // %(T const&)
2425-
type_name, // T(% const&)
2426-
type_name, // %(T&&)
2427-
type_name, // T(%&&)
2428-
type_name, // %& operator=(T const&)
2429-
type_name, // T& operator=(% const&)
2430-
type_name, // %& operator=(T&&)
2431-
type_name, // T& operator=(%&&)
24322408
bind<write_interface_usings>(type),
24332409
bind<write_interface_extensions>(type));
24342410
}
@@ -2454,10 +2430,6 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
24542430
{%
24552431
%(std::nullptr_t = nullptr) noexcept {}
24562432
%(void* ptr, take_ownership_from_abi_t) noexcept : Windows::Foundation::IUnknown(ptr, take_ownership_from_abi) {}
2457-
%(% const&) noexcept = default;
2458-
%(%&&) noexcept = default;
2459-
%& operator=(% const&) & noexcept = default;
2460-
%& operator=(%&&) & noexcept = default;
24612433
template <typename L> %(L lambda);
24622434
template <typename F> %(F* function);
24632435
template <typename O, typename M> %(O* object, M method);
@@ -2470,18 +2442,10 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
24702442
method_signature signature{ get_delegate_method(type) };
24712443

24722444
w.write(format,
2473-
type_name, // struct %
2445+
type_name,
24742446
bind<write_generic_asserts>(generics),
24752447
type_name,
24762448
type_name,
2477-
type_name, // %(T const&)
2478-
type_name, // T(% const&)
2479-
type_name, // %(T&&)
2480-
type_name, // T(%&&)
2481-
type_name, // %& operator=(T const&)
2482-
type_name, // T& operator=(% const&)
2483-
type_name, // %& operator=(T&&)
2484-
type_name, // T& operator=(%&&)
24852449
type_name,
24862450
type_name,
24872451
type_name,
@@ -3155,11 +3119,7 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
31553119
{
31563120
%(std::nullptr_t) noexcept {}
31573121
%(void* ptr, take_ownership_from_abi_t) noexcept : %(ptr, take_ownership_from_abi) {}
3158-
% %(% const&) noexcept = default;
3159-
%(%&&) noexcept = default;
3160-
%& operator=(% const&) & noexcept = default;
3161-
%& operator=(%&&) & noexcept = default;
3162-
%% };
3122+
%%% };
31633123
)";
31643124

31653125
w.write(format,
@@ -3171,14 +3131,6 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
31713131
type_name,
31723132
base_type,
31733133
bind<write_constructor_declarations>(type, factories),
3174-
type_name, // %(T const&)
3175-
type_name, // T(% const&)
3176-
type_name, // %(T%&)
3177-
type_name, // T(%&&)
3178-
type_name, // %& operator=(T const&)
3179-
type_name, // T& operator=(% const&)
3180-
type_name, // %& operator=(T&&)
3181-
type_name, // T& operator=(%&&)
31823134
bind<write_class_usings>(type),
31833135
bind_each<write_static_declaration>(factories, type));
31843136
}
@@ -3192,11 +3144,7 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
31923144
{
31933145
%(std::nullptr_t) noexcept {}
31943146
%(void* ptr, take_ownership_from_abi_t) noexcept : %(ptr, take_ownership_from_abi) {}
3195-
% %(% const&) noexcept = default;
3196-
%(%&&) noexcept = default;
3197-
%& operator=(% const&) & noexcept = default;
3198-
%& operator=(%&&) & noexcept = default;
3199-
%% };
3147+
%%% };
32003148
)";
32013149

32023150
w.write(format,
@@ -3207,14 +3155,6 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
32073155
type_name,
32083156
base_type,
32093157
bind<write_constructor_declarations>(type, factories),
3210-
type_name, // %(T const&)
3211-
type_name, // T(% const&)
3212-
type_name, // %(T%&)
3213-
type_name, // T(%&&)
3214-
type_name, // %& operator=(T const&)
3215-
type_name, // T& operator=(% const&)
3216-
type_name, // %& operator=(T&&)
3217-
type_name, // T& operator=(%&&)
32183158
bind<write_fast_class_base_declarations>(type),
32193159
bind_each<write_static_declaration>(factories, type));
32203160
}

strings/base_activation.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,10 +523,6 @@ WINRT_EXPORT namespace winrt
523523
{
524524
IActivationFactory(std::nullptr_t = nullptr) noexcept {}
525525
IActivationFactory(void* ptr, take_ownership_from_abi_t) noexcept : IInspectable(ptr, take_ownership_from_abi) {}
526-
IActivationFactory(IActivationFactory const&) noexcept = default;
527-
IActivationFactory(IActivationFactory&&) noexcept = default;
528-
IActivationFactory& operator=(IActivationFactory const&) & noexcept = default;
529-
IActivationFactory& operator=(IActivationFactory&&) & noexcept = default;
530526

531527
template <typename T>
532528
T ActivateInstance() const

strings/base_array.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ WINRT_EXPORT namespace winrt
295295
other.m_size = 0;
296296
}
297297

298-
com_array& operator=(com_array&& other) & noexcept
298+
com_array& operator=(com_array&& other) noexcept
299299
{
300300
clear();
301301
this->m_data = other.m_data;

strings/base_com_ptr.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ WINRT_EXPORT namespace winrt
5757
release_ref();
5858
}
5959

60-
com_ptr& operator=(com_ptr const& other) & noexcept
60+
com_ptr& operator=(com_ptr const& other) noexcept
6161
{
6262
copy_ref(other.m_ptr);
6363
return*this;
6464
}
6565

66-
com_ptr& operator=(com_ptr&& other) & noexcept
66+
com_ptr& operator=(com_ptr&& other) noexcept
6767
{
6868
if (this != &other)
6969
{
@@ -75,14 +75,14 @@ WINRT_EXPORT namespace winrt
7575
}
7676

7777
template <typename U>
78-
com_ptr& operator=(com_ptr<U> const& other) & noexcept
78+
com_ptr& operator=(com_ptr<U> const& other) noexcept
7979
{
8080
copy_ref(other.m_ptr);
8181
return*this;
8282
}
8383

8484
template <typename U>
85-
com_ptr& operator=(com_ptr<U>&& other) & noexcept
85+
com_ptr& operator=(com_ptr<U>&& other) noexcept
8686
{
8787
release_ref();
8888
m_ptr = std::exchange(other.m_ptr, {});

strings/base_events.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ WINRT_EXPORT namespace winrt
2929
event_revoker& operator=(event_revoker const&) = delete;
3030
event_revoker(event_revoker&&) noexcept = default;
3131

32-
event_revoker& operator=(event_revoker&& other) & noexcept
32+
event_revoker& operator=(event_revoker&& other) noexcept
3333
{
3434
if (this != &other)
3535
{
@@ -84,7 +84,7 @@ WINRT_EXPORT namespace winrt
8484
factory_event_revoker& operator=(factory_event_revoker const&) = delete;
8585
factory_event_revoker(factory_event_revoker&&) noexcept = default;
8686

87-
factory_event_revoker& operator=(factory_event_revoker&& other) & noexcept
87+
factory_event_revoker& operator=(factory_event_revoker&& other) noexcept
8888
{
8989
if (this != &other)
9090
{
@@ -140,7 +140,7 @@ namespace winrt::impl
140140
event_revoker& operator=(event_revoker const&) = delete;
141141

142142
event_revoker(event_revoker&&) noexcept = default;
143-
event_revoker& operator=(event_revoker&& other) & noexcept
143+
event_revoker& operator=(event_revoker&& other) noexcept
144144
{
145145
event_revoker(std::move(other)).swap(*this);
146146
return *this;

strings/base_handle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ WINRT_EXPORT namespace winrt
1616
{
1717
}
1818

19-
handle_type& operator=(handle_type&& other) & noexcept
19+
handle_type& operator=(handle_type&& other) noexcept
2020
{
2121
if (this != &other)
2222
{

strings/base_security.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ WINRT_EXPORT namespace winrt
5555

5656
access_token() = default;
5757
access_token(access_token&& other) = default;
58-
access_token& operator=(access_token&& other) & = default;
58+
access_token& operator=(access_token&& other) = default;
5959

6060
access_token impersonate() const
6161
{

strings/base_string.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,16 @@ WINRT_EXPORT namespace winrt
179179
m_handle(impl::duplicate_hstring(value.m_handle.get()))
180180
{}
181181

182-
hstring& operator=(hstring const& value) &
182+
hstring& operator=(hstring const& value)
183183
{
184184
m_handle.attach(impl::duplicate_hstring(value.m_handle.get()));
185185
return*this;
186186
}
187187

188188
hstring(hstring&&) noexcept = default;
189-
hstring& operator=(hstring&&) & = default;
189+
hstring& operator=(hstring&&) = default;
190190
hstring(std::nullptr_t) = delete;
191-
hstring& operator=(std::nullptr_t) & = delete;
191+
hstring& operator=(std::nullptr_t) = delete;
192192

193193
hstring(std::initializer_list<wchar_t> value) :
194194
hstring(value.begin(), static_cast<uint32_t>(value.size()))
@@ -206,17 +206,17 @@ WINRT_EXPORT namespace winrt
206206
hstring(value.data(), static_cast<size_type>(value.size()))
207207
{}
208208

209-
hstring& operator=(std::wstring_view const& value) &
209+
hstring& operator=(std::wstring_view const& value)
210210
{
211211
return *this = hstring{ value };
212212
}
213213

214-
hstring& operator=(wchar_t const* const value) &
214+
hstring& operator=(wchar_t const* const value)
215215
{
216216
return *this = hstring{ value };
217217
}
218218

219-
hstring& operator=(std::initializer_list<wchar_t> value) &
219+
hstring& operator=(std::initializer_list<wchar_t> value)
220220
{
221221
return *this = hstring{ value };
222222
}

strings/base_windows.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ WINRT_EXPORT namespace winrt::Windows::Foundation
161161
release_ref();
162162
}
163163

164-
IUnknown& operator=(IUnknown const& other) & noexcept
164+
IUnknown& operator=(IUnknown const& other) noexcept
165165
{
166166
if (this != &other)
167167
{
@@ -173,7 +173,7 @@ WINRT_EXPORT namespace winrt::Windows::Foundation
173173
return*this;
174174
}
175175

176-
IUnknown& operator=(IUnknown&& other) & noexcept
176+
IUnknown& operator=(IUnknown&& other) noexcept
177177
{
178178
if (this != &other)
179179
{
@@ -189,7 +189,7 @@ WINRT_EXPORT namespace winrt::Windows::Foundation
189189
return nullptr != m_ptr;
190190
}
191191

192-
IUnknown& operator=(std::nullptr_t) & noexcept
192+
IUnknown& operator=(std::nullptr_t) noexcept
193193
{
194194
release_ref();
195195
return*this;
@@ -425,9 +425,5 @@ WINRT_EXPORT namespace winrt::Windows::Foundation
425425
{
426426
IInspectable(std::nullptr_t = nullptr) noexcept {}
427427
IInspectable(void* ptr, take_ownership_from_abi_t) noexcept : IUnknown(ptr, take_ownership_from_abi) {}
428-
IInspectable(IInspectable const&) noexcept = default;
429-
IInspectable(IInspectable&&) noexcept = default;
430-
IInspectable& operator=(IInspectable const&) & noexcept = default;
431-
IInspectable& operator=(IInspectable&&) & noexcept = default;
432428
};
433429
}

test/old_tests/UnitTests/properties.cpp

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -44,50 +44,4 @@ TEST_CASE("properties")
4444

4545
REQUIRE_THROWS_AS(e.Name(L"throw"), hresult_invalid_argument);
4646
REQUIRE_THROWS_AS(e.Name(), hresult_invalid_argument);
47-
48-
}
49-
50-
namespace
51-
{
52-
// Make sure that statements like
53-
//
54-
// e.Name() = L"Fred"; // intended e.Name(L"Fred");
55-
// e.Uri() = newUri; // intended e.Uri(newUri);
56-
//
57-
// are not valid. These are common beginner errors when
58-
// trying to set Windows Runtime properties.
59-
60-
template<typename T, bool default_constructible, typename... ConstructorArgs>
61-
struct validate_rvalue_operations
62-
{
63-
// Make sure we didn't damage default constructor.
64-
static_assert(std::is_default_constructible_v<T> == default_constructible);
65-
66-
// Make sure we didn't damage other constructors.
67-
static_assert((std::is_constructible_v<T, ConstructorArgs> && ...));
68-
69-
// Make sure we didn't damage copy and move constructors.
70-
static_assert(std::is_copy_constructible_v<T>);
71-
static_assert(std::is_move_constructible_v<T>);
72-
73-
// Make sure rvalue assignment is disallowed, but lvalue is still okay.
74-
static_assert(!std::is_assignable_v<T&&, T>);
75-
static_assert((!std::is_assignable_v<T&&, ConstructorArgs> && ...));
76-
static_assert(std::is_assignable_v<T&, T>);
77-
static_assert((std::is_assignable_v<T&, ConstructorArgs> && ...));
78-
79-
constexpr static bool validate()
80-
{
81-
// Dummy method. Exists only to force instantiation of type so the static_assert's will fire.
82-
return true;
83-
}
84-
};
85-
86-
static_assert(validate_rvalue_operations<hstring, true, const wchar_t*>::validate());
87-
static_assert(validate_rvalue_operations<Windows::Foundation::IInspectable, true, std::nullptr_t>::validate());
88-
static_assert(validate_rvalue_operations<Windows::Foundation::IActivationFactory, true, std::nullptr_t>::validate());
89-
static_assert(validate_rvalue_operations<Windows::Foundation::IAsyncOperation<int32_t>, true, std::nullptr_t>::validate());
90-
static_assert(validate_rvalue_operations<Windows::Foundation::EventHandler<Windows::Foundation::IInspectable>, true, std::nullptr_t>::validate());
91-
static_assert(validate_rvalue_operations<Windows::Foundation::IUriRuntimeClass, true, std::nullptr_t>::validate());
92-
static_assert(validate_rvalue_operations<Windows::Foundation::Uri, false /* not default constructible */, std::nullptr_t>::validate());
9347
}

0 commit comments

Comments
 (0)