This scope issue seems like a type of C ++ fix that Scott Meyers would consider in one of his effective C ++ books.
I have a function Analyzethat does some analysis over a range of data. The function is called from several places with different types of iterators, so I made it a template (and thus implemented it in the header file). The function depends on a static data table AnalysisTable, which I do not want to show the rest of the code.
My first approach was to make table a static constinside Analysis.
namespace MyNamespace {
template <typename InputIterator>
int Analyze(InputIterator begin, InputIterator end) {
static const int AnalysisTable[] = { };
...
return result;
}
}
, AnalysisTable Analyze, (, , ).
, :
namespace MyNamespace {
const int AnalysisTable[] = { };
template <typename InputIterator>
int Analyze(InputIterator begin, InputIterator end) {
...
return result;
}
}
, . , :
namespace MyNamespace {
namespace {
const int AnalysisTable[] = { };
}
template <typename InputIterator>
int Analyze(InputIterator begin, InputIterator end) {
...
return result;
}
}
, , , . Analyze , . , , , .
, extern Analyze.
namespace MyNamespace {
template <typename InputIterator>
int Analyze(InputIterator begin, InputIterator end) {
extern const int AnalysisTable[];
...
return result;
}
}
#include "foo.h"
namespace MyNamespace {
const int AnalysisTable[] = { };
}
, , , , . , , " AnalysisTable". ! ( - , ?)
, , , :
namespace MyNamespace {
namespace PrivateStuff {
extern const int AnalysisTable[];
}
template <typename InputIterator>
int Analyze(InputIterator begin, InputIterator end) {
...
return result;
}
}
#include "foo.h"
namespace MyNamespace {
namespace PrivateStuff {
const int AnalysisTable[] = { };
}
}
AnalysisTable (yay!), (boo!). , , .
, Analyze?