Jump to content
  • Advertisement
Sign in to follow this  
Lode

interface with std::vectors

This topic is 4482 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm wondering, are std::vectors acceptable as interface for a library or something similar? Because, in the case of the png decoder I made, you have to give as parameters: an std::vector<unsigned char> containing the contents of the file, an empty std::vector<unsigned char>& that will be filled with the pixels of the image, and an empty std::vector<int>& that will be filled with information like the width and height of the image. So basically, when using this little library you have to make std::vectors and pass them to the function of the library. I don't think I've ever seen a library or API that used std::vectors, because most of them are for C too instead of only C++. My thing is only for C++ though, I find std::vectors much more conveniant, you don't have to think about memory allocation, they're standard C++, and don't decrease performance if you compile with optimization. So do you think it's annoying, or bad, to make it this way? Would it be better if you instead have to give normal C-style buffers, or those C++ things that will be allocated with "new[]" and "delete[]" as parameters?

Share this post


Link to post
Share on other sites
Advertisement
When you use a templated class, such as std::vector, in a library distributed as object code, it will only be compatible with code compiled with the same compiler, the same compile settings and same template implementation. This is generally a bad thing for distributing a library. However, if you're library is a source code library, then it's less of a problem.

Share this post


Link to post
Share on other sites
It's a source code library.

But wow that's bad about those templates... And they call that standard C++!! :(

Share this post


Link to post
Share on other sites
Quote:
Original post by Lode
But wow that's bad about those templates... And they call that standard C++!! :(

But the C++ code is standard. The binary files generated by the compiler, however, is not.

Share this post


Link to post
Share on other sites
Quote:
Original post by Lode
My thing is only for C++ though, I find std::vectors much more conveniant, you don't have to think about memory allocation, they're standard C++, and don't decrease performance if you compile with optimization.


The interface is standard. The implementation is not. If I pass a vector that has an incompatible implementation to your library, Bad Things will happen. If you want to use a vector as part of the library's interface, you will have to use a C++ header file with an inline interface that acts as a front-end to a C interface.

foo.h
int foo_impl(int* buf, int size); // in the library

inline int foo(std::vector<int>& vec)
{
return foo_impl(&vec.front(), vec.size());
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!