struct graph
This commit is contained in:
5
examples/types/CMakeLists.txt
Normal file
5
examples/types/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(cex_type_demo LANGUAGES CXX)
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
add_executable(type-demo main.cpp)
|
||||
2
examples/types/main.cpp
Normal file
2
examples/types/main.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "types.hpp"
|
||||
int main() { demo::Box<int> box(1); box.run(2); }
|
||||
2
examples/types/meson.build
Normal file
2
examples/types/meson.build
Normal file
@@ -0,0 +1,2 @@
|
||||
project('cex-type-demo', 'cpp', default_options: ['cpp_std=c++23'])
|
||||
executable('type-demo', 'main.cpp')
|
||||
25
examples/types/types.hpp
Normal file
25
examples/types/types.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
#include <concepts>
|
||||
#include <vector>
|
||||
namespace demo {
|
||||
template<class T> concept Addable = requires(T a) { a + a; };
|
||||
template<class T> concept Sized = requires(T a) { a.size(); };
|
||||
struct Base { int id; };
|
||||
struct Helper { int work() { return 1; } };
|
||||
template<Addable T>
|
||||
requires std::copyable<T> && (Addable<T> || Sized<T>)
|
||||
struct Box : Base {
|
||||
T value;
|
||||
std::vector<T> entries;
|
||||
Helper helper;
|
||||
using value_type = T;
|
||||
Box(Addable auto x) requires std::constructible_from<T, decltype(x)> : value(x) { helper.work(); }
|
||||
template<Sized U> requires requires(U u) { u.size(); }
|
||||
void set(const U& u) requires Addable<T> { helper.work(); }
|
||||
void run(std::integral auto n) { helper.work(); }
|
||||
template<class U> requires std::convertible_to<U, T>
|
||||
void assign(U u) { value = u; }
|
||||
};
|
||||
using IntBox = Box<int>;
|
||||
enum class State { idle, ready };
|
||||
}
|
||||
Reference in New Issue
Block a user