Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
googletest
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
googletest
Commits
b5fb5ba0
Commit
b5fb5ba0
authored
Oct 29, 2019
by
vslashg
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2527 from PiotrNycz:gmock_prevent_return_ref_to_store_temporaries_2
PiperOrigin-RevId: 277061341
parents
a1f71dd5
208c2f6b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
0 deletions
+40
-0
gmock-actions.h
googlemock/include/gmock/gmock-actions.h
+4
-0
gmock-actions_test.cc
googlemock/test/gmock-actions_test.cc
+36
-0
No files found.
googlemock/include/gmock/gmock-actions.h
View file @
b5fb5ba0
...
...
@@ -1052,6 +1052,10 @@ inline internal::ReturnRefAction<R> ReturnRef(R& x) { // NOLINT
return
internal
::
ReturnRefAction
<
R
>
(
x
);
}
// Prevent using ReturnRef on reference to temporary.
template
<
typename
R
,
R
*
=
nullptr
>
internal
::
ReturnRefAction
<
R
>
ReturnRef
(
R
&&
)
=
delete
;
// Creates an action that returns the reference to a copy of the
// argument. The copy is created when the action is constructed and
// lives as long as the action.
...
...
googlemock/test/gmock-actions_test.cc
View file @
b5fb5ba0
...
...
@@ -46,6 +46,7 @@
#include <iterator>
#include <memory>
#include <string>
#include <type_traits>
#include "gmock/gmock.h"
#include "gmock/internal/gmock-port.h"
#include "gtest/gtest.h"
...
...
@@ -647,6 +648,41 @@ TEST(ReturnRefTest, IsCovariant) {
EXPECT_EQ
(
&
derived
,
&
a
.
Perform
(
std
::
make_tuple
()));
}
template
<
typename
T
,
typename
=
decltype
(
ReturnRef
(
std
::
declval
<
T
&&>
()))
>
bool
CanCallReturnRef
(
T
&&
)
{
return
true
;
}
bool
CanCallReturnRef
(
Unused
)
{
return
false
;
}
// Tests that ReturnRef(v) is working with non-temporaries (T&)
TEST
(
ReturnRefTest
,
WorksForNonTemporary
)
{
int
scalar_value
=
123
;
EXPECT_TRUE
(
CanCallReturnRef
(
scalar_value
));
std
::
string
non_scalar_value
(
"ABC"
);
EXPECT_TRUE
(
CanCallReturnRef
(
non_scalar_value
));
const
int
const_scalar_value
{
321
};
EXPECT_TRUE
(
CanCallReturnRef
(
const_scalar_value
));
const
std
::
string
const_non_scalar_value
(
"CBA"
);
EXPECT_TRUE
(
CanCallReturnRef
(
const_non_scalar_value
));
}
// Tests that ReturnRef(v) is not working with temporaries (T&&)
TEST
(
ReturnRefTest
,
DoesNotWorkForTemporary
)
{
auto
scalar_value
=
[]()
->
int
{
return
123
;
};
EXPECT_FALSE
(
CanCallReturnRef
(
scalar_value
()));
auto
non_scalar_value
=
[]()
->
std
::
string
{
return
"ABC"
;
};
EXPECT_FALSE
(
CanCallReturnRef
(
non_scalar_value
()));
// cannot use here callable returning "const scalar type",
// because such const for scalar return type is ignored
EXPECT_FALSE
(
CanCallReturnRef
(
static_cast
<
const
int
>
(
321
)));
auto
const_non_scalar_value
=
[]()
->
const
std
::
string
{
return
"CBA"
;
};
EXPECT_FALSE
(
CanCallReturnRef
(
const_non_scalar_value
()));
}
// Tests that ReturnRefOfCopy(v) works for reference types.
TEST
(
ReturnRefOfCopyTest
,
WorksForReference
)
{
int
n
=
42
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment