Resources for C++

by Fredrik Daneby

These programs are run using g++ 14.2.0) on Ubuntu. CPP version 202302.

Useful links

The GNU compiler collection[ https://gcc.gnu.org/ ]
The GNU debugger[ https://sourceware.org/gdb/ ]
C++ reference[ https://en.cppreference.com/ ]
Compiler explorer[ https://godbolt.org/ ]

Snippets

Preprocessor

Some macros

#include <print>

#define NUMBER 10

#define FUNCTION(x) (x * x)
#define FUNCTION_WITH_ARGUMENTS(...) (__VA_ARGS__)
#define FUNCTION_WITH_TWO_ARGUMENTS(...) (#__VA_ARGS__)

#define CONCATENATOR(start,end) start##end

int main( void )
{

    /* Some predefined macros */
    std::print("CPP version: {}.\n", __cplusplus);
    std::print("File: {}.\n", __FILE__);
    std::print("Function: {}.\n", __func__);
    std::print("Line: {}.\n", __LINE__);
    std::print("Date: {}.\n", __DATE__);
    std::print("Time: {}.\n", __TIME__);
    std::print("More threads than 1: {}\n", (bool) __STDCPP_THREADS__);

    /* Examples of text-replacing macros with and without arguments */
    std::print("Call as function: {}.\n", NUMBER);
    std::print("Call as function: {}.\n", FUNCTION(12));
    std::print("Call as function with one arguments {}.\n", FUNCTION_WITH_ARGUMENTS(10));
    std::print("Call as function with two arguments {}.\n", FUNCTION_WITH_TWO_ARGUMENTS(10, 20));

    /* Example of how the ## operator concatenates identifiers */
    std::print("Join two macro arguments: {}.\n", CONCATENATOR(10, 20));

    /* GCC predefined macros */
    std::print("Include level: {}.\n", __INCLUDE_LEVEL__);
    std::print("GCC version: {}.\n", __VERSION__);
    std::print("ELF object format used: {}.\n", (bool) __ELF__);
	std::print("Pretty function: {}.\n", __PRETTY_FUNCTION__);

    if( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
    {
        std::print("Byte order = little endian\n");
    }
    else 
    {
        std::print("Byte order = big endian\n");
    }

    /* The predefined macro __COUNTER__ increases by 1 for each call */
    std::print("Counter is {}.\n", __COUNTER__);
    std::print("Counter is {}.\n", __COUNTER__);
    std::print("Counter is {}.\n", __COUNTER__);

    return 0;
}
			
			

OOP

Constructors, some operators and destructor calls

#include <iostream>

class point {

    private:
        int x;
        int y;
        int z;

    public:
        point() : x(0), y(0), z(0)
        {
            say(__PRETTY_FUNCTION__, "Default constructor called.");
        }
        point(int a) : x(a), y(0), z(0)
        {
            say(__PRETTY_FUNCTION__, "Constructor with one argument called.");
        }
        point(int a, int b) : x(a), y(b), z(0)
        {
            say(__PRETTY_FUNCTION__, "Constructor with two arguments called.");
        }
        point(int a, int b, int c) : x(a), y(b), z(c)
        {
            say(__PRETTY_FUNCTION__, "Constructor with three arguments called.");
        }
        point(const point & p) : x(p.x), y(p.y), z(p.z)
        {
            say(__PRETTY_FUNCTION__, "Copy constructor called.");
        }
        point & operator=(const point& p)
        {
            say(__PRETTY_FUNCTION__, "Copy assignment operator(=) called.");
            return *this;
        }
        point(point && p)
        {
            say(__PRETTY_FUNCTION__, "Move constructor called.");   
        }
        ~point()
        {
            say(__PRETTY_FUNCTION__, "Destructor called.");
        }
		
        void say(const char* func, const char* msg)
        {
            std::cout << func << ": " << msg;
            std::cout << "[x=" << x << ", y=" << y << ", z=" << z << "]" << std::endl;
        }
};

int main( void )
{

    std::cout << "p1=10" << std::endl;
    point p1 = 10;
    
    std::cout << "p2=20" << std::endl;
    point p2 = 20;

    std::cout << "p1 = p2" << std::endl;
    p1 = p2;

    std::cout << "point p3 = std::move(p1)" << std::endl;
    point p3 = std::move(p1);

    std::cout << "point p4 = {1, 2, 3}" << std::endl;
    point p4 = {1, 2, 3};

    std::cout << "point p5{9, 8, 7}" << std::endl;
    point p5{9, 8, 7};
    
    return 0;
}
		

Constructors, some operators and destructor calls - with delegation

		#include <iostream>

class point {	

    private:
        int x;
        int y;
        int z;

    public:
        point() : point(0, 0, 0)
        {
            say(__PRETTY_FUNCTION__, "Default constructor called.");
        }
        point(int a) : point(a, 0, 0)
        {
            say(__PRETTY_FUNCTION__, "Constructor with one argument called.");
        }
        point(int a, int b) : point(a, b, 0)
        {
            say(__PRETTY_FUNCTION__, "Constructor with two arguments called.");
        }
        point(int a, int b, int c) : x(a), y(b), z(c)
        {
            say(__PRETTY_FUNCTION__, "Constructor with three arguments called.");
        }
        point(const point & p) : point(p.x, p.y, p.z)
        {
            say(__PRETTY_FUNCTION__, "Copy constructor called.");

        }
        point & operator=(const point& p)
        {
            say(__PRETTY_FUNCTION__, "Copy assignment operator(=) called.");
            return *this;
        }

        point(point && p)
        {
            say(__PRETTY_FUNCTION__, "Move constructor called.");
            
        }
	
        void say(const char* func, const char* msg)
        {
            std::cout << func << ": " << msg;
            std::cout << "[x=" << x << ", y=" << y << ", z=" << z << "]" << std::endl;
        }
};

int main( void )
{

    std::cout << "p1=10" << std::endl;
    point p1 = 10;
    
    std::cout << "p2=20" << std::endl;
    point p2 = 20;

    std::cout << "p1 = p2" << std::endl;
    p1 = p2;

    std::cout << "point p3 = std::move(p1)" << std::endl;
    point p3 = std::move(p1);

    std::cout << "point p4 = {1, 2, 3}" << std::endl;
    point p4 = {1, 2, 3};

    std::cout << "point p5{9, 8, 7}" << std::endl;
    point p5{9, 8, 7};
    
    return 0;
}