Topic: void variables
Author: mynick@hotmail.com ("wpcmame")
Date: Thu, 29 May 2003 15:55:08 +0000 (UTC) Raw View
Has there been any discussion on making void a little less incomplete in
C++?
Searched google but didn't see anything like the following:
void x, y;
void func(void a, int b, void c);
struct { int u; void z; } s;
The void variables would have the following "properties":
- Takes no space
sizeof(x) == 0
- Only operation allowed is assignment which is a no-op
x = y = s.z; // ok
x = func(y, 1, y); // ok
x = *(&y); // illegal &
- implicit cast where explicit cast is legal
x = 3; // ok
- void function arguments are optional
x = func(1); // ok
x = func(1,y); // ok
Ofcourse this has little use except in templates. Consider the following
example:
//----------------------
template <typename TReturn, typename TArg1, typename TArg2>
class cThread {
public:
typedef TReturn (*tWorkFunc)(TArg1, TArg2);
cThread(tWorkFunc aWorkFunc) : mWorkFunc(aWorkFunc) {}
void startThread(TArg1 aArg1, TArg2 aArg2) {
mArg1 = aArg1; mArg2 = aArg2;
FunctionThatStartsAThread(threadRun, this);
}
volatile TReturn retVal() const { return mRetVal; } // should check
running()
volatile bool running() const { return mRunning; }
private:
static void threadRun(cThread* me) {
me->mRunning = true;
me->mRetVal = me->mWorkFunc(me->mArg1, me->mArg2);
me->mRunning = false;
}
const tWorkFunc mWorkFunc;
volatile TReturn mRetVal;
TArg1 mArg1;
TArg2 mArg2;
volatile bool mRunning;
};
//------------------
With void variables the same template can be used with a many different
functions.
//--------
int func1(int x);
cThread<int, int, void> thread1(func1);
thread1.startThread(5);
....
cout << thread1.retVal();
//----------
void func2(int x, int y);
cThread<void, int, int> thread2(func2);
thread2.startThread(5,6);
//-----------
void func3();
cThread<void, void, void> thread3(func3);
thread3.startThread();
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: ron@sensor.com ("Ron Natalie")
Date: Thu, 29 May 2003 17:09:29 +0000 (UTC) Raw View
""wpcmame"" <mynick@hotmail.com> wrote in message news:D5mBa.12145$dP1.23487@newsc.telia.net...
>
> The void variables would have the following "properties":
> - Takes no space
> sizeof(x) == 0
Gak, then
void foo[10];
has a lot of problems.
>- void function arguments are optional
> x = func(1); // ok
> = func(1,y); // ok
You need another change: that passing voids to functions NOT expecting them
is harmless (your example relies on this).
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: rmaddox@isicns.com (Randy Maddox)
Date: Fri, 30 May 2003 18:15:36 +0000 (UTC) Raw View
ron@sensor.com ("Ron Natalie") wrote in message news:<li2dnYyXJNyTpEujXTWQlg@giganews.com>...
> ""wpcmame"" <mynick@hotmail.com> wrote in message news:D5mBa.12145$dP1.23487@newsc.telia.net...
>
> >
> > The void variables would have the following "properties":
> > - Takes no space
> > sizeof(x) == 0
>
> Gak, then
> void foo[10];
> has a lot of problems.
>
> >- void function arguments are optional
> > x = func(1); // ok
> > = func(1,y); // ok
>
>
> You need another change: that passing voids to functions NOT expecting them
> is harmless (your example relies on this).
>
>
He also needs another another change: 3.9/6 explicitly states that
the void types are incomplete types => no objects of void type are
allowed.
Randy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: andys@despammed.com (Andy Sawyer)
Date: Fri, 30 May 2003 18:15:45 +0000 (UTC) Raw View
In article <li2dnYyXJNyTpEujXTWQlg@giganews.com>,
on Thu, 29 May 2003 17:09:29 +0000 (UTC),
ron@sensor.com ("Ron Natalie") wrote:
> Gak, then
> void foo[10];
> has a lot of problems.
Whilst I have no interest in "completing" void as a type, in the past
I've thought it might be handy to be able to declare "arrays of void",
as "typeless" buffers which give alignment guarantees, e.g.:
void foo[10];
This would be big enough to construct an object whose size <= 10, and
(here's the important bit) would be guaranteed to be correctly aligned
to do so. Currently, the only way to aquire such a buffer is to
allocate it dynamically, and I sometimes feel that it might be nice to
be able to declare an automatic buffer to deal with this kind of
thing.
e.g.:
void foo( const widget& w )
{
void buffer[sizeof(widget)]; // correctly aligned for
// construction of any type
widget *p = new (buffer) widget(w);
// do stuff with *p
}
Regards,
Andy S.
--
"Light thinks it travels faster than anything but it is wrong. No matter
how fast light travels it finds the darkness has always got there first,
and is waiting for it." -- Terry Pratchett, Reaper Man
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: ron@sensor.com ("Ron Natalie")
Date: Fri, 30 May 2003 20:00:58 +0000 (UTC) Raw View
"Randy Maddox" <rmaddox@isicns.com> wrote in message news:8c8b368d.0305300605.5e3a9336@posting.google.com...
> ron@sensor.com ("Ron Natalie") wrote in message news:<li2dnYyXJNyTpEujXTWQlg@giganews.com>...
>
> He also needs another another change: 3.9/6 explicitly states that
> the void types are incomplete types => no objects of void type are
> allowed.
That was the first thing he asked for (to allow them to be complete, see the
original post).
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]