As a follow up to my previous question (Variable Length Array Performance Implications (C/C++)), I am having a bit of trouble maintaining const correctness using the C system call writev(). Namely, it appears as though I have run into the exact same problem as this user did in C, although I am using C++:
Here's a snippet of my code:
int my_awesome_transmit_function(const uint8_t* const buffer, const size_t length) {
// ... some stuff happens...
struct iovec iov[2];
iov[1].iov_base = buffer; // compiler yells about this
iov[1].iov_len = length;
// ... some more code you don't care about
writev(my_fd, iov, 2);
}
Given the solution presented from the CodeReview post, I have implemented the following change to the line that's giving me issues since I'd like to avoid the C-style cast:
iov[1].iov_base = const_cast<uint8_t*>(buffer);
Is this a valid way of using const cast? As far as I can tell, writev guarentees that the iov structure will not be modified (http://linux.die.net/man/2/writev). This blog post (http://blog.aaronballman.com/2011/06/when-should-you-use-const_cast/) leads me to believe that this is a valid usage (since I am never modifying the buffer), but I'd like to be certain since generally wherever I see const_cast or reinterpret_cast, I get a bit leery.
Thanks in advance for the help.
Yes, your use of const_cast
is fine.