patterncppMinor
From new Q to compiler in 30 seconds
Viewed 0 times
fromsecondsnewcompiler
Problem
Reviewing code doesn't necessarily require actually building it, but it's often helpful to do so in order to evaluate fully. I usually create a
Update: I've just created a Python
Note: This code and its companion Python project are now available in a github repo.
This will automatically parse the
For much code in many questions, all that is then required is to navigate to the
The executable (if successfully created) will be created in
Note that this will not work if there are special things needed by the code in question. For instance, this code itself will not build unless this line is added to the
The reason is that it uses the C++17
CMake project and build from there. Since we're all about code here, naturally, I decided to automate part of the process. Specifically, here's how to use this code: - Hit "edit" on the question of interest that contains one or more C or C++ source code files
- Select the entire editable question and paste into a local text file with some appropriate name (this one might be
autoproject.md)
- run this code using the command line
autoproject project.md
Update: I've just created a Python
.md file fetcher that replaces the first two steps above.Note: This code and its companion Python project are now available in a github repo.
This will automatically parse the
project.md file and extract the files it finds to a directory tree like this.project
├── build (empty subdirectory)
├── CMakeLists.txt (generated)
└── src
├── CMakeLists.txt (generated)
├── project.cpp (extracted)
├── test.cpp (extracted)
└── project.h (extracted)
For much code in many questions, all that is then required is to navigate to the
build directory and then type:cmake ..
makeThe executable (if successfully created) will be created in
build/src and will be named project (or whatever more meaningful name you have given the original .md file). Examples of questions for which this works are Recursive Breadth First Search for Knights Tour and Hangman on the C++ommand line.Note that this will not work if there are special things needed by the code in question. For instance, this code itself will not build unless this line is added to the
src/CMakeLists.txt file (assuming g++):target_link_libraries(${EXECUTABLE_NAME} stdc++fs)The reason is that it uses the C++17
filesystem feature which is still in the experimental namesSolution
for (std::string line; getline(in, line); ) {This for loop looks weird.
It looks weird because it's a while loop where you've taken advantage of the ability to declare a variable.
I think it'd be better as a while loop:
std::string line;
while(getline(in, line)) {It's longer, but doesn't make me go "wait, what?" anymore.
// stop writing if non-indented line or EOF
if (!line.empty() && !isspace(line[0])) {Somewhere else you use
isIndented(line). Here your comment says "if non-indented line". Why are you not using your own utility function?Code Snippets
for (std::string line; getline(in, line); ) {std::string line;
while(getline(in, line)) {// stop writing if non-indented line or EOF
if (!line.empty() && !isspace(line[0])) {Context
StackExchange Code Review Q#124307, answer score: 2
Revisions (0)
No revisions yet.