Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ cmake_install.cmake

# Ignore alternate directory of exercises that is used for building
build_exercises/
build_hello-world/
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ script:
- bin/fetch-configlet
- bin/configlet lint .
- bin/check-configlet-fmt.sh
- bin/check-hello-world.sh
- bin/check-exercises.sh
28 changes: 28 additions & 0 deletions bin/check-hello-world.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash
#
# Test that the hello-world exercise compiles with no changes and fails.

# Fail if any command fails
set -e

repo=$(cd "$(dirname "$0")/.." && pwd)

hello_world_tmp_dir="$repo"/build_hello-world
hello_world_dir="$repo"/exercises/hello-world

mkdir -p "$hello_world_tmp_dir"

cd "$hello_world_tmp_dir"

# Configuring should work.
cmake -G Ninja "$hello_world_dir"

echo "Building hello-world, which should fail."

# The build will fail, since it runs a failing test.
if cmake --build .; then
# The build succeeded, which is not supposed to happen.
exit 1
fi

cd "$repo"
2 changes: 1 addition & 1 deletion exercises/hello-world/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using namespace std;

namespace hello_world
{

string hello()
{
return "Hello, World!";
Expand Down
16 changes: 16 additions & 0 deletions exercises/hello-world/hello_world.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "hello_world.h"

// Use everything from the 'std' namespace.
// This lets us write 'string' instead of 'std::string'.
using namespace std;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that really a good idea? This is actually suggesting to use using namespace std; all the time. Not a good coding style

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

People tend to disagree on this point - See CppCoreGuidelines and Google.

It's generally safe to use in a small, self-contained source file, though there are potential issues in large codebases. It is one of those conveniences that I see as being very helpful while learning the language and potentially dangerous later. You're going to almost never hit issues from it in a toy problem.

That said, I mostly use this in toy problems because I'm lazy and it's low- to no-risk. Do you think it would be better to encourage new learners to use using std::string; instead of the whole namespace?


namespace hello_world {

// Define the function itself. This could have also been written as:
// std::string hello_world::hello()
string hello() {
// Return the string we need.
return "Fix me!";
}

} // namespace hello_world
12 changes: 11 additions & 1 deletion exercises/hello-world/hello_world.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
// This is an include guard.
// You could alternatively use '#pragma once'
// See https://en.wikipedia.org/wiki/Include_guard
#if !defined(HELLO_WORLD_H)
#define HELLO_WORLD_H

// Include the string header so that we have access to 'std::string'
#include <string>

// Declare a namespace for the function(s) we are exporting.
// https://en.cppreference.com/w/cpp/language/namespace
namespace hello_world {

// Declare the 'hello()' function, which takes no arguments and returns a
// 'std::string'. The function itself is defined in the hello_world.cpp source
// file. Because it is inside of the 'hello_world' namespace, it's full name is
// 'hello_world::hello()'.
std::string hello();

}
} // namespace hello_world

#endif
7 changes: 6 additions & 1 deletion exercises/hello-world/hello_world_test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// Include the header file with the definitions of the functions you create.
#include "hello_world.h"

// Include the test framework.
#include "test/catch.hpp"

// Declares a single test.
TEST_CASE("test_hello")
{
REQUIRE("Hello, World!" == hello_world::hello());
// Check if your function returns "Hello, World!".
REQUIRE(hello_world::hello() == "Hello, World!");
}