I can use using namespace
directive to avoid identifier/variable name collision, but what happens when file names or library names collision happens in large projects.
In C
the conventional approach is to add files recursively using #include_next
directive. How can I achieve the same in C++
without using the #include_next
directive and address the issue of duplicate file names among applications and shared libraries. An example, a work around the class() function in AIX math.h clashing with identifiers named "class".
/* GNU Lesser GPLv2.1 or later */
#ifndef FFMPEG_COMPAT_AIX_MATH_H
#define FFMPEG_COMPAT_AIX_MATH_H
#define class some_text
#include_next <math.h>
#undef class
#endif /* FFMPEG_COMPAT_AIX_MATH_H */
EDIT:
Can I use, for example class machine-instruction-set
where the binary has to run on multiple platforms? Can there be namespace collision in such case?
How to avoid namespace collision in C and C++ I can use using namespace directive to avoid identifier/variable name collision, but what happens when file names or library names collision happens in large projects. In C the conventional approach is to add files recursively using #include_next directive.
Namespaces provide the space where we can define or declare identifiers i.e. names of variables, methods, classes, etc. Namespace is a feature added in C++ and is not present in C.
If two identical identifiers are introduced into the same program in a way that the compiler or linker can't tell them apart, the compiler or linker will produce an error. This error is generally referred to as a naming collision (or naming conflict).
A naming collision is a circumstance where two or more identifiers in a given namespace or a given scope cannot be unambiguously resolved, and such unambiguous resolution is a requirement of the underlying system.
I can use using namespace directive to avoid identifier/variable name collision
Quite to the contrary, using namespace
directive introduces collisions. You resolve collisions by specifying scopes, e.g. std::vector<>
vs. boost::numeric::ublas::vector<>
.
... but what happens when file names or library names collision happens in large projects?
Filename collisions are easy to prevent by being systematic: organize your headers so that they mimic your namespaces, e.g. boost::numeric::ublas::vector<>
comes from #include <boost/numeric/ublas/vector.hpp>
. And don't pile headers and sources of different libraries in one directory, so that you can include headers with the same name using a different directory prefix, e.g. #include <lzma/version.h>
vs. #include <linux/version.h>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With