Skip to content

Commit

Permalink
Add implicit noexcept for move and swap functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hsutter committed May 16, 2023
1 parent d4647ed commit ebe5ebc
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class mystruct {
public: mystruct(mystruct const& that);

public: auto operator=(mystruct const& that) -> mystruct& ;
public: mystruct(mystruct&& that);
public: auto operator=(mystruct&& that) -> mystruct& ;
public: mystruct(mystruct&& that) noexcept;
public: auto operator=(mystruct&& that) noexcept -> mystruct& ;
public: explicit mystruct();

#line 19 "pure2-types-ordering-via-meta-functions.cpp2"
Expand Down Expand Up @@ -129,9 +129,9 @@ auto main() -> int;
auto mystruct::operator=(mystruct const& that) -> mystruct& {
val = that.val;
return *this;}
mystruct::mystruct(mystruct&& that)
mystruct::mystruct(mystruct&& that) noexcept
: val{ std::move(that).val }{}
auto mystruct::operator=(mystruct&& that) -> mystruct& {
auto mystruct::operator=(mystruct&& that) noexcept -> mystruct& {
val = std::move(that).val;
return *this;}
mystruct::mystruct(){}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ class myclass {


#line 8 "pure2-types-smf-and-that-1-provide-everything.cpp2"
public: myclass(myclass&& that);
public: myclass(myclass&& that) noexcept
#line 8 "pure2-types-smf-and-that-1-provide-everything.cpp2"
;


#line 13 "pure2-types-smf-and-that-1-provide-everything.cpp2"
public: auto operator=(myclass const& that) -> myclass& ;


#line 18 "pure2-types-smf-and-that-1-provide-everything.cpp2"
public: auto operator=(myclass&& that) -> myclass& ;
public: auto operator=(myclass&& that) noexcept
#line 18 "pure2-types-smf-and-that-1-provide-everything.cpp2"
-> myclass& ;


#line 22 "pure2-types-smf-and-that-1-provide-everything.cpp2"
Expand Down Expand Up @@ -67,7 +71,9 @@ auto main() -> int;
std::cout << "ctor - copy (GENERAL)";
}

myclass::myclass(myclass&& that)
myclass::myclass(myclass&& that) noexcept
#line 8 "pure2-types-smf-and-that-1-provide-everything.cpp2"

: name{ std::move(that).name + "(CM)" }
, addr{ std::move(that).addr }
#line 8 "pure2-types-smf-and-that-1-provide-everything.cpp2"
Expand All @@ -86,7 +92,9 @@ auto main() -> int;
#line 16 "pure2-types-smf-and-that-1-provide-everything.cpp2"
}

auto myclass::operator=(myclass&& that) -> myclass& {
auto myclass::operator=(myclass&& that) noexcept
#line 18 "pure2-types-smf-and-that-1-provide-everything.cpp2"
-> myclass& {
name = std::move(that).name;
addr = std::move(that).addr;
#line 19 "pure2-types-smf-and-that-1-provide-everything.cpp2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ class myclass {


#line 8 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"
public: myclass(myclass&& that);
public: myclass(myclass&& that) noexcept
#line 8 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"
;


#line 13 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"
public: auto operator=(myclass const& that) -> myclass& ;

#line 13 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"
public: auto operator=(myclass&& that) -> myclass& ;
public: auto operator=(myclass&& that) noexcept
#line 13 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"
-> myclass& ;


#line 18 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"
Expand Down Expand Up @@ -70,7 +74,9 @@ auto main() -> int;
std::cout << "ctor - copy (GENERAL)";
}

myclass::myclass(myclass&& that)
myclass::myclass(myclass&& that) noexcept
#line 8 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"

: name{ std::move(that).name + "(CM)" }
, addr{ std::move(that).addr }
#line 8 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"
Expand All @@ -89,7 +95,9 @@ auto main() -> int;
#line 16 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"
}
#line 13 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"
auto myclass::operator=(myclass&& that) -> myclass& {
auto myclass::operator=(myclass&& that) noexcept
#line 13 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"
-> myclass& {
name = std::move(that).name;
addr = std::move(that).addr + "(AC)";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class myclass {


#line 8 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
public: myclass(myclass&& that);
public: myclass(myclass&& that) noexcept
#line 8 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
;


#line 13 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
Expand All @@ -33,7 +35,9 @@ class myclass {
// std::cout << "assign - copy ";
// }

public: auto operator=(myclass&& that) -> myclass& ;
public: auto operator=(myclass&& that) noexcept
#line 18 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
-> myclass& ;


#line 22 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
Expand Down Expand Up @@ -80,7 +84,9 @@ auto main() -> int;
#line 6 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
}

myclass::myclass(myclass&& that)
myclass::myclass(myclass&& that) noexcept
#line 8 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"

: name{ std::move(that).name + "(CM)" }
, addr{ std::move(that).addr }
#line 8 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
Expand All @@ -90,7 +96,9 @@ auto main() -> int;
}

#line 18 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
auto myclass::operator=(myclass&& that) -> myclass& {
auto myclass::operator=(myclass&& that) noexcept
#line 18 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
-> myclass& {
name = std::move(that).name;
addr = std::move(that).addr;
#line 19 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class myclass {
public: myclass(myclass const& that);

#line 4 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
public: myclass(myclass&& that);
public: myclass(myclass&& that) noexcept
#line 4 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
;


#line 8 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
Expand All @@ -33,7 +35,9 @@ class myclass {


#line 18 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
public: auto operator=(myclass&& that) -> myclass& ;
public: auto operator=(myclass&& that) noexcept
#line 18 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
-> myclass& ;


#line 22 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
Expand Down Expand Up @@ -71,7 +75,9 @@ auto main() -> int;
std::cout << "ctor - copy (GENERAL)";
}
#line 4 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
myclass::myclass(myclass&& that)
myclass::myclass(myclass&& that) noexcept
#line 4 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"

: name{ std::move(that).name }
, addr{ std::move(that).addr }
#line 4 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
Expand All @@ -90,7 +96,9 @@ auto main() -> int;
#line 16 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
}

auto myclass::operator=(myclass&& that) -> myclass& {
auto myclass::operator=(myclass&& that) noexcept
#line 18 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
-> myclass& {
name = std::move(that).name;
addr = std::move(that).addr;
#line 19 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ class myclass {
public: auto operator=(myclass const& that) -> myclass& ;

#line 4 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
public: myclass(myclass&& that);
public: myclass(myclass&& that) noexcept
#line 4 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
;

#line 4 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
public: auto operator=(myclass&& that) -> myclass& ;
public: auto operator=(myclass&& that) noexcept
#line 4 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
-> myclass& ;


#line 8 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
Expand Down Expand Up @@ -87,15 +91,19 @@ auto main() -> int;
#line 6 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
}
#line 4 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
myclass::myclass(myclass&& that)
myclass::myclass(myclass&& that) noexcept
#line 4 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"

: name{ std::move(that).name }
, addr{ std::move(that).addr }
#line 4 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
{
std::cout << "ctor - copy (GENERAL)";
}
#line 4 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
auto myclass::operator=(myclass&& that) -> myclass& {
auto myclass::operator=(myclass&& that) noexcept
#line 4 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
-> myclass& {
name = std::move(that).name;
addr = std::move(that).addr;
#line 5 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
Expand Down
16 changes: 12 additions & 4 deletions regression-tests/test-results/pure2-types-that-parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ class myclass {


#line 11 "pure2-types-that-parameters.cpp2"
public: myclass(myclass&& that);
public: myclass(myclass&& that) noexcept
#line 11 "pure2-types-that-parameters.cpp2"
;

#line 11 "pure2-types-that-parameters.cpp2"
public: auto operator=(myclass&& that) -> myclass& ;
public: auto operator=(myclass&& that) noexcept
#line 11 "pure2-types-that-parameters.cpp2"
-> myclass& ;


#line 16 "pure2-types-that-parameters.cpp2"
Expand Down Expand Up @@ -68,7 +72,9 @@ auto main() -> int;
#line 9 "pure2-types-that-parameters.cpp2"
}

myclass::myclass(myclass&& that)
myclass::myclass(myclass&& that) noexcept
#line 11 "pure2-types-that-parameters.cpp2"

: name{ std::move(that).name }
, addr{ std::move(that).addr }
#line 11 "pure2-types-that-parameters.cpp2"
Expand All @@ -77,7 +83,9 @@ auto main() -> int;
#line 14 "pure2-types-that-parameters.cpp2"
}
#line 11 "pure2-types-that-parameters.cpp2"
auto myclass::operator=(myclass&& that) -> myclass& {
auto myclass::operator=(myclass&& that) noexcept
#line 11 "pure2-types-that-parameters.cpp2"
-> myclass& {
name = std::move(that).name;
addr = std::move(that).addr;
return *this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class widget {
public: [[nodiscard]] auto operator<=>(widget const& that) const -> std::strong_ordering = default;
public: widget(widget const& that);
public: auto operator=(widget const& that) -> widget& ;
public: widget(widget&& that);
public: auto operator=(widget&& that) -> widget& ;
public: widget(widget&& that) noexcept;
public: auto operator=(widget&& that) noexcept -> widget& ;
public: explicit widget();

#line 5 "pure2-types-value-types-via-meta-functions.cpp2"
Expand All @@ -48,8 +48,8 @@ class w_widget {
public: [[nodiscard]] auto operator<=>(w_widget const& that) const -> std::weak_ordering = default;
public: w_widget(w_widget const& that);
public: auto operator=(w_widget const& that) -> w_widget& ;
public: w_widget(w_widget&& that);
public: auto operator=(w_widget&& that) -> w_widget& ;
public: w_widget(w_widget&& that) noexcept;
public: auto operator=(w_widget&& that) noexcept -> w_widget& ;
public: explicit w_widget();

#line 10 "pure2-types-value-types-via-meta-functions.cpp2"
Expand All @@ -64,8 +64,8 @@ class p_widget {
public: [[nodiscard]] auto operator<=>(p_widget const& that) const -> std::partial_ordering = default;
public: p_widget(p_widget const& that);
public: auto operator=(p_widget const& that) -> p_widget& ;
public: p_widget(p_widget&& that);
public: auto operator=(p_widget&& that) -> p_widget& ;
public: p_widget(p_widget&& that) noexcept;
public: auto operator=(p_widget&& that) noexcept -> p_widget& ;
public: explicit p_widget();

#line 15 "pure2-types-value-types-via-meta-functions.cpp2"
Expand Down Expand Up @@ -100,9 +100,9 @@ template<typename T> auto test() -> void;
auto widget::operator=(widget const& that) -> widget& {
val = that.val;
return *this;}
widget::widget(widget&& that)
widget::widget(widget&& that) noexcept
: val{ std::move(that).val }{}
auto widget::operator=(widget&& that) -> widget& {
auto widget::operator=(widget&& that) noexcept -> widget& {
val = std::move(that).val;
return *this;}
widget::widget(){}
Expand All @@ -125,9 +125,9 @@ widget::widget(){}
auto w_widget::operator=(w_widget const& that) -> w_widget& {
val = that.val;
return *this;}
w_widget::w_widget(w_widget&& that)
w_widget::w_widget(w_widget&& that) noexcept
: val{ std::move(that).val }{}
auto w_widget::operator=(w_widget&& that) -> w_widget& {
auto w_widget::operator=(w_widget&& that) noexcept -> w_widget& {
val = std::move(that).val;
return *this;}
w_widget::w_widget(){}
Expand All @@ -150,9 +150,9 @@ w_widget::w_widget(){}
auto p_widget::operator=(p_widget const& that) -> p_widget& {
val = that.val;
return *this;}
p_widget::p_widget(p_widget&& that)
p_widget::p_widget(p_widget&& that) noexcept
: val{ std::move(that).val }{}
auto p_widget::operator=(p_widget&& that) -> p_widget& {
auto p_widget::operator=(p_widget&& that) noexcept -> p_widget& {
val = std::move(that).val;
return *this;}
p_widget::p_widget(){}
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/test-results/version
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

cppfront compiler v0.2.1 Build 8515:0551
cppfront compiler v0.2.1 Build 8515:1945
Copyright(c) Herb Sutter All rights reserved

SPDX-License-Identifier: CC-BY-NC-ND-4.0
Expand Down
2 changes: 1 addition & 1 deletion source/build.info
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"8515:0551"
"8515:1945"
15 changes: 9 additions & 6 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4131,12 +4131,15 @@ class cppfront
emit(*n.parameters);
}

// Add implicit noexcept when we implement proper EH
// to handle calling Cpp1 code that throws
//if (!n.throws) {
// printer.add_pad_in_this_line(-25);
// printer.print_cpp2( " noexcept", n.position() );
//}
// For now, adding implicit noexcept only for move and swap functions
if (
n.is_move()
|| n.is_swap()
|| generating_move_from == n.my_decl
)
{
printer.print_cpp2( " noexcept", n.position() );
}

printer.print_cpp2( suffix1, n.position() );

Expand Down
Loading

0 comments on commit ebe5ebc

Please sign in to comment.