5 ms·
Mostly agree, but: > Don't use C API. The STL should provide you with enough API to use the proper C++ types, either std::string or std::string_view in C++17 i
by saagarjha 9y ago
Mostly agree, but:
> Don't use C API. The STL should provide you with enough API to use the proper C++ types, either std::string or std::string_view in C++17 if possible.
Sometimes you're working with C API that gives you back a char * that they've already allocated. AFAIK there isn't a way to create an std::string out of that without a copy.
> you can't static_cast<> a pointer
You can static_cast a void * into other kinds of pointers.
> use std::array
Do you mean std::vector and at()?
- fooker 9y ago>AFAIK there isn't a way to create an std::string out of that without a copy. You can use std::string_view, Google's StringPiece or llvm::StringRef.
- scott_s 9y ago> Do you mean std::vector and at()? No, std::array was added in C++11: http://en.cppreference.com/w/cpp/container/array http://en.cppreference.com/w/cpp/container/array
- nobugs 9y ago> You can static_cast a void * into other kinds of pointers. Moreover, you can use static_cast for downcasts (from the parent class to child class) - without runtime costs of dynamic_cast. static_cast is not safe (it doesn't perform runtime checks), but reinterpret_cast is even worse (it doesn't perform even compile-time checks).