Concepts and constraints in C++20 work .. I have just redesigned the Gamma GOF patterns using them. Goodbye GOF, really.Introducing Concepts -- http://accu.org/index.php/journals/2157 -- Concepts in C++11 had many false starts. Andrew Sutton show why they are a big deal now they are with us. by Andrew SuttonAn Inline-variant-visitor with C++ Concepts -- http://accu.org/index.php/journals/2160 -- Concepts are abstract. Jonathan Coe and Andrew Sutton provide us with a concrete example of their use.
// Test101Concepts.cpp
//
// Simplest example of a system. Context diagram consists only
// of Input and Output systems.
//
// We use C++20 Concepts to replace policy-based design
//
// Composition
//
// V2: design using C++20 modules
//
// (C) Datasim Education BV 2015-2021
//
#include <string>
#include <iostream>
// Interface contract specification
template<typename T>
concept IInput = requires (T x) { x.message(); };
template<typename T>
concept IOutput = requires (T x, const std::string& s) { x.print(s); };
// I/O stuff
template <typename I, typename O> requires IInput<I> && IOutput<O>
class SUD
{ // System under discussion, using composition
private:
I i;
O o;
public:
SUD(const I& input, const O& output) : i(input), o(output) {}
void run()
{
o.print(i.message());
}
};
// Instance Systems
class Input
{
public:
std::string message() const
{
// Get data from hardware device
return std::string("Good morning");
}
};
class Output
{
public:
void print(const std::string& s) const
{
std::cout << s << std::endl;
}
};
int main()
{
Input i; Output o;
SUD<Input, Output> s(i,o);
s.run();
return 0;
}
A very perceptive insight! Concepts are a big deal and quant developers need to reject Rand's Objectivist Epistemology mindset that has polluted mainstream software development in the last 35 years.Ah - that clears it right up! I promise to do all my PDE work in C++20 from now on.
// Interface contract specification
template<typename T, typename Data>
concept IDiffusion = requires (T c, Data t, Data x) { c.diffusion(t,x); };
template<typename T, typename Data>
concept IDrift = requires (T c, Data t, Data x) { c.drift(t, x); };
template<typename T, typename Data>
concept IDriftDiffusion = IDiffusion<T, Data> && IDrift<T, Data>;
template <typename T, typename Data> requires IDriftDiffusion<T, Data>
class SUD
{ // System under discussion, using composition
// Really a Bridge pattern
private:
T _t;
public:
SUD(const T& t) : _t(t) {}
Data diffusion(Data t, Data x)
{
return _t.diffusion(t, x);
}
Data drift(Data t, Data x)
{
return _t.drift(t, x);
}
};
Have a look at the SDE/GBM model I just posted. For a PDE, just add a reaction term. Ups-a-daisy.Ah - that clears it right up! I promise to do all my PDE work in C++20 from now on.
of courseObject Oriented Programming without Inheritance - ECOOP 2015 -- Bjarne Stroustrup