c++ - Method operating on container: hardcode the container type, or use generic template iterators? -


I have a code, where is the idea, my input Foo is some container of objects code Filling the containers of "processes" one by one, and the desired result FooProduct results objects.

Through the input container, I only need one pass "processing" is stateful (this is not a std :: transform () ) and the number of results object input object Is independent of the number.

Offhand, I can see two obvious methods of defining the API.

The easiest way to do this is to hardcode a specific type of container. For example, I can determine that I am looking for the vector parameter, for example:

  zero processorfufusion (const std :: vector & Lt; foo & gt; & amp; in; std :: vector & lt; FooProduct & gt; & amp; ou;);  

However, I do not really have any reason to restrict client code for a particular type of container. Instead of interrupting the parameter types specifically for vector , I can simplify the method and use the header as the parameter parameter:

  / ** * @tparam Foo_InputIterator_T An Input Iterator Object Object Type Foo * @tparam FooProduct_OutputIterator_T An Output Iterator Typing Objects * Type Foo Products. * / Template & lt; Typename Foo_InputIterator_T, type name FooProduct_OutputIterator_T & gt; Void ProcessContainerOfFoos (Foo_InputIterator_T before, Foo_InputIterator_T final, FooProduct_OutputIterator_T out);  

I am debating between these two formulations

thoughts

For me, the first code seems to be "easy" and the second " More correct ":

  • Make non-template type signatures clear; I do not have to understand how to use the documentation and what constraints the template parameters are.
  • Without the templates, I can hide the implementation in the .cpp file; With templates I will need to expose the implementation in a header file, the client code must be compelled to include anything for the actual processing argument.
  • The templated edition looks as if it expresses my intention more clearly, because I can instead use the container type.
  • The templated version is more flexible and tested - for example, in my code I can use some custom data structure MySuperEfficientVector , but I still use MyFooProcessor I am able to test without dependence on the custom class

In addition to subjective options based on these ideas, Similarly, Is there a better way to create this API I am missing

except those ideas that you have already listed:

  • The template version allows the client to pass any code, for example, a sub-category or reverse Iterator, not just a complete container From being
  • Allows to pass values ​​other than the template version Foo . To be useful, processing should be normal.
  • If the template only works with specific value types and the user tries to use the wrong tippers, the description of the error message can not be very descriptive of their mistake if it is a concern , You can give the user a better error by using the typed properties: static_assert (std :: is_same The user has no better way of communicating about the requirements of a template type in the signature until the concept of concepts is added to the standard.

There is also the option to provide both functions. Can coded the link to a templated version. This gives you the benefit of both versions at the expense of swelling of your API.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -