cpp

Shared files

A.hpp

#ifndef A_HPP
#define A_HPP

#include <string>

struct A
{
  std::string m_name;

  // For a direct-initiation with a string.
  A(const std::string &);
  // Copy and move constructors.
  A(const A &);
  A(A &&);

  void hello() const;
  ~A();
};

#endif // A_HPP

A.cpp

#include "A.hpp"
#include <iostream>

using namespace std;

A::A(const string &name): m_name(name)
{
  cout << "ctor: " << m_name << endl;
}

A::A(const A &a): m_name(a.m_name + " copied")
{
  cout << "copy-ctor: " << m_name << endl;
}

A::A(A &&a): m_name(a.m_name + " moved")
{
  cout << "move-ctor: " << m_name << endl;
}

void A::hello() const
{
  cout << "Hello from " << m_name << endl;
}

A::~A()
{
  cout << "dtor: " << m_name << endl;
}

hack.hpp

#include <iostream>
#include <string>

struct A
{
  std::string m_name;

  A(const std::string &name): m_name(name)
  {
    std::cout << "ctor: " << m_name << std::endl;
  }

  A(const A &a): m_name(a.m_name + " copied")
  {
    std::cout << "copy-ctor: " << m_name << std::endl;
  }

  A(A &&a): m_name(a.m_name + " moved")
  {
    std::cout << "move-ctor: " << m_name << std::endl;
  }

  void hello() const
  {
    std::cout << "Hello from " << m_name << std::endl;
  }

  ~A()
  {
    std::cout << "dtor: " << m_name << std::endl;
  }
};

timer.hpp

#ifndef TIMER_HPP
#define TIMER_HPP

#include <chrono>
#include <iostream>
#include <string>

struct timer
{
  std::chrono::time_point<std::chrono::system_clock> t0;
  std::string m_message;

  // Tells whether we should print at the start.
  bool m_flag;
  
  timer(const std::string &message, bool flag = false):
    m_message(message), m_flag(flag)
  {
    if (m_flag)
      std::cout << "Starting: " << m_message << '\n';

    t0 = std::chrono::system_clock::now();
  }

  ~timer()
  {
    auto t1 = std::chrono::system_clock::now();
    double dt = static_cast<std::chrono::duration<double>>(t1 - t0).count();

    if (m_flag)
      std::cout << "Finished: ";

    std::cout << m_message << " in " << dt << " s\n";
  }
};

#endif