CPP Best Practices and Notes -- Part 9

Notes taken from https://www.learncpp.com/

9.2 Arrays II

  • Explicitly initialize arrays, even if they would be initialized without an initializer list.

  • When passing arrays to functions, C++ does not copy an array. The actual array is passed.

    • To ensure a function does not modify the array passed into it, make the array const:
      1
      2
      3
      void functionWithArray(const int arr[5]) {
      std::size(arr); // note that this will cause error!!
      }
  • When you are in the same function that a array is
    declared in, sizeof() will return the total size of the array (array length multiplied by element size)

    • When sizeof is used on an array that has been passed to a function, it doesn’t error
      out like std::size() does. Instead, it returns the size of a pointer.
Read more

Set modules to false in babel

Version 2 of webpack supports ES6 modules syntax natively, meaning you can use import and export without a tool like babel to handle this for you.

So when we config babel-preset-env we can set “modules” to false, instead of using babel to compiling then into other module type.

Hexo Asset Folders

In root’s _config.yml, set post_asset_folder to true:

1
post_asset_folder: true

When you create a new post, hexo will generate an asset folder with the same name as the post.

To include assets in markdown:

1
2
3
{% asset_path slug %}
{% asset_img slug [title] %}
{% asset_link slug [title] %}

Examples:

1
2
{% asset_img example.jpg This is an example image %}
{% asset_img "spaced asset.jpg" "spaced title" %}

babel-polyfill and babel-runtime

Use @babel/runtime when you don’t want to pollute global namespace(e.g. you are building a library).

Install @babel/plugin-transform-runtime as development dependency.

1
npm install --save-dev @babel/plugin-transform-runtime

Install @babel/runtime as a production dependency.

1
npm install --save @babel/runtime

Use @babel/polyfill for writing an app.

Source: https://babeljs.io/docs/en/babel-plugin-transform-runtime