Skip to content

Instantly share code, notes, and snippets.

@sharkdp
Created March 30, 2021 12:59
Show Gist options
  • Save sharkdp/76a219b864145bfce0e81f574afeb547 to your computer and use it in GitHub Desktop.
Save sharkdp/76a219b864145bfce0e81f574afeb547 to your computer and use it in GitHub Desktop.
A todo() macro for C++, inspired by Rusts todo!()
#include <iostream>
struct todo_statement {
public:
todo_statement(const char* file, const int line)
: m_file(file), m_line(line) {}
template <typename T>
operator T() {
std::cerr << "Error: Remove todo() statement in " << m_file << ":" << m_line
<< std::endl;
throw;
}
const char* m_file;
const int m_line;
};
#define todo() (todo_statement{__FILE__, __LINE__})
@sharkdp
Copy link
Author

sharkdp commented Mar 30, 2021

The idea is that you can put todo() in places where you still need to write some code. Due to the generic operator T() implementation, the resulting expression can be cast into any type. At least during compile time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment