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

non-const reference to temporary derived from pure virtual Base

Hi Ng,

besides that this style may be asking for trouble i'm faced with a
unexpected gcc behaviour. Tested with gcc 3.3.3.
On M$ .Net this compiles just fine.

I'm trying to make a non-const reference to a temporary object. The
object is derived from a pure virtual base, so the compiler can't create
a copy of base-type.

Why does this not work with gcc?

To reproduce:
//-----------------------------------
class Base {
public:
Base() {};
virtual void pv() = 0;
virtual ~Base() {};
};

class Derived : public Base {
public:
Derived(int i) {};
virtual void pv() {};
virtual ~Derived() {};
};

class User {
public:
void useBase(Base& b) {};
void useBaseConst(const Base& b) {};
};
int main() {
User u;
Derived d(1);
// Ok with non-temporary !
u.useBase(d);

// not ok!
u.useBase(Derived(1));
//* no matching function for call to `User::useBase(Derived)'
//* candidates are: void User::useBase(Base&)
// seems that the compiler refuses to make a
// non-const reference to a temporary

// but this is ok!
u.useBaseConst(Derived(1));

}

Regards,

Michael
Jun 27 '08 #1
6 1405
Hi,

i striped it down further to:

class Derived {
public:
Derived(int i) {};
};

class User {
public:
void useBase(Derived& b) {};
};

int main() {
User u;
u.useBase(Derived(1));
}

I think it's just a "feature" to prevent writing code with surprising
results....
Jun 27 '08 #2
reppisch schrieb:
// not ok!
u.useBase(Derived(1));
//* no matching function for call to `User::useBase(Derived)'
//* candidates are: void User::useBase(Base&)
// seems that the compiler refuses to make a
// non-const reference to a temporary
Exactly that. Temporaries are like rvalues.

I had a similar question some time ago (maybe in the german group).
Somebody explained me that this is intended by the standard, because
there are risks otherwise.

You must create a non-temporary object for this purpuse.

Derived d(1);
u.useBase(d);

Marcel
Jun 27 '08 #3
On 2008-05-06 16:41, reppisch wrote:
Hi Ng,

besides that this style may be asking for trouble i'm faced with a
unexpected gcc behaviour. Tested with gcc 3.3.3.
On M$ .Net this compiles just fine.

I'm trying to make a non-const reference to a temporary object. The
object is derived from a pure virtual base, so the compiler can't create
a copy of base-type.
Are you sure VS.NET allowed that? If that is the case you really should
check the options you provide to the compiler because making a non-const
reference to a temporary is forbidden by the standard.

--
Erik Wikström
Jun 27 '08 #4
"Erik Wikström" <Er***********@telia.comwrote in message
On 2008-05-06 16:41, reppisch wrote:
>Hi Ng,

besides that this style may be asking for trouble i'm faced with a
unexpected gcc behaviour. Tested with gcc 3.3.3.
On M$ .Net this compiles just fine.

I'm trying to make a non-const reference to a temporary object. The
object is derived from a pure virtual base, so the compiler can't create
a copy of base-type.

Are you sure VS.NET allowed that? If that is the case you really should
check the options you provide to the compiler because making a non-const
reference to a temporary is forbidden by the standard.
Yes, by default VC allows this as an extension to Standrad (I tested a long
time back on probably v7.0). I think it's the /Za option in VC compiler that
would flash the error.

--
http://techytalk.googlepages.com
Jun 27 '08 #5
reppisch wrote:
...
i striped it down further to:
...
You can strip it down to

void foo(int&);
foo(1);

But the bottom line is that explicit binding of non-const references to
temporary objects is illegal in C++.

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #6
sk_usenet wrote:
...
Yes, by default VC allows this as an extension to Standrad (I tested a long
time back on probably v7.0). I think it's the /Za option in VC compiler that
would flash the error.
...
Yes, /Za will fix it.

Without /Za in VS2005 even they implemented a mix of standard behavior
and extended behavior so that the whole thing does seem to satisfy the
formal definition of "compiler extension", while the VC6 was genuinely
and hopelessly broken in that regard.

A plug: see item 2.3 in
http://atarasevich.blogspot.com/2008...liance_07.html

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #7

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

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
1
by: Markus Ernst | last post by:
Hi I wrote a function that "normalizes" strings for use in URLs in a UTF-8 encoded content administration application. After having removed the accents from latin characters I try to remove all...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
22
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
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:
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...

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.