Serving the Quantitative Finance Community

 
User avatar
katastrofa
Posts: 7430
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

Re: C++20

September 16th, 2020, 11:06 pm

Nice connection. The satire still applies.
Which programming languages are Lagadonian?
 
User avatar
Cuchulainn
Topic Author
Posts: 20203
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++20

September 17th, 2020, 2:44 pm

Nice connection. The satire still applies.
Which programming languages are Lagadonian?
"trying to change human excretion back into food and trying to extract sunbeams out of cucumbers or teaching mathematics to pupils by writing propositions on wafers and consuming them with "cephalick tincture".[2] The Academy is home to The Engine, a fictitious device resembling a modern computer. "
 
User avatar
Cuchulainn
Topic Author
Posts: 20203
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++20

September 17th, 2020, 2:47 pm

The Engine is a device that generates permutations of word sets. It is found at the Academy of Projectors in Lagado and is described thus by Swift:
“... Every one knew how laborious the usual method is of attaining to arts and sciences; whereas, by his contrivance, the most ignorant person, at a reasonable charge, and with a little bodily labour, might write books in philosophy, poetry, politics, laws, mathematics, and theology, without the least assistance from genius or study.” He then led me to the frame, about the sides, whereof all his pupils stood in ranks. It was twenty feet square, placed in the middle of the room. The superfices was composed of several bits of wood, about the bigness of a die, but some larger than others. They were all linked together by slender wires. These bits of wood were covered, on every square, with paper pasted on them; and on these papers were written all the words of their language, in their several moods, tenses, and declensions; but without any order. The professor then desired me “to observe; for he was going to set his engine at work.” The pupils, at his command, took each of them hold of an iron handle, whereof there were forty fixed round the edges of the frame; and giving them a sudden turn, the whole disposition of the words was entirely changed. He then commanded six-and-thirty of the lads, to read the several lines softly, as they appeared upon the frame; and where they found three or four words together that might make part of a sentence, they dictated to the four remaining boys, who were scribes. This work was repeated three or four times, and at every turn, the engine was so contrived, that the words shifted into new places, as the square bits of wood moved upside down."[2]
 
User avatar
Cuchulainn
Topic Author
Posts: 20203
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++20

January 27th, 2021, 4:40 pm

 
User avatar
ExSan
Posts: 493
Joined: April 12th, 2003, 10:40 am

Re: C++20

January 30th, 2021, 4:15 pm

Thank you!!!
°°° About ExSan bit.ly/3U5bIdq °°°
 
User avatar
Cuchulainn
Topic Author
Posts: 20203
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++20

January 30th, 2021, 10:13 pm

Thank you!!!
:-)
 
User avatar
JohnLeM
Posts: 379
Joined: September 16th, 2008, 7:15 pm

Re: C++20

February 7th, 2021, 9:53 am

I am looking to this. Thank you @Cuchullain, nice to have a C++ refresher !
 
User avatar
Cuchulainn
Topic Author
Posts: 20203
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++20

February 7th, 2021, 11:54 am

I am looking to this. Thank you @Cuchullain, nice to have a C++ refresher !
Do you want to try my quizzes and exercises? :-0
 
User avatar
Cuchulainn
Topic Author
Posts: 20203
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++20

July 14th, 2021, 12:40 pm

C++ is the new Python

https://www.efinancialcareers.co.uk/new ... gn=ED_NEWS

C++ is making a comeback. It ranked fourth on the Tiobe Index as the most popular coding language this month after being rated top by 8% of people. That doesn't exactly put it on a par with C or Java or Python at 11-12%, but it does mean that C++ is up there with the favourites- and that it's continuing a run of increasing popularity that began at the start of 2020.

It never went away.
 
User avatar
Cuchulainn
Topic Author
Posts: 20203
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++20

October 13th, 2022, 10:01 am

// TestGenericVisitorProtean.cpp 

// DJD 2022-10-9 version 0.9 #6
// (C) Datasim Education BV 2022

/*
A really modern implementation of a generic Visitor pattern that is an improvement on other approaches in C++:

        1. std::visit() and std::variant()
        2. Traditional Acyclic Visitor using subtype polymorphism, dynamic casting and 
        possibly multiple inheritance.

The proposed solution is more maintainable than solutions 1 and 2 while at the same time having better performance and reliability properties than solution 2. Our solution uses a combination of
template methods and C++ Concepts.

We take the well-known example of 2d shapes (Point, Line, Circle) that can be visited in different ways. 
The two visitors represent new functionality for drawing and scaling these shapes.

We note that we do not have a class hierarchy nor virtual functions. All behaviour is compile-time driven as it were.

C++ Concepts is a game changer.

This code contains:

        1. Visitor pattern based on C++20 Concepts (provide-requires contracts).
        2. Multimethods (the Holy Grail) .. what C++ tried for 30 years.
        3. Multimethods with variadic parameters, a) object with multiple visitors, 
        b) a visitor with multiple objects.

        WE GIVE A STRIPPED DOWN VERSION TO FIT ON PAGE
        More later.
*/

#include <iostream>

// Visitor with templates,V2
struct Point;

// Using C++20 Concepts to define contracts/protocols
// 1. The baseline contract between GOF Context and Visitor
template <typename Visitor, typename Context>
    concept IVisitor = requires (Visitor & v, Context & c)
{
    v.visit(c);
};

template <typename Visitor, typename Context>
    concept IAccept = requires (Visitor & v, Context & c)
 {
        c.accept(v);
 };
 
 template <typename Visitor, typename Context>
    concept IAcceptVisitor = IVisitor<Visitor, Context> && IAccept<Visitor, Context>;

// Specialisations of Concept interfaces (from 2 to 1 template parameter)
template <typename Visitor>
    concept IPointVisitor = IVisitor<Visitor, Point>;


/* End of Protocol definitions*/


struct Point 
{
    double x;
    double y;

    Point() : x(0.0), y(0.0) {}
    Point(double X, double Y) : x(X), y(Y) {}

    template <typename T> requires IPointVisitor<T>
        void accept(T& t)
    {
        t.visit(*this);
    }
};


// Specific visitors (Draw and Scale)
struct Draw
{
    void visit(Point& p)
    {
        std::cout << "("<< p.x << ", " << p.y << ")\n";
    }
    
  
};


struct Scale
{
    double fac;
    Scale(double factor) : fac(factor) {}
    void visit(Point& p)
    {
        p.x *= fac;
        p.y *= fac;
    }
   
};

// Multimethods, the Holy Grail of C++
template <typename Visitor, typename Context>
    void multimethod(Visitor& v, Context& c) requires IAcceptVisitor<Visitor, Context>
{
        v.visit(c);
        c.accept(v);
}


// Command multipattern ... a list of objects on a single visitor
 template <typename T, typename Visitor>
    void command(Visitor v, T& c) 
 {
       c.accept(v);
 }


template <typename T, typename Visitor, typename ... Types>
    void command(Visitor v , T& arg, Types& ... args) 
{ // 1 visitor, multiple contexts

        command(v, arg);
        command(v, args...);
}

 // Command multipattern ... a list of visitors  on a single object
 template <typename T, typename Visitor>
    void command2(T& c, Visitor v)
  {
      //  c.accept(v);
        v.visit(c);
  }

template <typename T, typename Visitor, typename ... Types>
    void command2(T& arg, Visitor& v, Types& ... args)
{ // 1 context, multiple visitors

        command2(arg, v);
        command2(arg, args...);
 }


int main()
{
  
    // Contract with C++ 20 Concepts
    {
        std::cout << "Contracts, points and lines\n";
        Point p1(2.0, -3.0); 
        Draw draw;
        Scale mod(0.5);

        p1.accept(mod);
        p1.accept(draw);    
        mod.visit(p1);
        draw.visit(p1);    
        p1.accept(mod);
        draw.visit(p1);     
    }

  
    {
        // multimethods
        std::cout << "Multimethod\n";
        Point p(2.0, 4.0); Point p2(20.0, 41.0);
        Point p3(-2.0, -4.0); Point p4(21.0, 42.0);
        Draw draw;
        Scale mod(0.5);

        // Magic
        multimethod(draw, p);
        multimethod(mod, p);
        multimethod(draw, p);

        std::cout << "variadics\n";
        command(draw, p);
        // Multimethods with variadic parameters
        command(draw, p, p2, p3, p4);

        // Object with multiple visitors
        command2(p, draw, mod, draw, mod, draw, mod, draw);
        command2(p2, draw, mod, draw);
    }
 }

21st century C++: Design Pattterns, C++20 Concepts and multimethods

This code contains:

1. Visitor pattern based on C++20 Concepts (provide-requires contracts).
2. Multimethods (the Holy Grail) .. what C++ tried for 30 years.
3. Multimethods with variadic parameters, a) object with multiple visitors,
b) a visitor with multiple objects.
Attachments
TestGenericVisitorProtean.cpp
(4.72 KiB) Downloaded 67 times
 
User avatar
Cuchulainn
Topic Author
Posts: 20203
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++20

October 13th, 2022, 10:07 am

I declare that this work was composed by myself, that the work contained herein is my own except where explicitly stated otherwise in the text..

A student exercise: port GOF patterns to C++ Concepts.
  • I just want to say one word to you. 
  • Just one word.
  • Yes, sir.
  • Are you listening?
  • Yes, I am.
  • Concepts.
  • Exactly how do you mean?
  • There's a great future in C++ Concepts. 
  • Think about it. 
  • Will you think about it?
 
User avatar
bearish
Posts: 5180
Joined: February 3rd, 2011, 2:19 pm

Re: C++20

October 14th, 2022, 2:16 am

I declare that this work was composed by myself, that the work contained herein is my own except where explicitly stated otherwise in the text..

A student exercise: port GOF patterns to C++ Concepts.
  • I just want to say one word to you. 
  • Just one word.
  • Yes, sir.
  • Are you listening?
  • Yes, I am.
  • Concepts.
  • Exactly how do you mean?
  • There's a great future in C++ Concepts. 
  • Think about it. 
  • Will you think about it?
Umm - synaptic connections at work: “plastics - the Great Pacific Garbage Patch!”
 
User avatar
Cuchulainn
Topic Author
Posts: 20203
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++20

October 14th, 2022, 11:24 am

And UCB MFE does C++.