Skip to content

Commit b777e0e

Browse files
Merge pull request #621 from andreasfertig/fixIssue620
Fixed #620: Do not show move construction for NRVO return.
2 parents 34cf210 + 544b3a1 commit b777e0e

11 files changed

Lines changed: 81 additions & 15 deletions

CodeGenerator.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4141,7 +4141,11 @@ void CodeGenerator::InsertArg(const ReturnStmt* stmt)
41414141
mOutputFormatHelper.Append(' ');
41424142

41434143
if(not temporaryFinder.Found()) {
4144-
InsertArg(retVal);
4144+
if(const auto* nrvoVD = stmt->getNRVOCandidate()) {
4145+
mOutputFormatHelper.Append(GetName(*nrvoVD));
4146+
} else {
4147+
InsertArg(retVal);
4148+
}
41454149
} else {
41464150
mOutputFormatHelper.Append(temporaryFinder.Name());
41474151
}

tests/Class2Test.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Test
3333
{
3434
Test tmp = Test(*this) /* NRVO variable */;
3535
this->operator++();
36-
return Test(static_cast<Test &&>(tmp).operator int());
36+
return tmp;
3737
}
3838

3939
inline Test & operator+=(const Test & rhs)

tests/ClassOperatorHandler2Test.expect

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Foo
3030
printf(" ======\n");
3131
++this->mX;
3232
printf(" ++++++\n");
33-
return Foo(static_cast<Foo &&>(old));
33+
return old;
3434
}
3535

3636
inline Foo & operator++()
@@ -45,7 +45,7 @@ class Foo
4545
printf(" ======\n");
4646
--this->mX;
4747
printf(" ++++++\n");
48-
return Foo(static_cast<Foo &&>(old));
48+
return old;
4949
}
5050

5151
inline Foo & operator--()

tests/ClassOperatorHandler9Test.expect

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ MyVector<int> operator+(const MyVector<int> & a, const MyVector<int> & b)
8383
result.operator[](s) = (a.operator[](s) + b.operator[](s));
8484
}
8585

86-
return MyVector<int>(static_cast<MyVector<int> &&>(result));
86+
return result;
8787
}
8888

8989
template<typename T>
@@ -107,7 +107,7 @@ MyVector<int> operator*<int>(const MyVector<int> & a, const MyVector<int> & b)
107107
result.operator[](s) = (a.operator[](s) + b.operator[](s));
108108
}
109109

110-
return MyVector<int>(static_cast<MyVector<int> &&>(result));
110+
return result;
111111
}
112112
#endif
113113

@@ -119,7 +119,7 @@ MyVector<int> Foo(const MyVector<int> & a, const MyVector<int> & b)
119119
result.operator[](s) = (a.operator[](s) + b.operator[](s));
120120
}
121121

122-
return MyVector<int>(static_cast<MyVector<int> &&>(result));
122+
return result;
123123
}
124124

125125
int main()

tests/ClassOperatorHandlerTest.expect

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Test
1717
{
1818
Test tmp = Test{*this} /* NRVO variable */;
1919
this->mX++;
20-
return Test(static_cast<Test &&>(tmp));
20+
return tmp;
2121
}
2222

2323
inline Test operator--()
@@ -30,7 +30,7 @@ class Test
3030
{
3131
Test tmp = Test{*this} /* NRVO variable */;
3232
this->mX--;
33-
return Test(static_cast<Test &&>(tmp));
33+
return tmp;
3434
}
3535

3636
inline Test & operator+=(const Test & rhs)

tests/ClassTest.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Test
2828
{
2929
Test tmp = Test(*this) /* NRVO variable */;
3030
this->operator++();
31-
return Test(static_cast<Test &&>(tmp).operator int());
31+
return tmp;
3232
}
3333

3434
inline Test & operator+=(const Test & rhs)

tests/EduLifeTimeTest10.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Vector createStrings2()
3131
const char *const __temporary19_58[6] = {"This", "is", "a", "vector", "of", "strings"};
3232
Vector v = Vector{std::initializer_list<const char *>{__temporary19_58, 6}} /* NRVO variable */;
3333
/* __temporary19_58 // lifetime ends here */
34-
return Vector(static_cast<Vector &&>(v));
34+
return v;
3535
/* v // lifetime ends here */
3636
}
3737

tests/Issue620.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <cstdio>
2+
#include <utility>
3+
4+
class C {
5+
public:
6+
C() { printf("default constructor\n"); }
7+
~C() { printf("destructor\n"); }
8+
//C(const C&) { printf("copy constructor\n"); }
9+
C(C&&) { printf("move constructor\n"); }
10+
//C& operator=(const C&) { printf("copy assignment\n"); return *this; }
11+
C& operator=(C&&) { printf("move assignment\n"); return *this; }
12+
private:
13+
int x;
14+
};
15+
16+
C f() {
17+
C c;
18+
return c;
19+
}
20+

tests/Issue620.expect

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <cstdio>
2+
#include <utility>
3+
4+
class C
5+
{
6+
7+
public:
8+
inline C()
9+
{
10+
printf("default constructor\n");
11+
}
12+
13+
inline ~C() noexcept
14+
{
15+
printf("destructor\n");
16+
}
17+
18+
inline C(C &&)
19+
{
20+
printf("move constructor\n");
21+
}
22+
23+
inline C & operator=(C &&)
24+
{
25+
printf("move assignment\n");
26+
return *this;
27+
}
28+
29+
30+
private:
31+
int x;
32+
public:
33+
// inline constexpr C(const C &) /* noexcept */ = delete;
34+
// inline C & operator=(const C &) /* noexcept */ = delete;
35+
};
36+
37+
38+
C f()
39+
{
40+
C c = C() /* NRVO variable */;
41+
return c;
42+
}

tests/NRVOHandlerTest.expect

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ struct C
1414
C f()
1515
{
1616
C namedC /* NRVO variable */;
17-
return C(static_cast<const C &&>(namedC));
17+
return namedC;
1818
}
1919

2020
C f2()
2121
{
2222
C namedC;
2323
C namedC2 = C{namedC} /* NRVO variable */;
24-
return C(static_cast<const C &&>(namedC2));
24+
return namedC2;
2525
}
2626

2727
int main()

0 commit comments

Comments
 (0)