Skip to content

Latest commit

 

History

History
179 lines (126 loc) · 5.4 KB

File metadata and controls

179 lines (126 loc) · 5.4 KB

C++

Install gcc-5 and g++5 in Ubuntu 20 :linux:setup:

  1. Add following source to /etc/apt/sources.list. Source

    deb http://dk.archive.ubuntu.com/ubuntu/ bionic main universe
    
  2. Update and install

    sudo apt update
    sudo apt install g++-5
  3. Configure Alternatives

    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 10
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 20
    
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.3 10
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.4 20
    
    sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
    sudo update-alternatives --set cc /usr/bin/gcc
    
    sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
    sudo update-alternatives --set c++ /usr/bin/g++

    Then select the version

    sudo update-alternatives --config gcc
    sudo update-alternatives --config g++

References:

- Source 2

Declare virtual destructors in polymorphic base classes

Why not always declare virtual destructors?

Virtual table and virtual pointer cost space.

Takeaway:

  1. polymorphic base classes should always declare a virtual destructor. If the base class already has a virtual member function, it should also declare a virtual destructor.
  2. Do not declare virtual destructor if it is not a polymorphic base classes.

coc-ccls not working :setup:vim:linux:

./lib not found

cd ~/.config/coc/extensions/node_modules/coc-ccls
ln -s node_modules/ws/lib lib

Reference

Run single test with ctest

Use regular expression

ctest -R '^test_'

Reference

Virtual Table Explained

References:

"this" and const member function

Reference:

"using" declaration for constructor inheritance

Using-declaration introduces a member of a base class into the derived class definition, such as to expose a protected member of base as public member of derived.

It maybe helpful in unit test, when you want to test the protected methods, or get private member through protected methods.

#include <iostream>
struct B {
    virtual void f(int) { std::cout << "B::f\n"; }
    void g(char)        { std::cout << "B::g\n"; }
    void h(int)         { std::cout << "B::h\n"; }
 protected:
    int m; // B::m is protected
    typedef int value_type;
};

struct D : B {
    using B::m; // D::m is public
    using B::value_type; // D::value_type is public

    using B::f;
    void f(int) { std::cout << "D::f\n"; } // D::f(int) overrides B::f(int)
    using B::g;
    void g(int) { std::cout << "D::g\n"; } // both g(int) and g(char) are visible
                                           // as members of D
    using B::h;
    void h(int) { std::cout << "D::h\n"; } // D::h(int) hides B::h(int)
};

int main()
{
    D d;
    B& b = d;

//    b.m = 2; // error, B::m is protected
    d.m = 1; // protected B::m is accessible as public D::m
    b.f(1); // calls derived f()
    d.f(1); // calls derived f()
    d.g(1); // calls derived g(int)
    d.g('a'); // calls base g(char)
    b.h(1); // calls base h()
    d.h(1); // calls derived h()
}

Reference:

Add Boost to project in cmake


set(BOOST_ROOT /opt/homebrew/Cellar/boost/1.76.0)
find_package(Boost)
if(Boost_FOUND)
    message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
    message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
    message(STATUS "Boost_VERSION: ${Boost_VERSION}")
    include_directories(${BOOST_ROOT})
    include_directories(${Boost_INCLUDE_DIRS})
    link_directories(${Boost_LIBRARY_DIRS})
    target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})
endif()

CMake install prefix

cmake -D CMAKE_INSTALL_PREFIX:PATH=/home/user/opt/ -Bbuild -S.

Reference

tempalte and typename

Reference