Skip to content

Commit

Permalink
Construct objects using direct initialization (#513)
Browse files Browse the repository at this point in the history
Direct initialization is simpler than copy initialization, and shorter
(you don't need to specify the type / constructor name on both the LHS
and RHS of the initialization line).

These two forms should have the same effect in C++17:
https://stackoverflow.com/a/41891708 pre-C++17, direct initialization
was typically faster / simpler (since copy initialization might incur
copies).
  • Loading branch information
CodingCanuck authored Aug 3, 2022
1 parent cba2c46 commit d9d621c
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 25 deletions.
2 changes: 1 addition & 1 deletion eventuals/event-loop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ bool EventLoop::HasDefault() {
void EventLoop::ConstructDefaultAndRunForeverDetached() {
ConstructDefault();

auto thread = std::thread([]() {
std::thread thread([]() {
EventLoop::Default().RunForever();
});

Expand Down
2 changes: 1 addition & 1 deletion eventuals/grpc/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ auto Server::Unimplemented(ServerContext* context) {
<< "Dropping call for host " << context->host()
<< " and path = " << context->method();

auto status = ::grpc::Status(
::grpc::Status status(
::grpc::UNIMPLEMENTED,
context->method() + " for host " + context->host());

Expand Down
8 changes: 2 additions & 6 deletions eventuals/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -599,12 +599,8 @@ struct _HTTP final {
}

// Assign key and value.
auto key = std::string(
line.cbegin(),
column_iterator);
auto value = std::string(
column_iterator + 1,
line.cend());
std::string key(line.cbegin(), column_iterator);
std::string value(column_iterator + 1, line.cend());

// Remove leading and trailing spaces.
key = absl::StripAsciiWhitespace(key);
Expand Down
2 changes: 1 addition & 1 deletion test/conditional.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ TEST(ConditionalTest, Fail) {
return Eventual<int>()
.raises<std::runtime_error>()
.start([](auto& k) {
auto thread = std::thread(
std::thread thread(
[&k]() mutable {
k.Fail(std::runtime_error("error"));
});
Expand Down
10 changes: 5 additions & 5 deletions test/eventual.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ TEST(EventualTest, Succeed) {
return Eventual<int>()
.context(5)
.start([](auto& context, auto& k) {
auto thread = std::thread(
std::thread thread(
[&context, &k]() mutable {
k.Start(context);
});
Expand All @@ -45,7 +45,7 @@ TEST(EventualTest, Succeed) {
>> Eventual<int>()
.context(9)
.start([](auto& context, auto& k, auto&& value) {
auto thread = std::thread(
std::thread thread(
[value, &context, &k]() mutable {
k.Start(context - value);
});
Expand Down Expand Up @@ -78,7 +78,7 @@ TEST(EventualTest, Fail) {
.raises()
.context("error")
.start([](auto& error, auto& k) {
auto thread = std::thread(
std::thread thread(
[&error, &k]() mutable {
k.Fail(std::runtime_error(error));
});
Expand Down Expand Up @@ -154,7 +154,7 @@ TEST(EventualTest, Reuse) {
return (Eventual<int>()
.context(i)
.start([](auto& context, auto& k) {
auto thread = std::thread(
std::thread thread(
[&context, &k]() mutable {
k.Start(context);
});
Expand All @@ -164,7 +164,7 @@ TEST(EventualTest, Reuse) {
>> Eventual<int>()
.context(9)
.start([](auto& context, auto& k, auto&& value) {
auto thread = std::thread(
std::thread thread(
[value, &context, &k]() mutable {
k.Start(context - value);
});
Expand Down
6 changes: 3 additions & 3 deletions test/lock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TEST(LockTest, Succeed) {
auto e1 = [&]() {
return Eventual<std::string>()
.start([](auto& k) {
auto thread = std::thread(
std::thread thread(
[&k]() mutable {
k.Start("t1");
});
Expand All @@ -39,7 +39,7 @@ TEST(LockTest, Succeed) {
auto e2 = [&]() {
return Eventual<std::string>()
.start([](auto& k) {
auto thread = std::thread(
std::thread thread(
[&k]() mutable {
k.Start("t2");
});
Expand Down Expand Up @@ -80,7 +80,7 @@ TEST(LockTest, Fail) {
>> Eventual<std::string>()
.raises<std::runtime_error>()
.start([](auto& k) {
auto thread = std::thread(
std::thread thread(
[&k]() mutable {
k.Fail(std::runtime_error("error"));
});
Expand Down
6 changes: 3 additions & 3 deletions test/static-thread-pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ TEST(StaticThreadPoolTest, Reschedulable) {
return Eventual<void>()
.start([&id](auto& k) {
EXPECT_EQ(id, std::this_thread::get_id());
auto thread = std::thread(
std::thread thread(
[&id, &k]() {
EXPECT_NE(id, std::this_thread::get_id());
k.Start();
Expand Down Expand Up @@ -133,7 +133,7 @@ TEST(StaticThreadPoolTest, Spawn) {
return Eventual<void>()
.start([&id](auto& k) {
EXPECT_EQ(id, std::this_thread::get_id());
auto thread = std::thread(
std::thread thread(
[&id, &k]() {
EXPECT_NE(id, std::this_thread::get_id());
k.Start();
Expand Down Expand Up @@ -167,7 +167,7 @@ TEST(StaticThreadPoolTest, SpawnFail) {
.raises<std::runtime_error>()
.start([&id](auto& k) {
EXPECT_EQ(id, std::this_thread::get_id());
auto thread = std::thread(
std::thread thread(
[&id, &k]() {
EXPECT_NE(id, std::this_thread::get_id());
k.Fail(std::runtime_error("error"));
Expand Down
4 changes: 2 additions & 2 deletions test/stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ TEST(StreamTest, InterruptStream) {
})
>> Loop<int>()
.body([&](auto& k, auto&&) {
auto thread = std::thread(
std::thread thread(
[&]() mutable {
while (!triggered.load()) {
std::this_thread::yield();
Expand Down Expand Up @@ -275,7 +275,7 @@ TEST(StreamTest, InterruptLoop) {
k.Next();
})
.body([&](auto&, auto& k, auto&, auto&&) {
auto thread = std::thread(
std::thread thread(
[&]() mutable {
while (!triggered.load()) {
std::this_thread::yield();
Expand Down
4 changes: 2 additions & 2 deletions test/then.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ TEST(ThenTest, Succeed) {
return Eventual<int>()
.context(1)
.start([](auto& value, auto& k) {
auto thread = std::thread(
std::thread thread(
[&value, &k]() mutable {
k.Start(value);
});
Expand Down Expand Up @@ -71,7 +71,7 @@ TEST(ThenTest, Fail) {
return Eventual<int>()
.raises<std::runtime_error>()
.start([](auto& k) {
auto thread = std::thread(
std::thread thread(
[&k]() mutable {
k.Fail(std::runtime_error("error"));
});
Expand Down
2 changes: 1 addition & 1 deletion test/timer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ TEST_F(EventLoopTest, InterruptTimer) {

k.Start();

auto thread = std::thread([&]() {
std::thread thread([&]() {
interrupt.Trigger();
});

Expand Down

0 comments on commit d9d621c

Please sign in to comment.