做一个模板接受一个特定的类/家庭的类? [英] Make a template accept a specific class/family of classes?

查看:127
本文介绍了做一个模板接受一个特定的类/家庭的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板方法,旨在使用一组特定的类。因为我没有真正需要运行时多态性,我决定不使用指针的父类,而是将编译器把一切为了我。

I have a template method that is designed to work with a specific set of classes. Since I have no real need for runtime polymorphism, I've decided to not use pointers to the parent class, but rather will have the compiler put everything together for me.

我想要限制可传递给模板的类型,或者在传递不正确的类型时使GCC问题产生明显的错误。

I want to either restrict the type that can be passed to the template, or make GCC issue sensible errors when an incorrect type is passed.

这是否可能?

推荐答案

是的。根据当天的具体需要,可以使用许多不同的技术。有boost :: enable_if。有MPL和static_assert ...(如果不使用C ++ 0x编译器,请使用BOOST_STATIC_ASSERT)。有Boost.Concepts。

Yes. It is possible using a number of different techniques depending on the specific needs of the day. There's boost::enable_if. There's MPL and static_assert...(use BOOST_STATIC_ASSERT if you're not using a C++0x compiler). There's Boost.Concepts.

是的。这是可能的。

这篇关于做一个模板接受一个特定的类/家庭的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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