Add little features topic

This commit is contained in:
Alex Hirsch 2021-01-21 15:08:19 +01:00
parent 8410f097e2
commit 585965ec7c
6 changed files with 130 additions and 0 deletions

1
topic/little_features/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/build*

View File

@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.16)
project(little_features CXX)
file(GLOB srcs *.cpp)
foreach(src IN LISTS srcs)
get_filename_component(exe ${src} NAME_WE)
add_executable(${exe} ${src})
target_compile_features(${exe} PRIVATE cxx_std_17)
set_target_properties(${exe} PROPERTIES CXX_EXTENSIONS OFF)
target_compile_options(${exe} PRIVATE
$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wall -Wextra -pedantic>)
target_link_libraries(${exe} -ltbb)
endforeach()

View File

@ -0,0 +1,19 @@
#include <cassert>
struct Vec3 {
float x, y, z;
};
struct Vec2 {
float x, y;
operator Vec3() const { return Vec3{x, y, 0}; }
};
int main()
{
Vec2 v2{1, 2};
Vec3 v3 = v2;
assert(v3.x == 1 && v3.y == 2 && v3.z == 0);
}

View File

@ -0,0 +1,15 @@
#include <algorithm>
#include <array>
#include <execution>
#include <iostream>
int main()
{
std::vector<int> numbers;
for (auto i = 0; i < 1000; i++) {
numbers.push_back(i);
}
// Output should be malformed since there is no synchronization in place.
std::for_each(std::execution::par, numbers.begin(), numbers.end(), [](int i) { std::cout << i << "\n"; });
}

View File

@ -0,0 +1,23 @@
#include <cstdlib>
#include <iostream>
struct Point {
Point(float x, float y) : x(x), y(y) { std::cout << "Constructed at address: " << this << "\n"; }
~Point() { std::cout << "Deconstructed at address: " << this << "\n"; }
float x, y;
};
int main()
{
auto memory = malloc(sizeof(Point));
// Construct a point at a specific location.
Point *p = new (memory) Point(1.0f, 2.0f);
std::cout << "x: " << p->x << "\n";
// Need to manually invoke the destructor.
p->~Point();
free(memory);
}

View File

@ -0,0 +1,59 @@
#include <cassert>
struct Point {
float x, y;
};
struct Rect {
Point tl, br;
float area() const { return (br.x - tl.x) * (br.y - tl.y); }
};
class IShape {
public:
virtual float area() const { assert(false); };
virtual ~IShape() {}
};
class Square : public IShape {
public:
Square(float extend) : m_extend(extend) {}
float area() const override { return m_extend * m_extend; }
private:
float m_extend;
};
int main()
{
Rect r1{{1, 1}, {4, 4}};
assert(r1.area() == 9.0f);
Rect *r1_ptr = &r1;
assert(r1_ptr->area() == 9.0f);
// member variable pointer
auto tl_mem_ptr = &Rect::tl; // Point Rect::*tl_mem_ptr
assert((r1.*tl_mem_ptr).x == 1.0f);
assert((r1_ptr->*tl_mem_ptr).x == 1.0f);
// member function pointer
auto area_memfn_ptr = &Rect::area; // float (Rect::*area_memfn_ptr)() const
assert((r1.*area_memfn_ptr)() == 9.0f);
assert((r1_ptr->*area_memfn_ptr)() == 9.0f);
// Virtual
Square square{2.0f};
IShape *shape_ptr = &square;
// virtual member function pointer
auto virt_area_memfn_ptr = &IShape::area; // float (IShape::*virt_area_memfn_ptr)() const
assert((square.*virt_area_memfn_ptr)() == 4.0f);
assert((shape_ptr->*virt_area_memfn_ptr)() == 4.0f);
}