initialization of const variable in header vs source file

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

I read that we can define a const variable in a header file, only if
the variable is initialized by a constant expression that can be
evaluated at compile-time. For example, we can have the following
header file:

File Sample.h:
---------------------
#ifndef _SAMPLE_H
#define _SAMPLE_H
const double d = 1.1;
#endif

But we can also initialize a const variable in a source file only if
it is qualified by extern specifier, in order to be used in other
source files(I understand the reason for specifying extern for const
variable initializations in source files). For example, consider the
program consisting of var_declr.h, x.cpp and y.cpp:

File var_declr.h:
-----------------------
#ifndef _VAR_DECLR_H
#define _VAR_DECLR_H
extern const double d;
extern void fn(void);
#endif

File x.cpp:
---------------
#include <cstdlib>
#include <iostream>
#include "var_declr.h"

using namespace std;

extern const double d = 1.1;

int main()
{
cout << "from main: " << d << endl;

fn();

return EXIT_SUCCESS;
}

File y.cpp:
---------------
#include <iostream>
#include "var_declr.h"

using namespace std;

void fn(void)
{
cout << "from fn(): " << d << endl;

return;
}

Thus we can define a const variable intialized by a constant
expression that can be evaluated at compile-time, in both header file
as well as source file. If we want to make any change to the
initializing expression, we can do so both in header and source files.
There is no difference in making changes to header or source files.

My question: What are the advantages of initializing a const variable
in a header file over initializing it in a source file ?

Kindly explain.

Thanks
V.Subramanian
 
A

Alf P. Steinbach

* (e-mail address removed), India:
I read that we can define a const variable in a header file, only if
the variable is initialized by a constant expression that can be
evaluated at compile-time.

No. The language features do not depend on what kinds of files the source code
is in. The difference between header files and what you call "source" files is
simply that that the source code in a header file can be included in more than
one compilation unit, thus, you need to be careful not to violate the One
Definition Rule.

For example, we can have the following
header file:

File Sample.h:

Identifiers starting with underscore followed by uppercase letter, such as
_SAMPLE_H, are reserved for the implementation.

const double d = 1.1;
#endif

But we can also initialize a const variable in a source file only if
it is qualified by extern specifier, in order to be used in other
source files(I understand the reason for specifying extern for const
variable initializations in source files).


No. You do not need 'extern' to define a 'const'. But you do need 'extern' to
define a constant with extern linkage, one that is defined in one compilation
unit and only referred to by other compilation units.

For example, consider the
program consisting of var_declr.h, x.cpp and y.cpp:

File var_declr.h:
-----------------------
#ifndef _VAR_DECLR_H
#define _VAR_DECLR_H
extern const double d;
extern void fn(void);
#endif

File x.cpp:
---------------
#include <cstdlib>
#include <iostream>
#include "var_declr.h"

using namespace std;

extern const double d = 1.1;

int main()
{
cout << "from main: " << d << endl;

fn();

return EXIT_SUCCESS;
}

File y.cpp:
---------------
#include <iostream>
#include "var_declr.h"

using namespace std;

void fn(void)
{
cout << "from fn(): " << d << endl;

return;
}

Thus we can define a const variable intialized by a constant
expression that can be evaluated at compile-time, in both header file
as well as source file. If we want to make any change to the
initializing expression, we can do so both in header and source files.
There is no difference in making changes to header or source files.

It's unclear what you mean here, but literally that's correct: the language
features don't depend on the kinds of files.

My question: What are the advantages of initializing a const variable
in a header file over initializing it in a source file ?

A constant that's defined in a header file can be known at compile time in every
compilation unit where the header file is included.

Also, in some cases you do not want to require separate compilation of anything,
in which case constants have to be defined in header files.


Cheers & hth.,

- Alf
 
S

subramanian100in

* "Alf P. Steinbach said:
Also, in some cases you do not want to require separate compilation of anything,
in which case constants have to be defined in header files.

Cheers & hth.,

- Alf

For my understanding purpose, kindly give an example regarding when we
do not want to require separate compilation of anything. Also, if
possible, please give a program sample for the same.

Thanks
V.Subramanian
 
A

Alf P. Steinbach

* (e-mail address removed), India:
For my understanding purpose, kindly give an example regarding when we
do not want to require separate compilation of anything. Also, if
possible, please give a program sample for the same.

E.g., the Boost library consists of a great many smaller individual libraries,
and many of these are designed as header only libraries so that you can use them
without building anything and maintaining a host of different binary versions:
all you need is to have the header files available.

Generally the cost is some added dependencies.

For example, you can use boost::shared_ptr without any separate compilation of
the smart pointers library (while, on the other hand, the Boost regex stuff
requires separate compilation, as I recall).


Cheers & hth.,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top