473,396 Members | 2,018 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,396 software developers and data experts.

A const behavior I don't understand

Hello,

I have the following code which I don't understand why it works :

#include <iostream>
using namespace std;

void DontWork (unsigned int& i) {
cout << i << endl;
}

void Work (unsigned int const& i) {
cout << i << endl;
}

void main () {
signed int i = 1;
// DontWork (i);
Work (i);
}

What I don't understand is why the compiler can find an implicit
conversion when I add the const modifier, but it is unable to do so
without it.

(I know I should not code like this, but I just want to understand the
behavior.)

Thank you,
Ben.
Jun 27 '08 #1
3 1536

"Ben Thomas" <be********@wickedstudios.comwrote in message
news:8h***************@wagner.videotron.net...
Hello,

I have the following code which I don't understand why it works :

#include <iostream>
using namespace std;

void DontWork (unsigned int& i) {
cout << i << endl;
}

void Work (unsigned int const& i) {
cout << i << endl;
}

void main () {
signed int i = 1;
// DontWork (i);
Work (i);
}

What I don't understand is why the compiler can find an implicit
conversion when I add the const modifier, but it is unable to do so
without it.

(I know I should not code like this, but I just want to understand the
behavior.)

Thank you,
Ben.
I compiled your code and looked up the error code (MSVC++). See
the last two paragraphs .

================================================== ==================
Error Message
'function' : cannot convert parameter number from 'type1' to 'type2'

A parameter cannot be converted to the required type. This might
happen if you create an instance of a class and attempt an implicit
conversion on a constructor marked with the explicit keyword.

If a temporary object is passed to a function that takes a reference
to an object as a parameter, that reference must be a const reference.

If the function is called with a parameter that is not of the type
that the function expects, a temporary object is created using the
appropriate constructor. This temporary object is then passed to
the function. In this case, the temporary object is used to initialize
the reference. In earlier versions of the language, all references
could be initialized by temporary objects.
================================================== ==================
Of course a particular implementation is not authoritative about the
language standard, but the above explanation makes sense to me.
BTW main()'s required (by the standard) return type is 'int', even
if a compiler lets you get away with 'void'.

-Mike
Jun 27 '08 #2
Thanks Mike for the clarification, I understand why it works/don't works
now.

Ben.

Mike Wahler wrote:
"Ben Thomas" <be********@wickedstudios.comwrote in message
news:8h***************@wagner.videotron.net...
>Hello,

I have the following code which I don't understand why it works :

#include <iostream>
using namespace std;

void DontWork (unsigned int& i) {
cout << i << endl;
}

void Work (unsigned int const& i) {
cout << i << endl;
}

void main () {
signed int i = 1;
// DontWork (i);
Work (i);
}

What I don't understand is why the compiler can find an implicit
conversion when I add the const modifier, but it is unable to do so
without it.

(I know I should not code like this, but I just want to understand the
behavior.)

Thank you,
Ben.

I compiled your code and looked up the error code (MSVC++). See
the last two paragraphs .

================================================== ==================
Error Message
'function' : cannot convert parameter number from 'type1' to 'type2'

A parameter cannot be converted to the required type. This might
happen if you create an instance of a class and attempt an implicit
conversion on a constructor marked with the explicit keyword.

If a temporary object is passed to a function that takes a reference
to an object as a parameter, that reference must be a const reference.

If the function is called with a parameter that is not of the type
that the function expects, a temporary object is created using the
appropriate constructor. This temporary object is then passed to
the function. In this case, the temporary object is used to initialize
the reference. In earlier versions of the language, all references
could be initialized by temporary objects.
================================================== ==================
Of course a particular implementation is not authoritative about the
language standard, but the above explanation makes sense to me.
BTW main()'s required (by the standard) return type is 'int', even
if a compiler lets you get away with 'void'.

-Mike

Jun 27 '08 #3
On 19 Jun, 17:54, Ben Thomas <ben.tho...@wickedstudios.comwrote:
Hello,

I have the following code which I don't understand why it works :

#include <iostream>
using namespace std;

void DontWork (unsigned int& i) {
* * * * cout << i << endl;

}

void Work (unsigned int const& i) {
* * * * cout << i << endl;

}

void main () {
* * * * signed int i = 1;
* * * * // DontWork (i);
* * * * Work (i);

}

What I don't understand is why the compiler can find an implicit
conversion when I add the const modifier, but it is unable to do so
without it.
In the process of converting from 'int' to 'unsigned int' a temporary
object is created. You cannot bind a non-const reference to a
temporary object.

Consider the example:
#include <iostream>
int main ()
{
int i = 1;
// fails: binding a non-const ref to a temporary
unsigned& uref = i;

// ok, binding const ref to temporary
const unsigned& curef = i;

i = 2;
if( i != curef)
std::cout << "curef is not bound to i";
}

You may think that the reference curef is bound to i, but it is not,
it is bound to an unnamed temporary object, as illustrated by the
example.

DP
Jun 27 '08 #4

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

Similar topics

39
by: JKop | last post by:
Back when I read my first C++ book, I was given the following scenario: class Cheese { public: int number_of_holes; int colour;
5
by: Kenneth Massey | last post by:
I have run into a peculiar problem, in which the following sample code does not compile (gcc 3.3). I have a template class with a member function that should take a const version of the template...
1
by: yccheok | last post by:
Hi, consider the following bug at #include <stdio.h> class Drink { public: Drink( char *name_ ) : name( name_ ) {} char *name; char & get(int i) { return name; } };
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
83
by: user | last post by:
Hello, Here is the program #include stdio int main(void) { const int num = 100; int *ip;
6
by: Spoon | last post by:
Hello, I don't understand why gcc barks at me in this situation: $ cat foo.c extern void func(const int * const list, int nent); int main(void) { int *p;
3
by: George2 | last post by:
Hello everyone, I am debugging MSDN code from, http://msdn2.microsoft.com/en-us/library/0eestyah(VS.80).aspx here is my output, 1>main.cpp
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
39
by: Leonardo Korndorfer | last post by:
Hi, I'm litle confused by the const modifier, particularly when use const char* or char*. Some dude over here said it should be const char when you dont modify it content inside the function, I...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...

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.