2. Test C++ with cling#

Interactive C++ cling (documentation ici) with jupyter notebook xeus (documentation ici)

2.1. Interactive C++ basic#

#include <iostream>
using namespace std;
cout << "hello world" <<endl;
hello world

2.2. Function#

double sqr(double a)
{
    return a * a;
}
double a = 2.5;
double asqr = sqr(a);
asqr
6.2500000

2.3. Classe#

class Foo
{
public:

    virtual ~Foo() {}
    
    virtual void print(double value) const
    {
        std::cout << "Foo value = " << value << std::endl;
    }
};
Foo bar;
bar.print(1.2);
Foo value = 1.2

2.4. Polymorphisme#

class Bar : public Foo
{
public:

    virtual ~Bar() {}
    
    virtual void print(double value) const
    {
        std::cout << "Bar value = " << 2 * value << std::endl;
    }
};
Foo* bar2 = new Bar;
bar2->print(1.2);
delete bar2;
Bar value = 2.4

2.5. Template#

#include <typeinfo>

template <class T>
class FooT
{
public:
    
    explicit FooT(const T& t) : m_t(t) {}
    
    void print() const
    {
        std::cout << typeid(T).name() << " m_t = " << m_t << std::endl;
    }
    
private:
    
    T m_t;
};

template <>
class FooT<int>
{
public:
    
    explicit FooT(const int& t) : m_t(t) {}
    
    void print() const
    {
        std::cout << "m_t = " << m_t << std::endl;
    }
    
private:
    
    int m_t;
};
FooT<double> foot1(1.2);
foot1.print();
d m_t = 1.2
FooT<int> foot2(4);
foot2.print();
m_t = 4

2.6. C++11/C++14#

class Foo11
{
public:
    
    Foo11() { std::cout << "Foo11 default constructor" << std::endl; }
    Foo11(const Foo11&) { std::cout << "Foo11 copy constructor" << std::endl; }
    Foo11(Foo11&&) { std::cout << "Foo11 move constructor" << std::endl; }
};
Foo11 f1;
Foo11 f2(f1);
Foo11 f3(std::move(f1));
Foo11 default constructor
Foo11 copy constructor
Foo11 move constructor
#include <vector>

std::vector<int> v = { 1, 2, 3};
auto iter = ++v.begin();
v
{ 1, 2, 3 }
*iter
2

2.6.1. Online documentation#

?std::vector
        <iframe class="xcpp-iframe-pager" src="https://en.cppreference.com/w/cpp/container/vector?action=purge"></iframe>

2.7. Display#

#include <string>
#include <fstream>

#include "xtl/xbase64.hpp"
#include "nlohmann/json.hpp"

namespace nl = nlohmann;

namespace im
{
    struct image
    {
        inline image(const std::string& filename)
        {
            std::ifstream fin(filename, std::ios::binary);
            m_buffer << fin.rdbuf();
        }

        std::stringstream m_buffer;
    };

    nl::json mime_bundle_repr(const image& i)
    {
        auto bundle = nl::json::object();
        bundle["image/png"] = xtl::base64encode(i.m_buffer.str());
        return bundle;
    }
}
im::image marie("marie.png");
marie

png

2.8. Tensor#

#include <iostream>

#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"
#include "xtensor/xview.hpp"

xt::xarray<double> arr1
  {{1.0, 2.0, 3.0},
   {2.0, 5.0, 7.0},
   {2.0, 5.0, 7.0}};

xt::xarray<double> arr2
  {5.0, 6.0, 7.0};

xt::view(arr1, 1) + arr2
  7.
 11.
 14.
#include <iostream>
#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"
xt::xarray<int> arr
  {1, 2, 3, 4, 5, 6, 7, 8, 9};

arr.reshape({3, 3});

std::cout << arr;
{{1, 2, 3},
 {4, 5, 6},
 {7, 8, 9}}

2.9. The END#