473,394 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

avoid implicit cctor call

Hi,

here the following test code

#include <iostream>

using namespace std;

struct Foo {
Foo() { cout << " Foo()\n"; }
~Foo() { cout << "~Foo()\n"; }
void set() const { cout << "Foo::set()\n"; }
};

struct Bar {
void set(const Foo& foo) const { foo.set(); }
};

int main() {
Bar b;
b.set( Foo() );
}

shows:

Foo()
Foo()
Foo::set()
~Foo()
~Foo()

Well, obviously the copy ctor from temporary is called. Can I avoid this
by using a different way?
Background: Foo can contain strings (should not be the problem) but also
function pointers and lists. About the last, I'm not sure about the copy
semantic. Any help here?

Thanks,
Olaf
Jul 26 '07 #1
9 1644
Olaf wrote:
here the following test code

#include <iostream>

using namespace std;

struct Foo {
Foo() { cout << " Foo()\n"; }
~Foo() { cout << "~Foo()\n"; }
void set() const { cout << "Foo::set()\n"; }
};

struct Bar {
void set(const Foo& foo) const { foo.set(); }
};

int main() {
Bar b;
b.set( Foo() );
}

shows:

Foo()
Foo()
Foo::set()
~Foo()
~Foo()

Well, obviously the copy ctor from temporary is called. Can I avoid
this by using a different way?
If you want to use a temporary, no, there is no other way. If you
could have a separate variable of type 'Foo', you could pass by non-
const refefence (Foo&), if you promise not to call any non-const
functions, or just a pointer to const Foo - that guarantees no copying.

The language definition says that a copy can be made during binding
of a reference to a temporary. It does not have to be, but it can.
Background: Foo can contain strings (should not be the problem) but
also function pointers and lists. About the last, I'm not sure about
the copy semantic. Any help here?
If you mean 'std::list', its copy semantics are well defined, but
copying collections is expensive, so you should probably try to avoid
those.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 26 '07 #2
On Jul 26, 8:20 pm, Olaf <o...@mdcc.dewrote:
Hi,

here the following test code

#include <iostream>

using namespace std;

struct Foo {
Foo() { cout << " Foo()\n"; }
~Foo() { cout << "~Foo()\n"; }
void set() const { cout << "Foo::set()\n"; }

};

struct Bar {
void set(const Foo& foo) const { foo.set(); }

};

int main() {
Bar b;
b.set( Foo() );

}

shows:

Foo()
Foo()
Foo::set()
~Foo()
~Foo()

Well, obviously the copy ctor from temporary is called. Can I avoid this
by using a different way?
*This is obviously wrong. Since you have not prepared a copy-ctor,any
copy construction would be silent(no-output).*
struct Bar {
void set(const Foo& foo) const { foo.set(); }
};
As 'Bar::set' accepts a reference - not a value - as its parameter no
copy-construction is invoked.I guess this output does not belong to
this code.You must have declared a 'Foo' object in your original code
which I guess is omited here because:
shows:

Foo()
Foo()
Foo::set()
~Foo()
~Foo()
this means twice default-construction and twice destruction.

Bar b;
b.set( Foo() );
Is 'Bar' derived form 'Foo'? This could also lead to the above output
as a result of default-construction of 'b'.

regards,
FM.

Jul 26 '07 #3
terminator schrieb:
On Jul 26, 8:20 pm, Olaf <o...@mdcc.dewrote:
>Hi,

here the following test code

#include <iostream>

using namespace std;

struct Foo {
Foo() { cout << " Foo()\n"; }
~Foo() { cout << "~Foo()\n"; }
void set() const { cout << "Foo::set()\n"; }

};

struct Bar {
void set(const Foo& foo) const { foo.set(); }

};

int main() {
Bar b;
b.set( Foo() );

}

shows:

Foo()
Foo()
Foo::set()
~Foo()
~Foo()

Well, obviously the copy ctor from temporary is called. Can I avoid this
by using a different way?

*This is obviously wrong. Since you have not prepared a copy-ctor,any
copy construction would be silent(no-output).*
Yes, you are right. I did verify the snippet again. No idea what I did
before (probably for test purpose there was a Foo created on stack).
Sorry for confusion.

Anyway, here a more real problem of me:

#include <iostream>
#include <boost/utility.hpp>

using namespace std;

struct Foo : public boost::noncopyable {
Foo() { cout << " Foo()\n"; }
~Foo() { cout << "~Foo()\n"; }
//Foo( const Foo& ) { cout << " Foo(const Foo&)\n"; }
void set() const { cout << "Foo::set()\n"; }
};

struct Bar {
void set(const Foo& foo) const { foo.set(); }
};

int main()
{
Bar b;

b.set( Foo() );
}

$ LANG=en g++ cctor.cpp -o cctor && ./cctor
/usr/include/boost/noncopyable.hpp: In copy constructor 'Foo::Foo(const
Foo&)':
/usr/include/boost/noncopyable.hpp:27: error:
'boost::noncopyable_::noncopyable::noncopyable(con st
boost::noncopyable_::noncopyable&)' is private
cctor.cpp:6: error: within this context
cctor.cpp: In function 'int main()':
cctor.cpp:21: note: synthesized method 'Foo::Foo(const Foo&)' first
required here

Background more precise: Foo can have std::list<Tand function pointer
private data.

I can verify where the copy ctor is involved. Using the own (out
commented) Foo(const Foo&) did compile and run fine, but I'm not shure
about side effects here.

Thanks,
Olaf
Jul 26 '07 #4
Olaf wrote:
[..] here a more real problem of me:

#include <iostream>
#include <boost/utility.hpp>

using namespace std;

struct Foo : public boost::noncopyable {
Foo() { cout << " Foo()\n"; }
~Foo() { cout << "~Foo()\n"; }
//Foo( const Foo& ) { cout << " Foo(const Foo&)\n"; }
void set() const { cout << "Foo::set()\n"; }
};

struct Bar {
void set(const Foo& foo) const { foo.set(); }
};

int main()
{
Bar b;

b.set( Foo() );
Binding of a reference to a temporary *requires* the type to be
copy-constructible. You basically want to have your cake and eat
it too. Can't happen. Either make it copy-constructible, or
don't expect to be able to bind a const reference to a temporary.
}

$ LANG=en g++ cctor.cpp -o cctor && ./cctor
/usr/include/boost/noncopyable.hpp: In copy constructor
'Foo::Foo(const Foo&)':
/usr/include/boost/noncopyable.hpp:27: error:
'boost::noncopyable_::noncopyable::noncopyable(con st
boost::noncopyable_::noncopyable&)' is private
cctor.cpp:6: error: within this context
cctor.cpp: In function 'int main()':
cctor.cpp:21: note: synthesized method 'Foo::Foo(const Foo&)' first
required here

Background more precise: Foo can have std::list<Tand function
pointer private data.

I can verify where the copy ctor is involved. Using the own (out
commented) Foo(const Foo&) did compile and run fine, but I'm not shure
about side effects here.
Side effects of what?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 26 '07 #5
>Background more precise: Foo can have std::list<Tand function
>pointer private data.

I can verify where the copy ctor is involved. Using the own (out
commented) Foo(const Foo&) did compile and run fine, but I'm not shure
about side effects here.

Side effects of what?
The legacy C code get function pointers, allocated memory regions ...
Maybe these will be affected.

Thanks,
Olaf
Jul 27 '07 #6
On Jul 26, 10:42 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Binding of a reference to a temporary *requires* the type to be
copy-constructible. You basically want to have your cake and eat
it too. Can't happen. Either make it copy-constructible, or
don't expect to be able to bind a const reference to a temporary.
what is the catch from such a restiriction?

regards,
FM.

Jul 27 '07 #7
terminator wrote:
On Jul 26, 10:42 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>Binding of a reference to a temporary *requires* the type to be
copy-constructible. You basically want to have your cake and eat
it too. Can't happen. Either make it copy-constructible, or
don't expect to be able to bind a const reference to a temporary.

what is the catch from such a restiriction?
I don't understand the question, sorry. "The catch"?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 27 '07 #8
On Jul 27, 3:41 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
terminator wrote:
On Jul 26, 10:42 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Binding of a reference to a temporary *requires* the type to be
copy-constructible. You basically want to have your cake and eat
it too. Can't happen. Either make it copy-constructible, or
don't expect to be able to bind a const reference to a temporary.
what is the catch from such a restiriction?

I don't understand the question, sorry. "The catch"?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
what is the benefit?

Jul 27 '07 #9
terminator wrote:
On Jul 27, 3:41 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>terminator wrote:
>>On Jul 26, 10:42 pm, "Victor Bazarov" <v.Abaza...@comAcast.net>
wrote:
Binding of a reference to a temporary *requires* the type to be
copy-constructible. You basically want to have your cake and eat
it too. Can't happen. Either make it copy-constructible, or
don't expect to be able to bind a const reference to a temporary.
>>what is the catch from such a restiriction?

I don't understand the question, sorry. "The catch"?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

what is the benefit?
The benefit? I am not sure how to answer you. What is the benefit
from the limitation that to convert a pointer to a derived class to
a pointer to a base class you have to have the base class accessible?
Or, what's the benefit from the limitation that to convert void* to
a pointer to object one needs a 'static_cast'? Those limitations
often exist because without them it would be difficult to implement
a compiler, or other rules would be too complicated. What's the
benefit of limiting (from below) the age at which people can start
driving cars or vote? Avoiding chaos, avoiding having to prosecute
minors for a vehicular manslaughter.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 27 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and...
25
by: Chad Z. Hower aka Kudzu | last post by:
I have defined some implicit convertors so that I can do: MyStruct x = 4; The implicit convert the 4 into MyStruct. But its essentially a copy constructor and thus if I had: MyStruct x;...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
5
by: Adriano Coser | last post by:
Hello. I'm moving an application from VC 2003 to VC 2005 Beta2. In mixed mode DLLs I need to set System::Threading::Thread::CurrentThread->ApartmentState =...
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
9
by: Alex Vinokur | last post by:
Compiler Green Hills C++, Version 4.0.6 --- foo.cpp --- struct A { }; struct B { B() {}
18
by: Tony | last post by:
class Interface { public: virtual void DoItNow()=0; }; class A: public Interface { public: void DoItNow(); // satisfies interface explicitly
1
by: not_a_commie | last post by:
I have an Angle class that I store angles in. It's basically just a bunch of fancy functions for manipulating a double. It has implicit casts for converting to/from double. Due to the project...
5
by: johanatan | last post by:
Does anyone know the reasons for the lack of an implicit casting operator in any greater depth than: A. Automatic conversion is believed to be too error prone. (from the FAQ at the bottom of:...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.