isocpp / isocpp/CppCoreGuidelines
Feature request: more detailed explnanation on preffared way declaring global constants
@BjarneStroustrup is already working on this.
Since Nov 19, 2020.
- Dominant language
- CSS
- Stars
- 45.3k
- Forks
- 5.6k
- PR merge metrics
- No merged PRs in 30d
Description
Consider we have Consts.h, where we want to declare some defaults used across application. (very)Old way where macros, now we have constexpr. BUT, there's multiple variations of that, and I personally feel confused on what to use.
For example, https://www.learncpp.com/cpp-tutorial/const-constexpr-and-symbolic-constants/ suggests to use inline constexpr since C++17 (without arguments on why), and you can find StackOverflow discussions on how static constexpr variables will be initialized once per application (which might be "better"?), if I understood correctly.
So, current possibilities:
Consts.h
#include <chrono>
namespace consts {
constexpr auto DefaultTimeout1 {std::chrono::seconds{1}};
static constexpr auto DefaultTimeout2 {std::chrono::seconds{1}};
inline constexpr auto DefaultTimeout3 {std::chrono::seconds{1}}; // since C++17?
};
So.. when do we use which?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.