What are some good practices for managing free functions in a C++ project? -
i'm writing code implement sift feature detector , there free functions used throughout project e.g.
int ialignup( int a, int b ){return (a%b != 0) ? (a + b - a%b) : (a);} int idivup( int a, int b ){return (a%b != 0) ? (a/b + 1) : (a/b);}
should encapsulate these functions in unnamed namespace? wish manage code can later scale without problems.
you have 2 options depending on whether used in single file or in multiple files.
put them in unnamed namespace.
if functions used within single source code file, can put them in unnamed namespace within file.
namespace { int ialignup( int a, int b ){return (a%b != 0) ? (a + b - a%b) : a;} int idivup( int a, int b ){return (a%b != 0) ? (a/b + 1) : (a/b);} }
put them in header file , internal namespace.
if functions used in multiple source code files, not outside project, can put them in header file included within project, , put them in internal namespace.
namespace project { namespace impl { inline int ialignup( int a, int b ){return (a%b != 0) ? (a + b - a%b) : a;} inline int idivup( int a, int b ){return (a%b != 0) ? (a/b + 1) : (a/b);} } }
in example, project::impl
namespace not visible outside project. not mentioned in externally accessible header files.
Comments
Post a Comment