C ++命名空间&模板 [英] C++ Namespaces & templates

查看:160
本文介绍了C ++命名空间&模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些可以组合在一起的函数,但不属于某个对象/实体,因此不能被视为方法。



因此,基本上在这种情况下,我将创建一个新的命名空间并将定义放在头文件 cpp 文件中实现。另外(如果需要)我将在 cpp 文件中创建一个匿名命名空间,并将所有不必暴露/包含的附加函数放到我的命名空间的界面。 / p>

查看下面的代码(可能不是最好的例子,可以做得更好的另一个程序架构,但我只是不能想到一个更好的示例...) / p>

示例代码( header

 命名空间算法{
void HandleCollision(Object * object1,Object * object2);
}

示例代码( cpp

  #includeheader

//匿名命名空间包装
//在'algorithm'方法中使用的例程
//但不必暴露
namespace {
void RefractObject(Object * object1){
//使用该对象做某事
//(...)
}
}

命名空间算法{
void HandleCollision * object1,Object * object2){
if(...)RefractObject(object1);
}
}

>我想这是一个很好的方式来管理我的代码,但我不知道如果我有一些基于模板的功能,并希望做基本相同的应该怎么办。



如果我使用模板,我必须把所有的代码放在头文件中。好吧,但是如何隐藏一些实现细节呢?



我想从我的界面隐藏 RefractObject 函数,但是我不能简单地删除它的声明我有我的所有代码在文件)...



我想出的唯一的办法是例如:



示例代码(

 命名空间算法{
//仍然作为接口的一部分公开!
namespace impl {
template< typename T>
void RefractObject(T * object1){
//使用该对象执行操作
//(...)
}
}

template< typename T,typename Y>
void HandleCollision(T * object1,Y * object2){
impl :: RefractObject(object1);
//另一个东西
}
}

在代码设计方面做得更好?

解决方案

这是一个很常见的解决方案。 Boost做到了,我也做了,但是用 detail 命名空间。只是使它成为一条规则:不要在内部细节



详细说明自己的文件,并将其放在一个详细文件夹中。也就是说,我的代码类似于:

  // v 
#includedetail / RefractObject.hpp

命名空间算法{

template< typename T,typename Y>
void HandleCollision(T * object1,Y * object2){
detail :: RefractObject(object1);
//另一个东西
}
}

良好的代码实践(保持分裂和可重复使用),并保持头文件更清晰的实现细节。


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?

解决方案

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.

这篇关于C ++命名空间&amp;模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆