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

new and operator ++

Hi,

I have a type:

public struct Value
{
public static Value operator ++(Value v)
{
return new Value();
}

public static Value New()
{
return new Value();
}
}

This code

Value v1 = Value.New()++;

compiles successfully, but i get InvalidProgramException.

The same for

Value v = new Value()++;

Changing from struct to class doesn't help.

Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42
Win XP SP2.

Jun 18 '07 #1
11 1968
This is pretty interesting, as I get the same thing (and one would
assume that the C# compiler wouldn't produce invalid code).

I would submit this to Microsoft Connect as a bug.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Kerneltrap" <Sh******@gmail.comwrote in message
news:11*********************@q69g2000hsb.googlegro ups.com...
Hi,

I have a type:

public struct Value
{
public static Value operator ++(Value v)
{
return new Value();
}

public static Value New()
{
return new Value();
}
}

This code

Value v1 = Value.New()++;

compiles successfully, but i get InvalidProgramException.

The same for

Value v = new Value()++;

Changing from struct to class doesn't help.

Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42
Win XP SP2.

Jun 18 '07 #2
Kerneltrap wrote:
Hi,

I have a type:

public struct Value
{
public static Value operator ++(Value v)
{
return new Value();
}

public static Value New()
{
return new Value();
}
}

This code

Value v1 = Value.New()++;

compiles successfully, but i get InvalidProgramException.

The same for

Value v = new Value()++;

Changing from struct to class doesn't help.

Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42
Win XP SP2.
Hi

JOOI, the Mono C# Compiler yields a compilation error:

"error CS0131: The left-hand side of an assignment must be a variable, a
property or an indexer"

Interesting.

--
Tom Spink
University of Edinburgh
Jun 18 '07 #3
"Tom Spink" <ts****@gmail.comwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
Kerneltrap wrote:
>Hi,

I have a type:

public struct Value
{
public static Value operator ++(Value v)
{
return new Value();
}

public static Value New()
{
return new Value();
}
}

This code

Value v1 = Value.New()++;

compiles successfully, but i get InvalidProgramException.

The same for

Value v = new Value()++;

Changing from struct to class doesn't help.

Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42
Win XP SP2.

Hi

JOOI, the Mono C# Compiler yields a compilation error:

"error CS0131: The left-hand side of an assignment must be a variable, a
property or an indexer"

Interesting.

--
Tom Spink
University of Edinburgh


This is actually what the C# compiler should do as well, this is a compiler
error that is corrected in Orcas version of the compiler.

Willy.

Jun 18 '07 #4
On Mon, 18 Jun 2007 16:42:45 -0700, Willy Denoyette [MVP]
<wi*************@telenet.bewrote:
"Tom Spink" <ts****@gmail.comwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
>Kerneltrap wrote:
>>[...]
This code

Value v1 = Value.New()++;

compiles successfully, but i get InvalidProgramException.

The same for

Value v = new Value()++;

JOOI, the Mono C# Compiler yields a compilation error:

"error CS0131: The left-hand side of an assignment must be a variable, a
property or an indexer"

This is actually what the C# compiler should do as well, this is a
compiler error that is corrected in Orcas version of the compiler.
Okay, I'll bite.

Why is "Value v1" and "Value v" not considered a "variable, a property or
an indexer"? Specifically, sure looks like a variable to me. What am I
missing?

Pete
Jun 18 '07 #5

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 18 Jun 2007 16:42:45 -0700, Willy Denoyette [MVP]
<wi*************@telenet.bewrote:
"Tom Spink" <ts****@gmail.comwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
>Kerneltrap wrote:
>>[...]
This code

Value v1 = Value.New()++;

compiles successfully, but i get InvalidProgramException.

The same for

Value v = new Value()++;

JOOI, the Mono C# Compiler yields a compilation error:

"error CS0131: The left-hand side of an assignment must be a variable, a
property or an indexer"

This is actually what the C# compiler should do as well, this is a
compiler error that is corrected in Orcas version of the compiler.
Okay, I'll bite.

Why is "Value v1" and "Value v" not considered a "variable, a property or
an indexer"? Specifically, sure looks like a variable to me. What am I
missing?

Pete
You're missing operator binding: The code reads as:

Value v = ((new Value())++);

The postincrement operator is buried within the right-hand side of the
assignment.

Does

Value v; (v = new Value())++;

work?

Jun 19 '07 #6
On Jun 19, 4:25 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
"Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote in message

news:op***************@petes-computer.local...
On Mon, 18 Jun 2007 16:42:45 -0700, Willy Denoyette [MVP]

<willy.denoye...@telenet.bewrote:
"Tom Spink" <tsp...@gmail.comwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
Kerneltrap wrote:
>[...]
This code
>Value v1 = Value.New()++;
>compiles successfully, but i get InvalidProgramException.
>The same for
>Value v = new Value()++;
JOOI, the Mono C# Compiler yields a compilation error:
"error CS0131: The left-hand side of an assignment must be a variable, a
property or an indexer"
This is actually what the C# compiler should do as well, this is a
compiler error that is corrected in Orcas version of the compiler.

Okay, I'll bite.

Why is "Value v1" and "Value v" not considered a "variable, a property or
an indexer"? Specifically, sure looks like a variable to me. What am I
missing?

Pete

You're missing operator binding: The code reads as:

Value v = ((new Value())++);

The postincrement operator is buried within the right-hand side of the
assignment.

Does

Value v; (v = new Value())++;

work?
It doesn't. Same effect.

Jun 19 '07 #7
"Peter Duniho" <Np*********@nnowslpianmk.comschrieb im Newsbeitrag
news:op***************@petes-computer.local...
>>>
Value v1 = Value.New()++;

compiles successfully, but i get InvalidProgramException.
JOOI, the Mono C# Compiler yields a compilation error:

"error CS0131: The left-hand side of an assignment must be a variable, a
property or an indexer"
Okay, I'll bite.
Why is "Value v1" and "Value v" not considered a "variable, a property or
an indexer"? Specifically, sure looks like a variable to me. What am I
missing?
What you're missing is, that exp++ contains an assignment to exp. So this
should be "a varialbe, a property or an indexer".

What actually do you wan't to do?

Christof
Jun 19 '07 #8
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 18 Jun 2007 16:42:45 -0700, Willy Denoyette [MVP]
<wi*************@telenet.bewrote:
"Tom Spink" <ts****@gmail.comwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
>Kerneltrap wrote:
>>[...]
This code

Value v1 = Value.New()++;

compiles successfully, but i get InvalidProgramException.

The same for

Value v = new Value()++;

JOOI, the Mono C# Compiler yields a compilation error:

"error CS0131: The left-hand side of an assignment must be a variable, a
property or an indexer"

This is actually what the C# compiler should do as well, this is a
compiler error that is corrected in Orcas version of the compiler.
Okay, I'll bite.

Why is "Value v1" and "Value v" not considered a "variable, a property or
an indexer"? Specifically, sure looks like a variable to me. What am I
missing?

Pete

Pete, In top of what others said, the Mono compiler's error CS0131: is
somewhat misleading.
Orcas C# compiler error message:
[error CS1059: The operand of an increment or decrement operator must be a
variable, property or indexer]
is IMO more clear and accurate.
Willy.

Jun 19 '07 #9
On Tue, 19 Jun 2007 04:11:52 -0700, Willy Denoyette [MVP]
<wi*************@telenet.bewrote:
Pete, In top of what others said, the Mono compiler's error CS0131: is
somewhat misleading.
Orcas C# compiler error message:
[error CS1059: The operand of an increment or decrement operator must be
a variable, property or indexer] is IMO more clear and accurate.
Ah, okay. Since ++ has an implicit assignment, the result has to be able
to be assigned back to the original source, which isn't possible in this
case.

Thanks everyone. Makes sense now.

Pete
Jun 19 '07 #10
Willy Denoyette [MVP] wrote:
"Tom Spink" <ts****@gmail.comwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
>Kerneltrap wrote:
>>Hi,

I have a type:

public struct Value
{
public static Value operator ++(Value v)
{
return new Value();
}

public static Value New()
{
return new Value();
}
}

This code

Value v1 = Value.New()++;

compiles successfully, but i get InvalidProgramException.

The same for

Value v = new Value()++;

Changing from struct to class doesn't help.

Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42
Win XP SP2.

Hi

JOOI, the Mono C# Compiler yields a compilation error:

"error CS0131: The left-hand side of an assignment must be a variable, a
property or an indexer"

Interesting.

--
Tom Spink
University of Edinburgh

This is actually what the C# compiler should do as well, this is a
compiler error that is corrected in Orcas version of the compiler.

Willy.
Nice. I figured as much, because of postfix increment's implicit
assignment. It's a shame it's worded a bit funny in gmcs.

--
Tom Spink
University of Edinburgh
Jun 19 '07 #11
Nicholas Paldino [.NET/C# MVP] wrote:
This is pretty interesting, as I get the same thing (and one would
assume that the C# compiler wouldn't produce invalid code).

I would submit this to Microsoft Connect as a bug.
Strange.

Decimal d = New Decimal()++;

or even

Decimal d = 0m++;

Produces just a compilation error as it should. What is the difference?
Decimal is just a struct like any other or is it treated special by the
compiler as it is with System.String?
Aug 27 '07 #12

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

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
8
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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...

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.