Saturday, July 24, 2021

extern vs constexpr vs const keyword

extern keyword

  1. Improve compile times with forward declarations
  2. Header files often #include lots of other headers because other classes are being referenced. These chained includes force the compiler to repeatedly load and precompile long lists of headers over and over again. Thanks to #pragma directives or #ifdef guards this is a rather cheap operation,...

  3. Definitions and ODR (One Definition Rule)
  4. Definitions are declarations that fully define the entity introduced by the declaration. Every declaration is a definition, except for the following:.

  5. Declarations
  6. Declarations introduce (or re-introduce) names into the C++ program. Each kind of entity is declared differently. Definitions are declarations that are sufficient to use the entity identified by the name.

    extern

  7. Understanding “extern” keyword in C
  8. So let me start by saying that the extern keyword applies to C variables (data objects) and C functions. Basically, the extern keyword extends the visibility of the C variables and C functions. That’s probably the reason why it was named extern.

    Though most people probably understand the difference between the “declaration” and the “definition” of a variable or function, for the sake of completeness, I would like to clarify them.

    • Declaration of a variable or function simply declares that the variable or function exists somewhere in the program, but the memory is not allocated for them. The declaration of a variable or function serves an important role–it tells the program what its type is going to be. In case of function declarations, it also tells the program the arguments, their data types, the order of those arguments, and the return type of the function. So that’s all about the declaration.
    • Coming to the definition, when we define a variable or function, in addition to everything that a declaration does, it also allocates memory for that variable or function. Therefore, we can think of definition as a superset of the declaration (or declaration as a subset of definition).

    if a variable is only declared and an initializer is also provided with that declaration, then the memory for that variable will be allocated–in other words, that variable will be considered as defined. Therefore, as per the C standard, this program will compile successfully and work.

    So that was a preliminary look at the extern keyword in C. In short, we can say:

    1. A declaration can be done any number of times but definition only once.
    2. the extern keyword is used to extend the visibility of variables/functions.
    3. Since functions are visible throughout the program by default, the use of extern is not needed in function declarations or definitions. Its use is implicit.
    4. When extern is used with a variable, it’s only declared, not defined.
    5. As an exception, when an extern variable is declared with initialization, it is taken as the definition of the variable as well.
  9. doubt in usage of extern keyword in c...any one help
  10. doubt in usage of extern keyword in c...any one help.

  11. Internal Linkage and External Linkage in C
  12. It is often quite hard to distinguish between scope and linkage, and the roles they play. This article focuses on scope and linkage, and how they are used in C language. .

  13. Redeclaration of global variable in C
  14. This is a paragraph.

  15. Different ways to declare variable as constant in C and C++
  16. There are many different ways to make the variable as constant.

  17. Internal static variable vs. External static variable with Examples in C
  18. The static variable may be internal or external depending on the place of declaration. Static variables are stored in initialised data segments..

  19. Internal static variable vs. External static variable with Examples in C
  20. The static variable may be internal or external depending on the place of declaration. Static variables are stored in initialised data segments.

    Internal Static Variables: Internal Static variables are defined as those having static variables which are declared inside a function and extends up to the end of the particular function..

  21. Use of explicit keyword in C++
  22. This is a paragraph.

  23. Understanding nullptr in C++
  24. This is a paragraph.

  25. Understanding constexpr specifier in C++
  26. constexpr is a feature added in C++ 11. The main idea is performance improvement of programs by doing computations at compile time rather than run time. Note that once a program is compiled and finalized by developer, it is run multiple times by users. The idea is to spend time in compilation and save time at run time (similar to template metaprogramming)

    constexpr specifies that the value of an object or a function can be evaluated at compile time and the expression can be used in other constant expressions. For example, in below code product() is evaluated at compile time.

    const s constexpr

  27. Constants and Constant Expressions in C++11
  28. In this article the issues concerning constant definitions (const) and constant expressions (contexpr) are discussed. The examples, concerning constants, run in GCC 4.7.0, Visual Studio 2008, Visual Studio 2010 and Visual Studio 11. The examples dealing with constexpr run in GCC 4.7.0, but not in Visual Studio (they are not implemented in Visual C++ 11 yet)..

  29. The One Definition Rule in C++11 and C++14: Constant Expressions and Inline Functions
  30. In C++, each name (of a template, type, function, or object) is subjected to the one definition rule (ODR): it can be defined only once. This article covers some issues with ODR, provides some recommendations and solutions to the problems that may occur, particularly when using constants and constant expressions in inline functions. The first five examples can be compiled using the following compilers: GCC C++ 4.8.1, the Microsoft Visual C++ Compiler Nov 2013 CTP, GCC C++ 4.9 and Clang 3.4. The last example can be compiled only in GCC C++ 4.9 and Clang 3.4..

  31. Constexpr constructors (C++11)
  32. A constructor that is declared with a constexpr specifier is a constexpr constructor. Previously, only expressions of built-in types could be valid constant expressions. With constexpr constructors, objects of user-defined types can be included in valid constant expressions.

  33. Application of C++11 User-Defined Literals to Handling Scientific Quantities, Number Representation and String Manipulation
  34. In this article, user-defined literals are explained and examples of their application are given. The examples with user-defined literals can be compiled only in GCC, version 4.7.0 and above..

  35. constexpr CRC32 using user-defined literal
  36. This tip is about compile time CRC32 calculation. Sometimes we need a to use a CRC for checking a static password or anything else, and write: .

  37. Very secure method to save and restore registry
  38. This article gives a very secure method to save and restore registry keys. It provides a ready to use tool in both command-line and UI modes..

  39. MSI Packages Manager
  40. This article shows how to process one or many MSI packages just by providing a configuration file. It gives also many useful hints and tricks that can be used in other projects..

  41. How to Save and Restore Registry Keys
  42. This article shows how to save and restore registry keys and provides a command-line tool demonstrating how to do it..

  43. How to display Windows Explorer objects in one command-line
  44. This article shows how to display Windows desktop objects like Control Panel, Administration Tools, Scanners and Cameras etc., in one command-line, and provides a complete application for illustration..

  45. The Most Essential C++ Advice
  46. Rather than a long and complete guide with comprehensive explanations, this is a short list of things to watch out for when using C++, kept up to date based on the things I see in code reviews..

No comments:

Post a Comment