- C++
- Install gcc-5 and g++5 in Ubuntu 20
- Declare virtual destructors in polymorphic base classes
- coc-ccls not working
- Run single test with ctest
- Virtual Table Explained
- "this" and const member function
- "using" declaration for constructor inheritance
- Add Boost to project in cmake
- CMake install prefix
- tempalte and typename
-
Add following source to
/etc/apt/sources.list. Sourcedeb http://dk.archive.ubuntu.com/ubuntu/ bionic main universe -
Update and install
sudo apt update sudo apt install g++-5
-
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
Why not always declare virtual destructors?
Virtual table and virtual pointer cost space.
Takeaway:
- 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.
- Do not declare virtual destructor if it is not a polymorphic base classes.
./lib not found
cd ~/.config/coc/extensions/node_modules/coc-ccls
ln -s node_modules/ws/lib libUse regular expression
ctest -R '^test_'References:
- https://www.cnblogs.com/zhxmdefj/p/11594459.html
- https://jacktang816.github.io/post/virtualfunction/
Reference:
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:
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 -D CMAKE_INSTALL_PREFIX:PATH=/home/user/opt/ -Bbuild -S.