const int FOO vs #define FOO

C

C. J. Clegg

When you say "const int FOO = 0" (as the commonly-recommended C++
alternative to "#define FOO 0"), isn't that declaration globally
visible?

I have "const int FOO = 0;" in one source file and "extern int FOO;"
in another source file (also tried "extern const int FOO;") and at
link time, I get undefined references to FOO.

Removed the "const" from the FOO declaration and everything worked
fine.

Does "const" make it static (file scope)? Certainly seems to...
 
M

Michiel.Salters

C. J. Clegg said:
When you say "const int FOO = 0" (as the commonly-recommended C++
alternative to "#define FOO 0"), isn't that declaration globally
visible?

No. It's visible in the definition scope. I.e. if you define it in the
true-branch of
an if-statement, it's invisible in the else-branch. A #define will be
visible until
the #undef, or EOF.
I have "const int FOO = 0;" in one source file and "extern int FOO;"
in another source file (also tried "extern const int FOO;") and at
link time, I get undefined references to FOO.

Same scope? Besides, you must give them the same type.
Removed the "const" from the FOO declaration and everything worked
fine.

Does "const" make it static (file scope)? Certainly seems to...

IIRC, yes, unless defined extern. But since I only use extern in the
corresponding
header, this is never a problem. Using extern in an unrelated .h or
..cpp is a hack,
IMO.

HTH,
Michiel Salters
 
C

C. J. Clegg

No. It's visible in the definition scope. I.e. if you define it in the
true-branch of
an if-statement, it's invisible in the else-branch. A #define will be
visible until
the #undef, or EOF.

I define it in global space, i.e. outside of any function or class.

In fact, for test, I have a file foo.cpp that has the single line

const int FOO = 0;

.... then I say "extern const int FOO;" at the top of other files, but
the other files can't see it.

So the question is, does it then become visible only to the file,
similar to "static int FOO"?
 
R

Rolf Magnus

C. J. Clegg said:
I define it in global space, i.e. outside of any function or class.

In fact, for test, I have a file foo.cpp that has the single line

const int FOO = 0;

... then I say "extern const int FOO;" at the top of other files, but
the other files can't see it.

Instead, put that in a header file "foo.h" and #include that header in all
of those files as well as foo.cpp.
So the question is, does it then become visible only to the file,
similar to "static int FOO"?

AFAIK, constants on namespace scope have internal linkage by default.
 
A

ALiX

C. J. Clegg said:
When you say "const int FOO = 0" (as the commonly-recommended C++
alternative to "#define FOO 0"), isn't that declaration globally
visible?

I have "const int FOO = 0;" in one source file and "extern int FOO;"
in another source file (also tried "extern const int FOO;") and at
link time, I get undefined references to FOO.

Removed the "const" from the FOO declaration and everything worked
fine.

Does "const" make it static (file scope)? Certainly seems to...

Yes it does. You can declare FOO as having external linkage by
prefixing the definition with 'extern', like this:

extern const int FOO = 0;

/ALiX
 
C

C. J. Clegg

Instead, put that in a header file "foo.h" and #include that header in all
of those files as well as foo.cpp.

Good evening, Rolf.

Ah, but "const int FOO = 0" allocates memory, specifically a block of
size int, and you're not supposed to put stuff in .h files that
allocate memory.

Anyway, if I say "const int FOO = 0" in a .h file and then #include
that .h file in two other files, don't I end up with two FOOs, and in
fact won't I get yelled at by the linker for duplicate symbols?
 
C

C. J. Clegg

Yes it does. You can declare FOO as having external linkage by
prefixing the definition with 'extern', like this:

extern const int FOO = 0;

Thanks, I will try that.
 
R

Ron House

C. J. Clegg said:
Ah, but "const int FOO = 0" allocates memory, specifically a block of
size int, and you're not supposed to put stuff in .h files that
allocate memory.

Anyway, if I say "const int FOO = 0" in a .h file and then #include
that .h file in two other files, don't I end up with two FOOs, and in
fact won't I get yelled at by the linker for duplicate symbols?

You would in C, but not in C++. A const int in C is an int variable that
may not be changed. A const int in C++ is a compile-time name for the
constant number given in the definition, and allocates no storage.
 
J

Jack Klein

When you say "const int FOO = 0" (as the commonly-recommended C++
alternative to "#define FOO 0"), isn't that declaration globally
visible?

I have "const int FOO = 0;" in one source file and "extern int FOO;"
in another source file (also tried "extern const int FOO;") and at
link time, I get undefined references to FOO.

Removed the "const" from the FOO declaration and everything worked
fine.

Does "const" make it static (file scope)? Certainly seems to...

You have just bumped into one of the "silent" changes from C in C++.
"const" on an object defined at file scope in C++ also gives the
object internal linkage, unlike in C.

The solution is to use:

extern const int FOO = 0;

....to restore the external linkage when you want it.
 
J

Jack Klein

You would in C, but not in C++. A const int in C is an int variable that
may not be changed. A const int in C++ is a compile-time name for the
constant number given in the definition, and allocates no storage.

....that is, unless its address is taken, of course.

Given:

extern int func1(const int &);
extern int func2(const int *);

const int FOO = 0;

int main()
{
return func1(FOO) + func2(&FOO);
}

....you can be fairly confident that FOO will be allocated storage,
especially if the called functions are in a separate translation unit.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top