Hi,
I'm designing an interface for a shared library, and I would like to
know if there is a standard about how to return an object to the user. I
will use exceptions to report errors, so there are at least four ways to
do it.
For example if we have a method named M, that receives a reference to
parameter p and returns object o.
1 - virtual o& M(p& parameter) throw(Exception 1, Exception2...
2 - virtual o* M(p& parameter) throw(Exception 1, Exception2...
3 - virtual void M(p& parameter, o& returnVal) throw(Exception 1,
Exception2...
4 - virtual void M(p& parameter, o* returnVal) throw(Exception 1,
Exception2...
Probably there is no convention about it and is only a mater of taste,
but I would like to know some opinions.
Best regards,
Gama Franco
Jul 22 '05
14 2049
Daniel T. wrote: [...] Try returning the object by const value first, [...]
Top-level const qualifiers are meaningless in most situations. And
I mean
const int foo();
void foo(const int);
V
Niels Dybdahl wrote: Use
virtual o M(const p& parameter) const;
instead. Pass by *const* reference and return by value.
I forgot to mention something very important. One of the requisites is efficiency. I mean, the API should run as fast as possible (it deals with scientific data aquisition). As mentioned virtual o M(...) is the most elegant solution. Often the implemention is that the caller allocates the memory for o and M initializes it with its return statement, which often defaults to a simple copy operation.
No.
A good optimizing C++ compiler should implement
the Named Return Value Optimization (NRVO).
When a function returns the value of a local variable
the optimizing compiler interprets the local variable
as a reference to the return value and initializes it directly --
*no* copy is required!
Return by value costs no more than any of the other options
that Gama Franco listed.
If your compiler does not implement the NRVO,
you must not be concerned about efficiency or performance
or you should be shopping for a better optimizing C++ compiler.
If you want to get rid of the construction, copying and destruction of the object for each call, you [must] use method 3 or 4. They are equal in terms of efficiency. Whether you use pointer or reference does not matter in terms of effiency. I prefer using references to emphasize, that it is not an array.
This probably isn't a good idea.
In-place operations (functions that modify objects)
should be of the form:
o& o::M(const p& parameter);
and return a reference to the object.
E. Robert Tisdale wrote: Niels Dybdahl wrote:
Use
virtual o M(const p& parameter) const;
instead. Pass by *const* reference and return by value. I forgot to mention something very important. One of the requisites is efficiency. I mean, the API should run as fast as possible (it deals with scientific data aquisition).
As mentioned virtual o M(...) is the most elegant solution. Often the implemention is that the caller allocates the memory for o and M initializes it with its return statement, which often defaults to a simple copy operation.
No. A good optimizing C++ compiler should implement the Named Return Value Optimization (NRVO). When a function returns the value of a local variable the optimizing compiler interprets the local variable as a reference to the return value and initializes it directly -- *no* copy is required! Return by value costs no more than any of the other options that Gama Franco listed.
If your compiler does not implement the NRVO, you must not be concerned about efficiency or performance or you should be shopping for a better optimizing C++ compiler.
If you want to get rid of the construction, copying and destruction of the object for each call, you [must] use method 3 or 4. They are equal in terms of efficiency. Whether you use pointer or reference does not matter in terms of effiency. I prefer using references to emphasize, that it is not an array.
This probably isn't a good idea.
In-place operations (functions that modify objects) should be of the form:
o& o::M(const p& parameter);
and return a reference to the object.
Hi,
I do I know if my compiler does that kind of optimization?
The code will compile in a great number of versions of g++, and probably
also in the default compiler of Solaris.
The program will be used for data aquisition, dealing with data in the
order of hundreds of megabytes per second. Since we are dealing with the
refractoring of an existing API, I really would like to make a
consistent choice without affecting the efficiency of the existing code.
If most of the compilers use that kind of optimization, probably return
by value may be the best choice, since the object will get destroyed
when the variable gets out of scope.
Best regards,
Gama Franco
Gama Franco wrote: I do I know if my compiler does that kind of optimization? The code will compile in a great number of versions of g++,
My compiler:
g++ --version
g++ (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
does it.
and probably also in the default compiler of Solaris.
Later versions of the Solaris C++ compiler do it too.
The program will be used for data aquisition, dealing with data in the order of hundreds of megabytes per second. Since we are dealing with the refractoring of an existing API, I really would like to make a consistent choice without affecting the efficiency of the existing code. If most of the compilers use that kind of optimization, probably return by value may be the best choice,
It *is* the best choice.
Most modern C++ compilers support the NRVO
but what matters is that there is at least one
good optimizing C++ compiler for each target platform.
You can do your development with whatever compiler is most convenient
and then recompile with your best optimizing C++ compiler
just before you distribute the production release.
since the object will get destroyed when the variable gets out of scope.
Victor Bazarov <v.********@com Acast.net> wrote: Daniel T. wrote: [...] Try returning the object by const value first, [...]
Top-level const qualifiers are meaningless in most situations. And I mean
const int foo();
When it comes to basic types like 'int' I agree, but not with user
defined types. For example if we have:
value foo();
Then clients can do:
value myValue = foo().modify();
Then changing to 'const value&' will break the program. Better is to
return 'const value' so that clients can't modify the temporary (making
user defined types more like basic types) thus allowing us to change to
a 'const value&' without having to modify any client code. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Web Developer |
last post by:
Hi,
Can someone provide a short and concise statement(s) on the difference
between pointers and references. A graphical representation (via links?) of
both would be much appreciated as well.
WD
|
by: cppaddict |
last post by:
I understand that there are a couple differences between reference
variables and pointer:
1) reference variables *must* be initialized.
2) You cannot change what a reference variable refers to.
My question is: In what situations is it better to use a reference
variable over a pointer? Also, are there other differences besides
the ones I listed above?
|
by: Neal Coombes |
last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for
better from the unmoderated groups:
-------- Original Message --------
Subject: Return appropriately by value, (smart) pointer, or reference
Date: 27 Aug 2005 15:13:23 -0400
From: Neal Coombes <nealc@trdlnk.com>
Organization: Aioe.org NNTP Server
To: (Usenet)
Newsgroups: comp.lang.c++.moderated
|
by: wellstone9912 |
last post by:
Java programmers seem to always be whining about how confusing and
overly complex C++ appears to them. I would like to introduce an
explanation for this. Is it possible that Java programmers simply
aren't smart enough to understand C++?
This is not merely a whimsical hypothesis. Given my experience with
Java programmers --- the code they write and the conversations they
have --- Occam's Razor points to this explanation. For example,...
|
by: Zytan |
last post by:
I know there are no pointers in C#, but if you do:
a = b;
and a and b are both arrays, they now both point to the same memory
(changing one changes the other). So, it makes them seem like
pointers.
Can someone please explain why? thanks.
Zytan
| |
by: cppquester |
last post by:
A colleague told me that there is a rule about good stype that a
function in C++ should have only one point of return (ie. return
statement). Otherwise there might be trouble.
I never heard about it and doubt it.
Anybody heard of it? What would be the advantage?
Regards,
Marc
Example:
|
by: MQ |
last post by:
Can someone tell me where I should use pointers and where I should use
references? In his book, Stroustrup says that you should use pointers
for passing arguments that are to be modified, not references. Yet,
in an interview, he laments the overuse of pointers and such in code.
It seems contradictory to me. Why should we not use references
instead of pointers? Can someone give me an idea of best practices in
this respect?
regards,...
|
by: Sebastian Schucht |
last post by:
Hi,
I have a templateclass with virtual member-functions, so i can't create
instances ... but for returning the result i would like use one. So i
replaced the A<TTypefrom the baseclass with the Type from the spezialized
class. After this, the error invalid covariant return type occoured.
the following minimal sample shows the problem:
#include <iostream>
|
by: Jim Langston |
last post by:
I remember there was a thread a while back that was talking about using the
return value of a function as a reference where I had thought the reference
would become invalidated because it was a temporary but it was stated that
it would not. This has come up in an irc channel but I can not find the
original thread, nor can I get any code to work.
Foo& Bar( int Val )
{
return Foo( Val );
}
|
by: Simon Saize |
last post by:
Hi -
What's the point of having references (&)? Why don't we just use
pointers (*)?
Thanks.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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.
| |