Hi,
look at this function, which does some low-level IO.
void writen(int fd, const void* data, size_t size)
{
while (size) {
int n = write(fd, data, size);
if (n < 0)
throw logic_error...
static_cast<con st char*>(data) += n; // reinterpret_cas t?
size -= n;
}
}
Here it is necessary to do a cast in order to do some pointer arithmetic.
Is the static_cast or the reinterpret_cas t appropriate?
/FAU 2 2111
"Frank A. Uepping" <nu********@spa mgourmet.com> wrote in message news:bp******** *****@news.t-online.com... Here it is necessary to do a cast in order to do some pointer arithmetic. Is the static_cast or the reinterpret_cas t appropriate?
I'd prefer static_cast (in it's undo defined conversions role), but I don't know that
it is more appropriate than reinterpret_cas t in this case.
Frank A. Uepping wrote: Hi, look at this function, which does some low-level IO.
void writen(int fd, const void* data, size_t size) { while (size) { int n = write(fd, data, size); if (n < 0) throw logic_error...
static_cast<con st char*>(data) += n; // reinterpret_cas t?
This will not even compile. The result of this cast in an rvalue.
Operator '+=' cannot be applied to an rvalue. Most likely is was
supposed to be
static_cast<con st char*&>(data) += n
But I don't think this is a valid technique, regardless of the cast
operator used. The more proper way to do this would be the following
data = static_cast<con st char*>(data) + n;
size -= n; } }
Here it is necessary to do a cast in order to do some pointer arithmetic. Is the static_cast or the reinterpret_cas t appropriate? ...
--
Best regards,
Andrey Tarasevich This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Gary Labowitz |
last post by:
Am I doing this correctly? It is a sample program for my class.
#include <iostream>
using namespace std;
int main( )
{
int x=3, y=4;
|
by: A |
last post by:
Hi,
I have not been able to find an example for using the reinterpret cast. Any
ideas?
Regards,
A
|
by: Steven T. Hatton |
last post by:
The code shown below is an example from the Coin3D documentation. I believe
the use of the C-style cast is safe under the circumstances, but from what
I've been exposed to (TC++PL(SE)), I would favor using a static_cast. Is
there any technical reason to favor the C-style over a static_cast?
http://doc.coin3d.org/Coin/index.html
void foo(SoNode * node)
{
if (node->getTypeId() == SoFile::getClassTypeId()) {
|
by: Amit |
last post by:
Greetings.
I am having some problem while using a cast operation(static_cast and/or
dynamic_cast) between base and derived objects when passing to functions.
what I have is something like this..
template <class T>
class Node{
protected:
T e;
Node* left;
|
by: thomas.mertes |
last post by:
For a hash function I want to reinterpret the bits of a float
expression as unsigned long. The normal cast
(unsigned long) float_expression
truncates the float to an (unsigned long) integer.
But this is not the effect I want for a hash function.
With unions my hash function can be implemented:
| |
by: Rahul |
last post by:
Hi,
I have a
class A : public B {...member functions......data members};
and am doing the following
A *p=new A();
void *p=static_cast<void *>(p);
factory_instance->process(p);
|
by: NiveditaB06 |
last post by:
it is recommended not to use reinterpret cast that is mostly used for type converting one pointer type to other totally different type( like int ).After casting one member variable to another type using reinterpret cast you should cast back the member variable to actual type before using.Please anyone explain the reason behind this.If it is for system dependency then explain the dependency in details.
|
by: jason.cipriani |
last post by:
There have been some recent threads about casting pointers to and from
void* that have me rethinking some of my usual practices. I have a
couple of questions.
1. What is the purpose of C++'s static_cast<>? In other words, is
there any real difference between statements like (with non-pointer
types):
double a = 3.4;
int b = (int)a; // <--- this
|
by: Rahul |
last post by:
Hi,
Everywhere I read that static_cast<only work fine for the conversion
which are implicitly allowed by the compiler
hence the following does not work
int *i;
double *d;
d = i; // Compilation error ,
OK
i= static_cast<int *>(d); // Compilation error. OK
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |