简体   繁体   中英

C++: Using external linkage with an unnamed namespace to use a constant variable

My goal is that when I call myFunction from within main, I do not have to pass the SIZE constant. The function within myFunction.cpp should then run and simply output the contents of the array.

I've read some notes on external linkage but I feel like I don't understand it well enough to apply anything in this case. Ideally main.cpp shouldn't be changed much other than giving the namespace including SIZE a name if absolutely necessary.

main.cpp

#include <iostream>
#include "myFunction.hpp"

namespace
{
    extern const int SIZE = 10;
}

void myFunction(char []);

int main()
{
    char myArray[SIZE] = "123456789";

    myFunction(myArray);
}

myFunction.h

#ifndef MYFUNCTION_H_INCLUDED
#define MYFUNCTION_H_INCLUDED

namespace
{
    extern const int SIZE;
}

#endif // MYFUNCTION_H_INCLUDED

myFunction.cpp

#include <iostream>

void myFunction(char myArray[])
{
    for(int i = 0; i < SIZE; ++i)
    {
        std::cout << myArray[i] << " ";
    }
}

With what I have so far I still get the error that SIZE was not declared in that scope (myFunction.cpp).

Question: What do I have to do to make this example work? If possible, I'd also love an explanantion of why one would handle sharing SIZE this way.

Am unnamed namespace tells the compiler that nothing inside is to be shared with another translation unit. (That is, everything inside has internal linkage .) As long as SIZE is in an unnamed namespace , it cannot have external linkage , so it cannot be seen by myFunction() .

If possible, I'd also love an explanantion of why one would handle sharing SIZE this way.

Since you are working with a toy exercise, my guess is that one would handle sharing SIZE this way to better understand internal and external linkage. It's an exercise, not a real-world example.

For a more realistic example, maybe a version string would be both simple and good? Your app might have a version (eg "1.69.8109") that it displays at various points. This version could be declared as a const string that is used by several source files. It would go into a (named) namespace to avoid name conflicts. It could be defined in a source file rather than a header file to limit the amount of re-compilation needed when the version changes. (Only the source with the definition would need to be recompiled, not every source that uses the version string.) This might be a reasonable benefit, especially if the version number changes reasonably often, not necessarily just when a new release is being prepared.

(From the comments): "Try to get this to work. You may give the unnamed namespace holding SIZE a name if that helps"

It's funny how often instructions for an exercise will tell you what to do. (Heh, "if".)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM