Hi,
In my code I use two implicit casts in a row. VC compiled it in all
version I have. 7.1 and 8.0, but it seems not to be standard. Comeau in
strict mode and gcc don't accept it.
Of course, I can just throw a cast in C c = (C)b;
but this is not what I want. I want to enable implicit conversion.
Any idea?
Ingo
// main.cpp:
class A{};
class B
{
public:
operator A( )
{
return a;
}
A a;
};
class C
{
public:
C( A const& a ) { }
};
int main()
{
B b;
C c = b;
} 4 1587
nutty wrote: Hi,
In my code I use two implicit casts in a row. VC compiled it in all version I have. 7.1 and 8.0, but it seems not to be standard. Comeau in strict mode and gcc don't accept it.
Of course, I can just throw a cast in C c = (C)b;
but this is not what I want. I want to enable implicit conversion.
Any idea?
Ingo
// main.cpp:
class A{}; class B { public: operator A( ) { return a; } A a; }; class C { public: C( A const& a ) { } }; int main() { B b; C c = b; }
Very strange.
On my GNU compiler, it compiles if you use the following sysntax:
int main()
{
B b;
C c(b);
But technically, both syntax should evaluate to the same thing.
C c(b);
C c = b;
The compiler should fail with both, or accept both.
* Axter: C c(b); C c = b;
The compiler should fail with both, or accept both.
The declaration
C c(b);
is a "direct initialization" and involves one user-defined conversion
before c is reached: from B to A (the C constructor argument type).
C c = b;
is a "copy initialization" and involves two user-defined conversions
before c is reached: from B to A, and from A to C, because the right
side is a C object that c is copy-constructed from (although that copy
construction can and usually will be optimized away, it's there).
The standard allows only one implicit user-defined conversion, so that
the compiler won't outsmart the programmer (at least not a genius one).
You have the same situation with std::auto_ptr.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? The standard allows only one implicit user-defined conversion, so that the compiler won't outsmart the programmer (at least not a genius one).
You have the same situation with std::auto_ptr.
And in my real code it is actually a situation similar to auto_ptr,
where I use a reference counting pointer class. The pointer class
should be assigned the internal value of a proxy object, which is a raw
pointer.
So, next question: How do people resolve the issue with the auto ptr? I
mean, almost any issues that can be thought of, have been solved at
some time by somebody, right? :-) Very strange. On my GNU compiler, it compiles if you use the following sysntax: int main() { B b; C c(b);
But technically, both syntax should evaluate to the same thing. C c(b); C c = b;
The compiler should fail with both, or accept both.
by technicaly, you mean the generated instructions.
Of course, in this case I could write it like this. But then I have
peaces of code, where an assignment is affected, or an argument to a
function:
void func( C c );
func( b );
and
C c;
c = b;
in these cases I should either write
func( (C)b );
or
func( C(b) );
and
c = (C)c;
or
c = C( b );
That would surely work, but is actually an explicit cast.
The reason for my concern is, that I don't want to think and care that
there are casts involved. Also casts are even more powerful and may do
things that I don't want in some situations. They may invoke a
cunstructer which is declared explicit, where for some reason a
function returns the exact type that matches the cunstructor parameter. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: lomat |
last post by:
Hello,
I am using VisiBroker 4.0 on RedHat 8.0 with GCC 3.2.
When I do a "nm liborb_r.so | grep _invoke", I get some output. The symbol
of my intrest is manged as below,
002f6270 T _preinvoke__12CORBA_ObjectPCc
Now when I build an app that links to liborb_r.so, and uses the _preinvoke
API, GCC mangles this symbol as follows,
|
by: Nimmi Srivastav |
last post by:
Once and for all can someone kindly tell me the difference between C
and C++ linkage. I thought I understood it till someone showed me the
other day that C functions, that would ordinarily require the
extern "C"
declaration, for correct linkage into a C++ program, don't need it if
the filename, in which they are defined, ends in .cc or .cpp! Can
anyone explain this in a rational manner?
For the record, this is what I have heard...
|
by: Sonia |
last post by:
Why does C++ return a 0 and Visual C++ return a 1 when I execute this
program? Why the difference if they are both MS products?
#include <iostream>
using namespace std;
void main()
{
cout<<"123"<="89"<<endl;
|
by: WittyGuy |
last post by:
Hi,
What is the major difference between function overloading and function
templates?
Thanks!
http://www.gotw.ca/resources/clcm.htm for info about ]
|
by: xman |
last post by:
very hard is fix exactly group for this question.. but I am sure that some
of people here have some experience with these compilers: Visual Studio NET
Enterprise Architect 2002 and Visual Studio NET 2003
1. What is difference between Visual Studio NET Enterprise Architect 2002
and Visual Studio NET 2003
2. If I try user Visual Studio NET 2003, what I have not included inside (in
compare with Visual Studio NET 2003)?
| |
by: Jason Curl |
last post by:
I've seen the document N869.txt and the copy I could find talks about
C99. Is there anything that comprehensively describes the difference
between C89 and C99? I'd like to write code to be as portable as possible.
Thanks,
Jason.
|
by: Chen Shusheng |
last post by:
Hello,
I do a experiment on compile one file using different compilers.I got the
results below:
Output file1: hello1 14,332Byte under C-free
Output file2: hello2 4,096Byte under Dev-C++
Output file3: hello3 57,344Byte under Microsoft Visual C++ Express
2005
Why does these files vary so large?
Is there any method and tools of analyzing these .EXE files to see its
difference?
|
by: Sam of California |
last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing
of the source file"?
I responded to that comment by saying that the preprocessor is not just a
pass. It processes statements that the compiler does not process. The good
people in the alt.comp.lang.learn.c-c++ newsgroup insist that the
preprocessor is just one of many passes. The preprocessor processes a
grammer unique to the preprocessor and only that grammer.
...
|
by: anto frank |
last post by:
hi friends,
is ther any difference in array in c and array in c++?
|
by: puzzlecracker |
last post by:
I see that a lot of former in the code, and wonder if there is a
technical reason for that
|
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: 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: 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: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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...
| |