Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 772983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:53:28+00:00 2026-05-14T18:53:28+00:00

I have some functions that can be grouped together, but don’t belong to some

  • 0

I have some functions that can be grouped together, but don’t belong to some object / entity and therefore can’t be treated as methods.

So, basically in this situation I would create a new namespace and put the definitions in a header file, the implementation in cpp file. Also (if needed) I would create an anonymous namespace in that cpp file and put all additional functions that don’t have to be exposed / included to my namespace’s interface there.

See the code below (probably not the best example and could be done better with another program architecture, but I just can’t think of a better sample…)

Sample code (header)

namespace algorithm {
   void HandleCollision(Object* object1, Object* object2);
}

Sample code (cpp)

#include "header"

// Anonymous namespace that wraps 
// routines that are used inside 'algorithm' methods
// but don't have to be exposed
namespace {
   void RefractObject(Object* object1) {
      // Do something with that object
      // (...)
   }
}

namespace algorithm {
   void HandleCollision(Object* object1, Object* object2) {
      if (...) RefractObject(object1);
   }
}

So far so good. I guess this is a good way to manage my code, but I don’t know what should I do if I have some template-based functions and want to do basically the same.

If I’m using templates, I have to put all my code in the header file. Ok, but how should I conceal some implementation details then?

I want to hide RefractObject function from my interface, but I can’t simply remove its declaration (just because I have all my code in a header file)…

The only approach I came up with was something like:

Sample code (header)

namespace algorithm {
   // Is still exposed as a part of interface!
   namespace impl {
      template <typename T>
      void RefractObject(T* object1) {
         // Do something with that object
         // (...)
      }
   }

   template <typename T, typename Y>
   void HandleCollision(T* object1, Y* object2) {
      impl::RefractObject(object1);
      // Another stuff
   }
}

Any ideas how to make this better in terms of code designing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:53:29+00:00Added an answer on May 14, 2026 at 6:53 pm

    That’s a pretty common solution. Boost does it, and I do it as well, but with the detail namespace instead. Just make it a rule: “don’t look inside detail!”

    File-wise, I recommend giving details their own file, and tucking it away in a detail folder. That is, my code would be akin to:

    //           v
    #include "detail/RefractObject.hpp"
    
    namespace algorithm {
    
       template <typename T, typename Y>
       void HandleCollision(T* object1, Y* object2) {
          detail::RefractObject(object1);
          // Another stuff
       }
    }
    

    This is just good code practice in general (keep things split up and re-usable) and keeps the header file cleaner of implementation details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some debugging functions that I would like to refactor, but seeing as
I have some Matlab functions that I have to translate in C but I
I my framework I have some functions that when done they can add some
I have some functions in my code that accept either an object or an
I have some functions that use opengl to draw lines on the screen (health
I have some code in a couple of different functions that looks something like
I have a Base Router where i define some functions that need to be
I've been writing some jQuery functions that have JavaScript variables and looping, etc inside
I have some setup code in my functions.php file that sets permalinks and adds
I have a c# project that calls some functions from a c dll, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.