Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 361
manav m-n Avatar asked Sep 28 '13 13:09

manav m-n


People also ask

How does the collision be avoided in C++?

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.

Does C support namespace?

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.

What is name collision in global scope?

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).

What is name space collision in Java?

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.


1 Answers

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>.

like image 90
Maxim Egorushkin Avatar answered Sep 20 '22 05:09

Maxim Egorushkin