When is an empty namespace definition required?

Namespaces are not declared or defined, like most other things, but the namespace equivalent is for a direct declaration:

namespace X {}  // empty body

Usually you define a namespace by placing other declarations in it. But is there a problem for which this “namespace declaration” is the easiest solution? What is the use of an empty namespace?

+3
source share
2 answers

Here is the one that even appears in the standard: The declaration of the use directive to denote a namespace

namespace unique { }
using namespace unique;

, using .

+4

, "" . , , , , (, , , ).

:

namespace container_inserters {}

template<class Stream, class Iter, class Ch>
void write_sequence(Stream& s, Iter begin, Iter end,
                    Ch const *initial, Ch const *sep, Ch const *final)
{
  using namespace container_inserters;
  if (initial) s << initial;
  if (begin != end) {
    s << *begin;
    ++begin;
    for (; begin != end; ++begin) {
      if (sep) s << sep;
      s << *begin;
    }
  }
  if (final) s << final;
}

namespace container_inserters {
#define G(N) \
template<class Ch, class Tr, class T, class A> \
std::basic_ostream<Ch,Tr>& operator<<(std::basic_ostream<Ch,Tr> &s, \
                                      N<T,A> const &value) \
{ \
  write_sequence(s, value.begin(), value.end(), "[", ", ", "]"); \
  return s; \
}
G(std::deque)
G(std::list)
G(std::vector)
#undef G
}  // container_inserters::

s << *begin , write_sequence ( ), use. :

int main() {
  using namespace std;
  vector<deque<list<int> > > v (3, deque<list<int> >(2, list<int>(1, 42)));

  using namespace container_inserters;
  cout << v << '\n';

  return 0;
}
// output:
//  [[[42], [42]], [[42], [42]], [[42], [42]]]

Boost , , .

+2

Source: https://habr.com/ru/post/1766852/


All Articles