vulkan-renderer icon indicating copy to clipboard operation
vulkan-renderer copied to clipboard

Use or don't use const consistently for function parameters

Open IAmNotHanni opened this issue 5 years ago • 7 comments

Const or not const - that is the question?

MeshBuffer(const wrapper::Device &device, const VkQueue data_transfer_queue,
const std::uint32_t data_transfer_queue_family_index, const VmaAllocator vma_allocator,
const std::string &name, const VkDeviceSize size_of_vertex_structure,
const std::size_t number_of_vertices, void *vertices, const VkDeviceSize size_of_index_structure,
const std::size_t number_of_indices, void *indices);
  • When to use const in function declarations in header files?
  • When to use const in function definitions in cpp files?
  • Which types should be const? Should Vulkan handles be const or not?

IAmNotHanni avatar Sep 05 '20 15:09 IAmNotHanni

When creating a PR for this, we could also resolve the following issue:

ImGUIOverlay(const ImGUIOverlay &) = delete;
ImGUIOverlay(ImGUIOverlay &&) noexcept;
ImGUIOverlay &operator=(const ImGUIOverlay &) = delete;
ImGUIOverlay &operator=(ImGUIOverlay &&) noexcept;

or

ImGUIOverlay(const ImGUIOverlay &) = delete;
ImGUIOverlay(ImGUIOverlay &&other) noexcept;
ImGUIOverlay &operator=(const ImGUIOverlay &other) = delete;
ImGUIOverlay &operator=(ImGUIOverlay &&other) noexcept;

IAmNotHanni avatar Sep 05 '20 15:09 IAmNotHanni

Use const for every pointee-type if possible. But for values (including the pointer type), don't use const, at least in the header (declaration), whether you use it in the source file (definition) is up to you.

yeetari avatar Sep 05 '20 15:09 yeetari

Should get methods return a const type? Does it even matter?

IAmNotHanni avatar Sep 08 '20 01:09 IAmNotHanni

Not for values or pointer types. E.g. putting const here:

const Foo *bar();

is ok, but putting const here is useless:

Foo *const bar();
const Foo baz();

yeetari avatar Sep 08 '20 08:09 yeetari

Also const for references?

[[nodiscard]] const std::string &name() const {
    return m_name;
}

IAmNotHanni avatar Sep 09 '20 20:09 IAmNotHanni

I introduced a lot of stuff which is unnecessary, like const VkPhysicalDevice &graphics_card. Vulkan pointers don't need to be const references.

IAmNotHanni avatar Dec 10 '20 23:12 IAmNotHanni

TODO:

  • [ ] Don't use const in header files at all, only in source files.
  • [ ] Don't const Vulkan resource pointers like VkDevice,
  • [ ] Don't use references for Vulkan resource pointers.
  • [ ] Don't use const for get methods unless it's a get method for a const reference.

IAmNotHanni avatar Dec 23 '20 02:12 IAmNotHanni