473,379 Members | 1,253 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,379 software developers and data experts.

T t(a) not equivalent to T t = a

I often see it written here that the declaration
T t = a;
(where 'a' is not of type T) calls the copy-constructor and
is exactly equivalent to
T t(( T(a) ));

However the following code gives me a compiler error:

#include <string>

struct M
{
M(std::string const &s);
};

int main()
{
M a ("Hello");
M b = M("Hello");
M c ( M("Hello") );
M d = "Hello";
return 0;
}

m.cc:13: error: conversion from `const char[6]' to non-scalar type `M'
requested

The error is one the "M d" line, and the other lines compiled
correctly. What's going on?

Jul 23 '05 #1
7 1415

"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
I often see it written here that the declaration
T t = a;
(where 'a' is not of type T) calls the copy-constructor and
is exactly equivalent to
T t(( T(a) ));

However the following code gives me a compiler error:

#include <string>

struct M
{
M(std::string const &s);
};

int main()
{
M a ("Hello");
M b = M("Hello");
M c ( M("Hello") );
M d = "Hello";
return 0;
}

m.cc:13: error: conversion from `const char[6]' to non-scalar type `M'
requested

The error is one the "M d" line, and the other lines compiled
correctly. What's going on?


M d = std::string("Hello"); // expecting std::string

Implicity convertion to std::string from char* is not allowed, I guess...

Ben
Jul 23 '05 #2
Me
Old Wolf wrote:
I often see it written here that the declaration
T t = a;
(where 'a' is not of type T) calls the copy-constructor and
is exactly equivalent to
T t(( T(a) ));

However the following code gives me a compiler error:

#include <string>

struct M
{
M(std::string const &s);
};

int main()
{
M a ("Hello");
M b = M("Hello");
M c ( M("Hello") );
M d = "Hello";
return 0;
}

m.cc:13: error: conversion from `const char[6]' to non-scalar type `M'
requested

The error is one the "M d" line, and the other lines compiled
correctly. What's going on?


Totally simplified answer: the standard limits up to 2 implicit
conversions to occur. Variable d looks like const char[6] -> const char
* (this one is a freebie so it doesn't really count) -> std::string ->
M.

Jul 23 '05 #3
Old Wolf wrote:
I often see it written here that the declaration
T t = a;
(where 'a' is not of type T) calls the copy-constructor and
is exactly equivalent to
T t(( T(a) ));

However the following code gives me a compiler error:

#include <string>

struct M
{
M(std::string const &s);
};

int main()
{
M a ("Hello");
M b = M("Hello");
M c ( M("Hello") );
M d = "Hello";
return 0;
}

m.cc:13: error: conversion from `const char[6]' to non-scalar type `M'
requested

The error is one the "M d" line, and the other lines compiled
correctly. What's going on?


The problem in line 13 (the last line before the return statement) is
that it requires two user-defined implicit conversions:

const char[6] -> std::string -> M

in order to complete the operation. The C Standard limits user-defined
implicit conversions of a single value to no more than one in any
single expression.

Greg

Jul 23 '05 #4
The error seems very simple, you didn't implement the constructor, but
you have define the constructor explicitly.

Try the following:

struct M
{
M(std::string const &s){};
};

Aries Sun

Jul 23 '05 #5


Aries Sun schreef:
The error seems very simple, you didn't implement the constructor, but
you have define the constructor explicitly.


True, but that doesn't give you a compiler error. Missing definitions
cause linker errors. The OP might link to M.cpp which defines the M
ctor.
The other explanations are correct, though: there is no conversion
from char const* to M.

HTH,
Michiel Salters

Jul 23 '05 #6
Me wrote:
Old Wolf wrote:
I often see it written here that the declaration
T t = a;
(where 'a' is not of type T) calls the copy-constructor and
is exactly equivalent to
T t(( T(a) ));

However the following code gives me a compiler error:

#include <string>

struct M
{
M(std::string const &s);
};

int main()
{
M a ("Hello");
M b = M("Hello");
M c ( M("Hello") );
M d = "Hello";
return 0;
}

m.cc:13: error: conversion from `const char[6]' to non-scalar type `M'
requested

The error is one the "M d" line, and the other lines compiled
correctly. What's going on?


Totally simplified answer: the standard limits up to 2 implicit
conversions to occur. Variable d looks like const char[6] -> const char
* (this one is a freebie so it doesn't really count) -> std::string ->
M.


So why does the "M a" line compile? That has two conversions:
const char[6] -> std::string -> M.

I guess you saying that the ( ) syntax counts as an explicit
conversion but the = syntax does not, even though they are
both initialization?

Jul 23 '05 #7
Me
Old Wolf wrote:
struct M
{
M(std::string const &s);
}; Totally simplified answer: the standard limits up to 2 implicit
conversions to occur. Variable d looks like const char[6] -> const char
* (this one is a freebie so it doesn't really count) -> std::string ->
M.


So why does the "M a" line compile? That has two conversions:
const char[6] -> std::string -> M.


What I meant by the above deals with where the compiler generates its
own temporaries to do the conversion as opposed to the programmer
explicitly creating an unnamed object along the way.
I guess you saying that the ( ) syntax counts as an explicit
conversion but the = syntax does not, even though they are
both initialization?


Greg's answer in this thread was better than mine since he had it in
terms of user defined conversions and mine was just implicit
conversions while not counting the freebies that occur. But if you're
still confused, here is how the compiler might expand these to (I'm
going to use trailing underscores to note the temporaries the compiler
generates):

M a("Hello");
// becomes:
std::string temp_("Hello");
M a(temp_);

M b = M("Hello");
// becomes:
std::string temp_("Hello");
M temp(temp_);
M b(temp);

M c ( M("Hello") );
// becomes:
std::string temp_("Hello");
M temp(temp_);
M c(temp);

M d = "Hello";
// becomes:
std::string temp_("Hello");
M temp__(temp_);
M d(temp__);

M e = std::string("Hello");
// becomes:
std::string temp("Hello");
M temp_(temp);
M e(temp_);

I hope you can see why d doesn't work but the others do.

Jul 23 '05 #8

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

Similar topics

14
by: John | last post by:
Is there an equivalent of COM on Linux that I can get through Python. My need is to have some sort of language independent component framework. I can think of CORBA but I have to have a server...
2
by: Michael Foord | last post by:
Please pardon my ignorance on this one - but I'm not certain how the sign bt is treated in python bitwise operators. I've trying to convert a javascript DES encryption routine into python. ...
3
by: Robert Dodier | last post by:
Hello, Here's a thought that I'm sure has already occurred to someone else, I just can't find any record of it yet. An XML document is just a more verbose and clumsy representation of an...
1
by: Vannela | last post by:
Is there any equivalent control in .NET for the Power builder DataWindow control? I am explaining the Datawindow architecture to some extent. Power Builder DataWindow Control has got different...
6
by: Frank Rachel | last post by:
So I can focus on the correct areas of research, I was wondering if someone could give me the .NET equivelents for this J2EE architecture: JSP's make calls to local JavaBean Controls. The...
3
by: Marty | last post by:
Hi, What is the VB.NET equivalent of the VB6 ADODB.Connector and ADODB.Recordset? Thanks Marty
7
by: Tim Conner | last post by:
Hi, I am an ex-delphi programmer, and I having a real hard time with the following simple code (example ): Which is the equivalent to the following code ? var chars : PChar; sBack, s :...
10
by: karch | last post by:
How would this C# contruct be represented in C++/CLI? Or is it even possible? PolicyLevel level = (enumerator->Current) as PolicyLevel; Thanks, Karch
9
by: Alan Silver | last post by:
Hello, I'm converting some old VB6 code to use with ASP.NET and have come unstuck with the Asc() function. This was used in the old VB6 code to convert a character to its ASCII numeric...
14
by: grid | last post by:
Hi, I have a certain situation where a particular piece of code works on a particular compiler but fails on another proprietary compiler.It seems to have been fixed but I just want to confirm if...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.