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

Proposal for signed/unsigned modifier in class declarations

Hello everybody,

this is my first post to a newsgroup at all.
I would like to get some feedback on one proposal I am thinking about:

--- begin of proposal ---

Proposal to add
signed/unsigned modifier to class declarations
to next revision of C++ programming language

Abstract

This document describes proposed syntax that enable signed/unsigned
modifiers on base classes in order to allow signed/unsigned versions
of the same class with only minor behavioral changes. This will allow
much simpler implementation of advanced numeric classes that are about
to be accepted for TR1 (and TR2).

Semantics

The class can be declared either to be signed, to be unsigned or
none. When neither signed or unsigned is specified an signed/unsigned
specifier cannot be used to create an object.
When either signed nor unsigned modifier is used to create an object,
the default semantics is used (signed/unsigned specified in class
declaration).

Member functions can be specified to be either signed or unsigned or
none. This overloads the member function specificaly for signed or
unsigned version of the class. When none of these modifiers is used,
the method is available for both signed and unsigned types.
When only single member function, explicitly declared as signed or
unsigned is used, the other type cannot use this member function.
See Synatx.

When signed/unsigned specification is not used on the class declaration
the normal behavior retains. No member function can be declared
signed/unsigned and the class name cannot be used with signed/unsigned
modifier in order to create an object.

The signed/unsigned flexibility is not derived. Using modifier other
than default yields in different type.

Syntax

The class declaration is extended of _ability_ to specify it either
as signed or unsigned:

class C signed { ... };
or
class C unsigned { ... };

The object's methods then can be overloaded for signed or unsigned
version:

class C signed {
void M () signed;
void M () unsigned;
};
class C signed {
void M (); // the same as if "signed" was used here
void M () unsigned;
};
class C signed {
void M ();
void M () signed; // error: signed version already declared
};
class C signed {
void M (); // one version for both signed and unsigned
};
class C signed {
void M () unsigned; // unable to call M from signed type
};
class C signed {
void M () signed; // unable to call M from unsigned type
};

Object creation:

class C signed { ... };
C c; // the same type as "signed C c"
signed C c; // the same type as "C c"
unsigned C c; // has different type than "signed C c"

class C unsigned { ... };
C c; // the same type as "unsigned C c"
signed C c; // has different type than "unsigned C c"
unsigned C c; // the same type as "C c"

class C { ... };
C c;
signed C c; // syntax error
unsigned C c; // syntax error

Problems

None known. No problems with current conforming code is seen because
"signed" and "unsigned" are not allowed at proposed places.

--- end of proposal ---

I know that the terminology is not correct and that it would need a proper
wording, but this is just for initial consideration.
So what do you think? Is it worth trying to submit?

---
Jan Ringo¹, Tr****@MX-3.cz
http://Tringi.MX-3.cz
Feb 3 '07 #1
10 3268
Hi,
you could declare
class unsigned_C {...};
class signed_C {...};

What's the difference to your approach ?
Does your approach have any advantages to this ?

Regards
Thorsten

Jan Ringoš wrote:
Hello everybody,

this is my first post to a newsgroup at all.
I would like to get some feedback on one proposal I am thinking about:

--- begin of proposal ---

Proposal to add
signed/unsigned modifier to class declarations
to next revision of C++ programming language

Abstract

This document describes proposed syntax that enable signed/unsigned
modifiers on base classes in order to allow signed/unsigned versions
of the same class with only minor behavioral changes. This will allow
much simpler implementation of advanced numeric classes that are about
to be accepted for TR1 (and TR2).

Semantics

The class can be declared either to be signed, to be unsigned or
none. When neither signed or unsigned is specified an signed/unsigned
specifier cannot be used to create an object.
When either signed nor unsigned modifier is used to create an object,
the default semantics is used (signed/unsigned specified in class
declaration).

Member functions can be specified to be either signed or unsigned or
none. This overloads the member function specificaly for signed or
unsigned version of the class. When none of these modifiers is used,
the method is available for both signed and unsigned types.
When only single member function, explicitly declared as signed or
unsigned is used, the other type cannot use this member function.
See Synatx.

When signed/unsigned specification is not used on the class
declaration the normal behavior retains. No member function can be
declared signed/unsigned and the class name cannot be used with
signed/unsigned modifier in order to create an object.

The signed/unsigned flexibility is not derived. Using modifier other
than default yields in different type.

Syntax

The class declaration is extended of _ability_ to specify it either
as signed or unsigned:

class C signed { ... };
or
class C unsigned { ... };

The object's methods then can be overloaded for signed or unsigned
version:

class C signed {
void M () signed;
void M () unsigned;
};
class C signed {
void M (); // the same as if "signed" was used here
void M () unsigned;
};
class C signed {
void M ();
void M () signed; // error: signed version already declared
};
class C signed {
void M (); // one version for both signed and unsigned
};
class C signed {
void M () unsigned; // unable to call M from signed type
};
class C signed {
void M () signed; // unable to call M from unsigned type
};

Object creation:

class C signed { ... };
C c; // the same type as "signed C c"
signed C c; // the same type as "C c"
unsigned C c; // has different type than "signed C c"

class C unsigned { ... };
C c; // the same type as "unsigned C c"
signed C c; // has different type than "unsigned C c"
unsigned C c; // the same type as "C c"

class C { ... };
C c;
signed C c; // syntax error
unsigned C c; // syntax error

Problems

None known. No problems with current conforming code is seen because
"signed" and "unsigned" are not allowed at proposed places.

--- end of proposal ---

I know that the terminology is not correct and that it would need a proper
wording, but this is just for initial consideration.
So what do you think? Is it worth trying to submit?

---
Jan Ringoš, Tr****@MX-3.cz
http://Tringi.MX-3.cz
Feb 3 '07 #2
First of all, less code duplication. You would write most of the operators
only once and overloaded those that needs to be different. For example,
unsigned division can be a little simpler than signed division. The
less-than operator is also simpler for unsigned types.

I have seen some proposals for TR1 numeric types where signed version is
derived from unsigned version (not sure what it was, I cannot find it
now). This is one alternative but is it really right way to do that?

After all, it is also syntactic sugar that would make use of
library-defined numeric types easier and more intuitive (especially for
beginners).

---
Jan Ringo¹, Tr****@MX-3.cz
http://Tringi.MX-3.cz

---
Dne Sat, 03 Feb 2007 23:46:32 +0100 Thorsten Kiefer
<to*****@usenet.cnntp.orgnapsal/-a:
Hi,
you could declare
class unsigned_C {...};
class signed_C {...};

What's the difference to your approach ?
Does your approach have any advantages to this ?

Regards
Thorsten

Jan Ringo¹ wrote:
>Hello everybody,

this is my first post to a newsgroup at all.
I would like to get some feedback on one proposal I am thinking about:

--- begin of proposal ---

Proposal to add
signed/unsigned modifier to class declarations
to next revision of C++ programming language

Abstract

This document describes proposed syntax that enable signed/unsigned
modifiers on base classes in order to allow signed/unsigned versions
of the same class with only minor behavioral changes. This will
allow
much simpler implementation of advanced numeric classes that are
about
to be accepted for TR1 (and TR2).

Semantics

The class can be declared either to be signed, to be unsigned or
none. When neither signed or unsigned is specified an
signed/unsigned
specifier cannot be used to create an object.
When either signed nor unsigned modifier is used to create an
object,
the default semantics is used (signed/unsigned specified in class
declaration).

Member functions can be specified to be either signed or unsigned or
none. This overloads the member function specificaly for signed or
unsigned version of the class. When none of these modifiers is used,
the method is available for both signed and unsigned types.
When only single member function, explicitly declared as signed or
unsigned is used, the other type cannot use this member function.
See Synatx.

When signed/unsigned specification is not used on the class
declaration the normal behavior retains. No member function can be
declared signed/unsigned and the class name cannot be used with
signed/unsigned modifier in order to create an object.

The signed/unsigned flexibility is not derived. Using modifier other
than default yields in different type.

Syntax

The class declaration is extended of _ability_ to specify it either
as signed or unsigned:

class C signed { ... };
or
class C unsigned { ... };

The object's methods then can be overloaded for signed or unsigned
version:

class C signed {
void M () signed;
void M () unsigned;
};
class C signed {
void M (); // the same as if "signed" was used here
void M () unsigned;
};
class C signed {
void M ();
void M () signed; // error: signed version already declared
};
class C signed {
void M (); // one version for both signed and unsigned
};
class C signed {
void M () unsigned; // unable to call M from signed type
};
class C signed {
void M () signed; // unable to call M from unsigned type
};

Object creation:

class C signed { ... };
C c; // the same type as "signed C c"
signed C c; // the same type as "C c"
unsigned C c; // has different type than "signed C c"

class C unsigned { ... };
C c; // the same type as "unsigned C c"
signed C c; // has different type than "unsigned C c"
unsigned C c; // the same type as "C c"

class C { ... };
C c;
signed C c; // syntax error
unsigned C c; // syntax error

Problems

None known. No problems with current conforming code is seen because
"signed" and "unsigned" are not allowed at proposed places.

--- end of proposal ---

I know that the terminology is not correct and that it would need a
proper
wording, but this is just for initial consideration.
So what do you think? Is it worth trying to submit?

---
Jan Ringo¹, Tr****@MX-3.cz
http://Tringi.MX-3.cz
Feb 3 '07 #3
Jan Ringoš wrote:
First of all, less code duplication. You would write most of the operators
only once and overloaded those that needs to be different. For example,
unsigned division can be a little simpler than signed division. The
less-than operator is also simpler for unsigned types.
I doubt that you can reuse unsigned-code in the signed-class.
Example :
class Mathtools signed {
static signed int add(signed int a,signed int b){
return primitive_add(a,b);
}
};
To reuse this you would write:
class Mathtools unsigned {
static unsigned int add(unsigned int a,unsigned int b){
return (unsigned)add((signed)a,(signed)b);
}
};
/* presuming that the signed add is available in the unsigned version */

An idea would be to make the compile generate the second class definition as default,
where you can override/overload the default behaviour.
It this what you mean ?
If not, please give a better example.

>
I have seen some proposals for TR1 numeric types where signed version is
derived from unsigned version (not sure what it was, I cannot find it
now). This is one alternative but is it really right way to do that?
In that case the derived class contains both signed and unsigned versions.
I think that's not what you want.
>
After all, it is also syntactic sugar that would make use of
library-defined numeric types easier and more intuitive (especially for
beginners).
How about using templates ?

template<class X>
class Mathtools {
static X add(X a,X b){
return a + b;
}
};

To get the signed version :
Mathtools<signed int>::add(1,2);
To get the unsigned version :
Mathtools<unsigned int>::add(1,2);

And you can still specialize the template, if you need to :
template<float>
class Mathtools {
static X add(X a,X b){
return my_fancy_float_adder(a,b);
}
};

Please forgive me syntactic errors, as we are talking about principles, syntax is unimportant at the moment.

---
Jan Ringoš, Tr****@MX-3.cz
http://Tringi.MX-3.cz
Feb 3 '07 #4
Dne Sun, 04 Feb 2007 00:37:02 +0100 Thorsten Kiefer
<to*****@usenet.cnntp.orgnapsal/-a:
Jan Ringo¹ wrote:
>First of all, less code duplication. You would write most of the
operators
only once and overloaded those that needs to be different. For example,
unsigned division can be a little simpler than signed division. The
less-than operator is also simpler for unsigned types.
I doubt that you can reuse unsigned-code in the signed-class.
Example :
class Mathtools signed {
static signed int add(signed int a,signed int b){
return primitive_add(a,b);
}
};
To reuse this you would write:
class Mathtools unsigned {
static unsigned int add(unsigned int a,unsigned int b){
return (unsigned)add((signed)a,(signed)b);
}
};
/* presuming that the signed add is available in the unsigned
version */

An idea would be to make the compile generate the second class
definition as default,
where you can override/overload the default behaviour.
It this what you mean ?
If not, please give a better example.
Yes, the idea is to make the compiler to generate as much code as
possible, but not exactly in the way you wrote the Mathtools example. I'll
provide mine example:

class int128 signed {
...
int128 & operator += (const int128 & b) throw () {
unsigned __int64 old_lo (this->lo);

this->lo += b.lo;
this->hi += b.hi + (this->lo < old_lo);

return *this;
};
int128 & operator /= (const int128 & b) signed throw () {
... // b's type is actually const signed int128 &
};
int128 & operator /= (const int128 & b) unsigned throw () {
... // b's type is actually const unsigned int128 &
};
...
};
int128 operator + (const int128 & a, const int128 & b) throw () {
return int128 (a) += b; };
int128 operator / (const int128 & a, const int128 & b) throw () {
return int128 (a) /= b; };

Here, although the operator+= is generated once, the compiler would
probably generate both operator+ and operator/ twice, once for singed
int128 and once for unsigned int128. I would expect the optimizer to
collapse both operators+ into one function when the function body is the
same (but I am thinking too far forward here).

And there you go and write:

unsigned int128 a;
unsigned int128 b;
...
unsigned int128 c = a + b;
unsigned int128 d = a / b;

The compiler would use the unsigned version of operator/ that use unsigned
version of operator/=.

Maybe you can see more problems with those operators. Although it is/are
actually two functions, the promotion to higher-rank type, generally the
unsigned one, should be done by implicit constructor, like this:

class int128 signed {
...
int128 (const signed int128 &) unsigned throw ()
: hi (...), lo (...) { ... };
...
};

unsigned int128 a;
signed int128 b;
...
usnigned int128 c = a + b;

The b is silently (using the implicit constructor) converted to unsigned
int128.

By the way, I am not really sure with the specialized constructor syntax..
It could also be one of these, but the above is more consistent with
member function declarations:
int128 unsigned (const signed int128 &) { ... };
unsigned int128 (const signed int128 &) { ... };

>
>>
I have seen some proposals for TR1 numeric types where signed versionis
derived from unsigned version (not sure what it was, I cannot find it
now). This is one alternative but is it really right way to do that?
In that case the derived class contains both signed and unsigned
versions.
I think that's not what you want.
That is correct.
>
>>
After all, it is also syntactic sugar that would make use of
library-defined numeric types easier and more intuitive (especially for
beginners).
How about using templates ?

template<class X>
class Mathtools {
static X add(X a,X b){
return a + b;
}
};

To get the signed version :
Mathtools<signed int>::add(1,2);
To get the unsigned version :
Mathtools<unsigned int>::add(1,2);

And you can still specialize the template, if you need to :
template<float>
class Mathtools {
static X add(X a,X b){
return my_fancy_float_adder(a,b);
}
};

Please forgive me syntactic errors, as we are talking about principles,
syntax is unimportant at the moment.
Of course, but I don't see any point in this code. Since I presented the
example with int128, you probably see what I mean now.

--
Jan Ringo¹, Tr****@MX-3.cz
http://Tringi.MX-3.cz
Feb 4 '07 #5
Jan Ringo¨ <Tr****@mx-3.czwrote:
I would like to get some feedback on one proposal I am thinking about:

--- begin of proposal ---

Proposal to add
signed/unsigned modifier to class declarations
to next revision of C++ programming language
Sorry, I have nothing useful to add to this discussion, but you may get
more feedback if you posted this to comp.std.c++.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 5 '07 #6
Dne Mon, 05 Feb 2007 19:37:15 +0100 Marcus Kwok
<ri******@gehennom.invalidnapsal/-a:
Jan Ringo¹ <Tr****@mx-3.czwrote:
>I would like to get some feedback on one proposal I am thinking about:

--- begin of proposal ---

Proposal to add
signed/unsigned modifier to class declarations
to next revision of C++ programming language

Sorry, I have nothing useful to add to this discussion, but you may get
more feedback if you posted this to comp.std.c++.
Will do. First I have to extend the proposal of what came up from my
discussion with Thorsten Kiefer.

--
Jan Ringo¹, Tr****@MX-3.cz
http://Tringi.MX-3.cz
Feb 5 '07 #7
Jan Ringo¨ wrote:
this is my first post to a newsgroup at all.
I would like to get some feedback on one proposal I am thinking about:
(...)
Member functions can be specified to be either signed or unsigned or
none. This overloads the member function specificaly for signed or
unsigned version of the class. When none of these modifiers is used,
the method is available for both signed and unsigned types.
When only single member function, explicitly declared as signed or
unsigned is used, the other type cannot use this member function.
I don't see the need for new syntax to allow that semantic:

class MyNumber { .... };

class MyNumberSigned : public MyNumber { ... };

class MyNumberUnsigned : public MyNumber { .... };

--
Salu2
Feb 5 '07 #8
Dne Mon, 05 Feb 2007 21:03:08 +0100 Julián Albo <JU********@terra.es>
napsal/-a:
Jan Ringo¹ wrote:
>this is my first post to a newsgroup at all.
I would like to get some feedback on one proposal I am thinking about:
(...)
> Member functions can be specified to be either signed or unsigned or
none. This overloads the member function specificaly for signed or
unsigned version of the class. When none of these modifiers is used,
the method is available for both signed and unsigned types.
When only single member function, explicitly declared as signed or
unsigned is used, the other type cannot use this member function.

I don't see the need for new syntax to allow that semantic:

class MyNumber { .... };

class MyNumberSigned : public MyNumber { ... };

class MyNumberUnsigned : public MyNumber { .... };
Hi Julián,

yes, my proposal is oriented mostly towards extending the syntax so you
could implement your own numeric types that would be used the same way as
built-in types can be. But your example has a lot of disadvantages like
polymorphism overhead, not mentioning that you cannot have virtual
operators.

--
Jan Ringo¹, Tr****@MX-3.cz
http://Tringi.MX-3.cz
Feb 5 '07 #9
Jan Ringo¨ wrote:
>>this is my first post to a newsgroup at all.
I would like to get some feedback on one proposal I am thinking about:
(...)
>> Member functions can be specified to be either signed or unsigned or
none. This overloads the member function specificaly for signed or
unsigned version of the class. When none of these modifiers is used,
the method is available for both signed and unsigned types.
When only single member function, explicitly declared as signed or
unsigned is used, the other type cannot use this member function.

I don't see the need for new syntax to allow that semantic:

class MyNumber { .... };

class MyNumberSigned : public MyNumber { ... };

class MyNumberUnsigned : public MyNumber { .... };

yes, my proposal is oriented mostly towards extending the syntax so you
could implement your own numeric types that would be used the same way as
built-in types can be. But your example has a lot of disadvantages like
polymorphism overhead, not mentioning that you cannot have virtual
operators.
You will need a much more detailed argumentation and data supporting it if
you want that your proposal will be accepted for discussion.

--
Salu2
Feb 5 '07 #10
Dne Mon, 05 Feb 2007 23:08:06 +0100 Julián Albo <JU********@terra.es>
napsal/-a:
Jan Ringo¹ wrote:
>>>this is my first post to a newsgroup at all.
I would like to get some feedback on one proposal I am thinking about:

You will need a much more detailed argumentation and data supporting it
if
you want that your proposal will be accepted for discussion.
I am aware of that. I posted here just to see if it won't be immediately
rejected as nonsense. Now I am preparing more detailed proposal document
to send to comp.std.c++ but this will take some time ;-)

--
Jan Ringo¹, Tr****@MX-3.cz
http://Tringi.MX-3.cz
Feb 6 '07 #11

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

Similar topics

2
by: Dan Disney | last post by:
I have read conflicting information on the access scope of VB.Net class declarations when no access modifier is specified. MSDN library documentation states "Classes that do not specify an access...
19
by: Christopher Benson-Manica | last post by:
Given signed char str_a="Hello, world!\n"; unsigned char str_b="Hello, world!\n"; what is the difference, if any, between the following two statements? printf( "%s", str_a ); printf( "%s",...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
3
by: Mr Newbie | last post by:
I am messing around with Web User Controls at present and (think) I have discovered the following. 1.) The identifier for the control in the code behind must match the ID for the control on the...
9
by: corey.coughlin | last post by:
Alright, so I've been following some of the arguments about enhancing parallelism in python, and I've kind of been struck by how hard things still are. It seems like what we really need is a more...
8
by: Marcin Kalicinski | last post by:
Are 3 types: signed char, char and unsigned char distinct? My compiler is treating char as signed char (i.e. it has sign, and range from -128 to 127), but the following code does not call f<char>...
6
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but...
39
by: Juha Nieminen | last post by:
I was once taught that if some integral value can never have negative values, it's a good style to use an 'unsigned' type for that: It's informative, self-documenting, and you are not wasting half...
1
by: SchoolOfLife | last post by:
Hello, When I try compiling this code: signed float fl1=-10e-2; with variable declarations of type signed float and unsigned float, gcc 3.3.5 outputs: error: short, signed or unsigned invalid...
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:
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.