473,670 Members | 2,538 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::Choic es’ 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<typena me EnumStrucTstruc t EnumChoices: EnumStrucT
{EnumChoices(): EnumStrucT() {} // only works if EnumStrucT
default constructs
EnumChoices(con st 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.asS calar;}

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

const string asString() const
{return string(EnumStru cT::value.asCha rs, sizeof
EnumStrucT::val ue.asChars);}

const uint16_t asScalar() const {return EnumStrucT::val ue;}};

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<Tes tChcTest;

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

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 1434
On Sep 17, 7:49*am, Zak <Unceld...@gmai l.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<typena me EnumStrucTstruc t EnumChoices: EnumStrucT
* * {EnumChoices(): EnumStrucT() {} // only works if EnumStrucT
default constructs
* * *EnumChoices(co nst 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::Choice s -TestChc
TestChc -Test

Ali

P.S. Your code needs lots of vertical white space to be readable.
Sep 17 '08 #2
In article <ga**********@a ioe.org>, Andrey Tarasevich
<an************ **@hotmail.comw rote:
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....@gish puppy.com (blargg) wrote:
In article <gare52$75...@a ioe.org>, Andrey Tarasevich
<andreytarasev. ..@hotmail.comw rote:
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 objektorientier ter Datenverarbeitu ng
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
4842
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
2585
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
2160
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, promotion, conversion, not possible) that is, what i don't really understand. so i tried do make a little
5
1693
by: j0mbolar | last post by:
i know what type promotion is but why is it important?
112
4356
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 between C89 and C99, I would like to know. I have tried with different compilers, and I see some differences. Before I file a bug report with my C vendor, I would like to know what the correct behavior is. struct S
4
5162
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 version is made freely available at http://publications.gbdirect.co.uk/c_book/) ....snip... The position is clearer if an unsigned operand is right shifted, because there is no choice: it must be a logical shift. For that reason, whenever right...
21
4108
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. Could someone please explain to me what's going on? I would have thought that the following happens: (1) The literal, 0, whose type is int, gets converted to an unsigned char.
15
7595
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
2370
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 its prototype. Let's say for instance : foo.c #include <stdio.h>
0
8384
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,...
0
8901
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, 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...
0
8813
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 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...
1
8591
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,...
0
8659
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 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...
0
4208
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...
1
2799
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
2
2037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1791
bsmnconsultancy
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...

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.