473,511 Members | 16,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

type promotion for some constructors but not others

Zak
I have some c++ source where construction via

Type var(init); // compiles and...

Type var = init; // does not

I thought the two are supposed to be completely equivalent? Why does
type promotion seem to take place on the first line, but not the
second? Is there a way it can be enabled for the second?

In this case, init is an enum constant contained within Type. The
compiler is g++ 4.x. Both lines work with VS8, unless you turn
language extensions off, in which case you get the g++ behavior.
(error: conversion from ‘TestChc::Choices’ to non-scalar type ‘Test’
requested). Here's the complete code:

#include <iostream>
#include <string>
using namespace std;

#define TWOCHARINT(A, B) (A+(B<<8)) // FIXME: wrong for big endian
archs

template<typename EnumStrucTstruct EnumChoices: EnumStrucT
{EnumChoices(): EnumStrucT() {} // only works if EnumStrucT
default constructs
EnumChoices(const EnumStrucT& arg): EnumStrucT(arg) {}
EnumChoices& operator=(const EnumStrucT& arg)
{this->value = arg.value; return *this;} // ^ EnumStrucT handles
derivations

bool operator== (const EnumStrucT& arg) const
{return this->value.asScalar==arg.value.asScalar;}

bool operator!= (const EnumStrucT& arg) const
{return this->value.asScalar!=arg.value.asScalar;}

const string asString() const
{return string(EnumStrucT::value.asChars, sizeof
EnumStrucT::value.asChars);}

const uint16_t asScalar() const {return EnumStrucT::value;}};

struct TestChc
{enum Choices {CN = 0, C1 = '1', CB = TWOCHARINT('A', 'B'), CS = '
', CZ = 'Z'};
union Value
{uint16_t asScalar;
char asChars[2];
Value(const Choices& arg): asScalar(arg) {}} value;
TestChc(const Choices& arg):value(arg) {}};

typedef EnumChoices<TestChcTest;

Test& func() {return *(new Test(TestChc::CZ));}

int main (int, char**)
{Test t(TestChc::CB); // , tf('X'); is invalid
// Test t2 = TestChc::CB; // <- this works in VS
Test t2 = func();
// Test t2; // this only works if TestChc default constructs
// Test t2(TestChc::CS);
cout << "size is " << sizeof t2 << endl;
cout << t.asString() << '\t' << t2.asString() << endl;
t = t2;
cout << (t==t2) << endl;
t = TestChc::C1;
cout << (t!=t2) << endl;
return 0;}

There's a little more than needed there, but the crux of the question
is why does the first declaration in main compile while the second (if
uncommented) does not?

Thanks in advance.
Zachary
Sep 17 '08 #1
3 1423
On Sep 17, 7:49*am, Zak <Unceld...@gmail.comwrote:
I have some c++ source where construction via

* * Type var(init); *// compiles and...

* * Type var = init; *// does not
Because the compiler considers only one user defined conversion.
* * template<typename EnumStrucTstruct EnumChoices: EnumStrucT
* * {EnumChoices(): EnumStrucT() {} // only works if EnumStrucT
default constructs
* * *EnumChoices(const EnumStrucT& arg): EnumStrucT(arg) {}
* * *EnumChoices& operator=(const EnumStrucT& arg)
* * *{this->value = arg.value; return *this;} // ^ EnumStrucT handles
derivations
* * struct TestChc
* * {enum Choices {CN = 0, C1 = '1', CB = TWOCHARINT('A', 'B'),CS = '
', CZ = 'Z'};
* * *union Value
* * *{uint16_t asScalar;
* * * char asChars[2];
* * * Value(const Choices& arg): asScalar(arg) {}} value;
* * *TestChc(const Choices& arg):value(arg) {}};
* * // Test t2 = TestChc::CB; // <- this works in VS
Two conversions needed, which the compiler will not consider:

TestChc::Choices -TestChc
TestChc -Test

Ali

P.S. Your code needs lots of vertical white space to be readable.
Sep 17 '08 #2
In article <ga**********@aioe.org>, Andrey Tarasevich
<an**************@hotmail.comwrote:
Zak wrote:
I have some c++ source where construction via

Type var(init); // compiles and...

Type var = init; // does not

I thought the two are supposed to be completely equivalent?

They are not. They are equivalent only in one particular case when the
source and destination types are the same.
Even in that case, the second form can create a temporary if init is not
Type or something derived from it. That is

Type var = init;

can generate either of the following code-wise

Type var( (Type(init)) );
Type var(init);

but must still check for validity as if the first form were used.

See section 8.5 paragraph 14 of the 2003 C++ standard.
Sep 17 '08 #3
On Sep 17, 9:04 pm, blargg....@gishpuppy.com (blargg) wrote:
In article <gare52$75...@aioe.org>, Andrey Tarasevich
<andreytarasev...@hotmail.comwrote:
Zak wrote:
I have some c++ source where construction via
Type var(init); // compiles and...
Type var = init; // does not
I thought the two are supposed to be completely equivalent?
They are not. They are equivalent only in one particular
case when the source and destination types are the same.
Even in that case, the second form can create a temporary if
init is not Type or something derived from it.
In theory, at least.
That is
Type var = init;
can generate either of the following code-wise
Type var( (Type(init)) );
Type var(init);
but must still check for validity as if the first form were used.
Except that if init has type Type, then if the second is legal,
so is the first. In both cases, you're constructing a Type from
another type.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 17 '08 #4

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

Similar topics

8
4836
by: BigMan | last post by:
Can someone cite the rules for type promotion in C++? And, in particular, what is the type of the result of adding 2 values of type char?
31
2560
by: xah | last post by:
what's the pro and con of using <script language="javascript"> vs <script type="text/javascript"> Xah xah@xahlee.org ∑ http://xahlee.org/
2
2149
by: Andy | last post by:
Hi... i'm trying to understand the concept of function name overloading in c++. to understand the resolving system it's important to understand the diffrent levels of typecasting (exact match,...
5
1686
by: j0mbolar | last post by:
i know what type promotion is but why is it important?
112
4277
by: Carsten Hansen | last post by:
Suppose I'm using an implementation where an int is 16 bits. In the program below, what function is called in the first case, and what is called in the second case? Also, if there is a difference...
4
5142
by: kernelxu | last post by:
Hi,folks. I got some suggestion about bitwise shift from <The C Book, second edition>(written by Mike Banahan, Declan Brady and Mark Doran, originally published by Addison Wesley in 1991. This...
21
4079
by: Frederick Gotham | last post by:
I set about trying to find a portable way to set the value of UCHAR_MAX. At first, I thought the following would work: #define UCHAR_MAX ~( (unsigned char)0 ) However, it didn't work for me....
15
7554
by: shuisheng | last post by:
Dear All, Assume I have a class named Obj. class Obj { }; And a class named Shape which is derived from Obj. class Shape: public Obj
9
2355
by: Yannick | last post by:
Hi everyone - I am not quite sure to understand what is really going on when a function defined in one translation unit calls a function defined in a different translation unit without knowing...
0
7138
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
7353
Oralloy
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,...
0
7418
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...
1
7075
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
7508
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...
1
5063
isladogs
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...
0
4737
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...
0
3222
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...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.