带前向声明的默认模板参数 [英] Default template parameters with forward declaration

查看:223
本文介绍了带前向声明的默认模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以转发声明一个使用默认参数但不指定或不知道这些参数的类吗?



例如,我要声明一个 boost :: ptr_list< TYPE> ,而无需将整个Boost库拖放到包含traits的每个文件中。我想声明
命名空间boost {template< class T> class ptr_list< T。 } ,但这不工作,因为它不完全匹配真正的类声明:

  template< class T,
class CloneAllocator = heap_clone_allocator,
class Allocator = std :: allocator< void *>
>
class ptr_list {...};是我的选项只与它一起生活还是指定 boost :: ptr_list< ; TYPE,boost :: heap_clone_allocator,std :: allocator< void *> 在我的traits类? (如果我使用后者,我还要转发declare boost :: heap_clone_allocator 并包括< memory> ,我想。)



我浏览了Stroustrup的书,SO和互联网的其余部分,还没有找到解决方案。通常人们关心的不包括STL,解决方案是只包括STL头。但是,Boost是一个更大规模的编译器密集型库,所以我宁愿放弃,除非我绝对必须。

解决方案

任何使用你的工具的编译单元,只要你有某些程序不会真正使用你的设施的boost部分,那么就需要包含boost头文件。



这是真的,通过转发声明,你可以避免包括boost头文件这样的程序。但是你必须为实际使用boost部分的程序手动包含boost标头(或者有一个 #ifdef )。



请记住,在未来的Boost版本中可能会添加更多默认模板参数。我建议反对这条路线。如果你的目标是加快编译速度,我会考虑使用 #define 来指示是否应该禁用使用该boost库的代码。这样你避免了前面的声明麻烦。


Is it possible to forward declare a class that uses default arguments without specifying or knowing those arguments?

For example, I would like to declare a boost::ptr_list< TYPE > in a Traits class without dragging the entire Boost library into every file that includes the traits. I would like to declare namespace boost { template<class T> class ptr_list< T >; }, but that doesn't work because it doesn't exactly match the true class declaration:

template < class T,
    class CloneAllocator = heap_clone_allocator,
    class Allocator = std::allocator<void*>
    >
class ptr_list { ... };

Are my options only to live with it or to specify boost::ptr_list< TYPE, boost::heap_clone_allocator, std::allocator<void*> in my traits class? (If I use the latter, I'll also have to forward declare boost::heap_clone_allocator and include <memory>, I suppose.)

I've looked through Stroustrup's book, SO, and the rest of the internet and haven't found a solution. Usually people are concerned about not including STL, and the solution is "just include the STL headers." However, Boost is a much more massive and compiler-intensive library, so I'd prefer to leave it out unless I absolutely have to.

解决方案

Any compilation unit that uses your facility that forward-declares boost stuff will need to include the boost headers anyway, except in the case that you have certain programs that won't actually use the boost part of your facility.

It's true that by forward-declaring, you can avoid including the boost headers for such programs. But you'll have to manually include the boost headers (or have an #ifdef) for those programs that actually use the boost part.

Keep in mind that more default template parameters could be added in a future Boost release. I'd advise against this route. What I would consider, if your goal is to speed compile times, is to use a #define to indicate whether the code using that boost library should be disabled. This way you avoid the forward declaration hassle.

这篇关于带前向声明的默认模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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