473,472 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Parameter mismatch with ?: operator

My compiler rejects this:

std::string foo()
{
std::string s;
return some_condition() ? s : "";
}

saying that the last two operands of ?: must be the same type.
I have to fix it by changing "" to std::string() .

Is this correct behaviour? I remember reading that the compiler
ought to try implicitly converting one of the operands to the
type of the other.

Aug 30 '05 #1
5 1399
>std::string foo()
{
std::string s;
return some_condition() ? s : "";
}
My Compiler is not showing any problem : gcc 3.4.2, vc++7.1
saying that the last two operands of ?: must be the same type. Absolutely right. I have to fix it by changing "" to std::string() .

It should be implicit conversion.Most probably your compiler either
don't support it or it has a option for it and that option is turned
off.

Aug 30 '05 #2
> My compiler rejects this:

What compiler do you use?
Aug 30 '05 #3
"Old Wolf" <ol*****@inspire.net.nz> schrieb:
std::string foo()
{
std::string s;
return some_condition() ? s : "";
}

saying that the last two operands of ?: must be the same type. I
have to fix it by changing "" to std::string() .

Is this correct behaviour? I remember reading that the compiler
ought to try implicitly converting one of the operands to the type
of the other.


I would also suggest to fix this as you describe it yourself.
Otherwise the compiler looks for an implicit constructor matching a
const char* parameter. If your code is compiled with a STL
implementation using explicit constructors the compiler will not use
even a matching constructor.

I find implicit constructors bad style because people beeing not
familiar with this (especially beginners) don't even know that there's
a constructor and from what class. It's the same with function
parameters.

T.M.
Aug 30 '05 #4
In my opinion the compiler is right,
because you had to return an std::string
and "" is not a std::string.

Greetings Wolfgang...
Old Wolf wrote:
My compiler rejects this:

std::string foo()
{
std::string s;
return some_condition() ? s : "";
}

saying that the last two operands of ?: must be the same type.
I have to fix it by changing "" to std::string() .

Is this correct behaviour? I remember reading that the compiler
ought to try implicitly converting one of the operands to the
type of the other.

Aug 30 '05 #5
Gabriel wrote:
My compiler rejects this:


What compiler do you use?


Borland C++Builder 5 (compiler version 5.5.1).
It looks like it's just a compiler bug then -- the library
(Rogue Wave 2.1.1) doesn't use explicit constructors.

Aug 30 '05 #6

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

Similar topics

2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
4
by: Dave | last post by:
Hello all, I hope the context to my problem shown below is adequate but not overwhelming. I tried to keep it minimal. At the line indicated (in comment form) in the source below, the...
3
by: ded' | last post by:
Hello ! I've read in a magazine "reference parameter in operator= must be const, because in C++, temporary objects are const" and then my operator would not work with temporary objets. But,...
1
by: George Lewis | last post by:
Hi, I am trying to invoke a method from a COM dll using InvokeMember which has the following signature: int GetStatus(ref byte scannedBytes, System.Int32 length); However when I try and...
2
by: andrewbb | last post by:
This is fairly simple code, what am I missing or is there a potential workaround? It fails when passing someParms to Invoke with "Parameter count mismatch". I created a test with an empty form...
0
by: lrobo01 | last post by:
I'm having a problem with Crystal report.net with ASP.NET. The problem occurs when exporting the report. The report uses a discrete parameter with multiple values. When the report is loaded into...
0
by: lrobo01 | last post by:
I'm having a problem with Crystal report.net with ASP.NET. The problem occurs when exporting the report. The report uses a discrete parameter with multiple values. When the report is loaded into...
0
by: AJIT PITALE | last post by:
Hi, I am using Visual Basic 6 as front end and Visual FoxPro as a Database. I am trying to pass a parameter from VB to Visual FoxPro's Stored Procedure. I am getting an error showing "Data...
4
by: =?Utf-8?B?UHVjY2E=?= | last post by:
The function that I'm trying to call through DLLImport has a parameter that has a C code's vector's Itrator to a structure. I Have marshalled the structure in C# but how do I do the C type...
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...
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
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,...
1
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
agi2029
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,...
1
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.