2

I have a project that requires "creating a constant variable using an unnamed namespace", and I need to share it with functions in another .cpp file. It said the variable declarations could be in their own file. Use of the extern keyword was mentioned, and I figured out how to use extern, having the var in a header file and declared like extern const char varname;, assigned it a value in my main.cpp, (const char varname = A; globally above the main function) and was able to use it in the other .cpp file. But I'm not sure how to make use of an unnamed namespace. In an example file they have the following in the main file:

namespace
{
  extern const double time = 2.0;
}

But there's now example of how to access that in another .cpp file. I tried doing that with my variable and I get an error in the other file where I try to use it saying it's not declared in that scope.

Can someone offer some insight as to what I should be doing here to make use of both of these things?

Rama
  • 3,222
  • 2
  • 11
  • 26
windy401
  • 197
  • 1
  • 8
  • 5
    I do not think you understand your assignment. The point of using unnamed namespace is that functions and variables defined within it are **not** visible from other .cpp files (or, to use proper term, translation units). – SergeyA Jan 23 '17 at 20:49
  • 2
    The assignment as stated seems to be a contradiction in terms. The items in the unnamed namespace cannot be shared by name with any other translation unit. I suppose you could 'share' with another file by `#include "otherfile.cpp"` in the `originalfile.cpp` code, but that really is cheating. – Jonathan Leffler Jan 23 '17 at 20:52
  • Hmm. Well it says to declare the constant using the unnamed namespace, that I have to share it with the other functions, and that it can't be passed by reference. And it specifically suggests using extern. So yea, I'm not sure what it wants. – windy401 Jan 23 '17 at 20:55
  • 1
    `extern` and `unnamed namespace` can't really be used together, since the namespace creates a unique namespace in that translation unit causing the extern variable to be unfindable in another translation unit. You could put a constant in an unnamed namespace in a header file, but then it's technically two different constants with the same value and not a shared constant, from the linker's point of view. – Cameron Jan 23 '17 at 21:45

2 Answers2

1

You can access to use it via an other reference to the variable.

For example:

namespace
{
  const double time = 2.0;
  const double & local_ref_time(time); //Create a local referece to be used in this module
}


extern const double & global_ref_time(local_ref_time); //Create the global reference to be use from any other modules
Rama
  • 3,222
  • 2
  • 11
  • 26
1

You could try writing an accessor function like so:

main.cpp

#include "other.hpp"

namespace
{
    const double time = 2.0;
}

int main()
{
    tellTime();
    return 0;
}

const double getTime()
{
    return time;
}

other.hpp

#ifndef OTHER_HPP_INCLUDED
#define OTHER_HPP_INCLUDED

const double getTime();
void tellTime();

#endif // OTHER_HPP_INCLUDED

other.cpp

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

void tellTime()
{
    std::cout << "Time from the anonymous namespace of main.cpp is " << getTime() << std::endl;
}

I don't think any amount of extern will help: https://stackoverflow.com/a/35290352/1356754

Community
  • 1
  • 1
Brandon
  • 456
  • 2
  • 6
  • 19
  • You do not need to forward declare the `getTime()` function declaration in the `main.cpp` if you include it with the header file. It is legal c++ but it's code duplication which, in general, should be avoided. – clickMe Feb 11 '17 at 13:35