Restricting templates to only certain classes?

2020-07-10 09:08发布

In Java you can restrict generics so that the parameter type is only a subclass of a particular class. This allows the generics to know the available functions on the type.

I haven't seen this in C++ with templates. So is there a way to restrict the template type and if not, how does the intellisense know which methods are available for <typename T> and whether your passed-in type will work for the templated function?

7条回答
相关推荐>>
2楼-- · 2020-07-10 10:06

An evolution of the actual type system adopted by C++ is on the way and it's named concepts.

The new C++1y concepts will probably provide what you are looking for, and since this feature was already planned for C++11 but didn't make it into the final draft, there is a gcc fork with an implementation of this concepts .

For now "the poor man" solution, if you want to stick with what is given to you by the standard, is to use type traits .

查看更多
登录 后发表回答