Dicking around with template aliases and raii
This commit is contained in:
parent
6674d44fd0
commit
419970d6cd
2 changed files with 30 additions and 0 deletions
7
makefile
7
makefile
|
@ -36,7 +36,10 @@ HDRS := $(wildcard src/*.hpp)
|
|||
# Specify the object files in the build directory
|
||||
OBJS := $(patsubst src/%.cpp,build/%.o,$(SRCS))
|
||||
|
||||
# Print green after success
|
||||
all: $(TARGET)
|
||||
make size
|
||||
@echo -e "\033[0;32m[Success]\033[0m"
|
||||
|
||||
# For convenience
|
||||
run: $(TARGET)
|
||||
|
@ -76,6 +79,10 @@ tar: $(TARGET_TAR)
|
|||
# Sign the tar
|
||||
sign: $(TARGET_SIG)
|
||||
|
||||
# Check the assembly
|
||||
asm: $(TARGET)
|
||||
$(OBJDUMP) -d $(TARGET)
|
||||
|
||||
$(TARGET_SIG): $(TARGET_TAR)
|
||||
minisign -Sm $<
|
||||
|
||||
|
|
23
src/main.cpp
23
src/main.cpp
|
@ -1,7 +1,25 @@
|
|||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include "point.hpp"
|
||||
|
||||
template <typename T> struct Option {
|
||||
private:
|
||||
T value;
|
||||
bool is_some;
|
||||
|
||||
public:
|
||||
Option() : is_some(false) {}
|
||||
Option(T value) : value(value), is_some(true) {}
|
||||
};
|
||||
|
||||
// Templated alias for unique_ptr
|
||||
template <typename T> using Box = std::unique_ptr<T>;
|
||||
|
||||
template <typename T, typename... Args> Box<T> Enbox(Args &&...args) {
|
||||
return std::make_unique<T>(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
Point p = Point(10, 5);
|
||||
std::cout << p << std::endl;
|
||||
|
@ -13,7 +31,12 @@ int main(int argc, char *argv[]) {
|
|||
std::cout << "Distance is not the same" << std::endl;
|
||||
}
|
||||
|
||||
Box<Point> p_box = std::make_unique<Point>(10, 5);
|
||||
Box<Point> p_box2 = Enbox<Point>(10, 5);
|
||||
|
||||
std::cout << p << std::endl;
|
||||
std::cout << *p_box << std::endl;
|
||||
std::cout << *p_box2 << std::endl;
|
||||
std::cout << "Hello, World!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue