Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Any use for the comma operator? (http://www.velocityreviews.com/forums/t950913-any-use-for-the-comma-operator.html)

Rui Maciel 08-16-2012 04:20 PM

Any use for the comma operator?
 
Did anyone ever used the comma operator in some place other than a loop
condition?


Thanks in advance,
Rui Maciel

Victor Bazarov 08-16-2012 05:36 PM

Re: Any use for the comma operator?
 
On 8/16/2012 12:20 PM, Rui Maciel wrote:
> Did anyone ever used the comma operator in some place other than a loop
> condition?


Absolutely. If you can find a reference on the 'net to "list assignment
trick" or "technique", you can implement your own list class in such a
way that would allow writing something like

MyList<int> mylist;
...
mylist = 1, 2, 4;

for instance. I honestly have no opinion on convenience of such code.
But it looks like fun.

V
--
I do not respond to top-posted replies, please don't ask

Kaba 08-16-2012 05:37 PM

Re: Any use for the comma operator?
 
16.8.2012 19:20, Rui Maciel kirjoitti:
> Did anyone ever used the comma operator in some place other than a loop
> condition?


Consider a class A which is a container of some kind. Then with suitable
definitions it could be filled with the syntax

A a;
a += 1, 2, 3, 4;

I did that for my matrix class. However, with C++11 and initializer
lists such uses should decline.

--
http://kaba.hilvi.org

Luca Risolia 08-16-2012 05:45 PM

Re: Any use for the comma operator?
 
On 16/08/2012 18:20, Rui Maciel wrote:
> Did anyone ever used the comma operator in some place other than a loop
> condition?


Sometimes I used it to dynamically initialize some static data before
main(), for example:

// .h
struct S {
static void compute() { // non-const expr
// ...
}
static int x[], y[];
static bool init;
};

// .cpp
int S::x[100], y[100];
bool S::init = (S::compute(), true);


Rui Maciel 08-16-2012 07:21 PM

Re: Any use for the comma operator?
 
Victor Bazarov wrote:

> On 8/16/2012 12:20 PM, Rui Maciel wrote:
>> Did anyone ever used the comma operator in some place other than a loop
>> condition?

>
> Absolutely. If you can find a reference on the 'net to "list assignment
> trick" or "technique", you can implement your own list class in such a
> way that would allow writing something like
>
> MyList<int> mylist;
> ...
> mylist = 1, 2, 4;
>
> for instance. I honestly have no opinion on convenience of such code.
> But it looks like fun.


Thanks for mentioning that trick. Meanwhile, while googling for "list
assignment trick" I've stumbled on Boost's assignment library, which might
be of some interest:

http://www.boost.org/doc/libs/1_47_0...doc/index.html


With C++11's initializer lists this trick might go the way of the dodo, but
it is still a neat trick, though.


Rui Maciel

Kaba 08-16-2012 07:29 PM

Re: Any use for the comma operator?
 
16.8.2012 22:21, Rui Maciel kirjoitti:
> With C++11's initializer lists this trick might go the way of the dodo, but
> it is still a neat trick, though.


Well, yes:) More generally, though, such tricks reflect a language's
weakness in its abstraction capability. Fortunately that's fixed. Let's
hope to get polymorphic lambdas and concepts in the next iteration:)

--
http://kaba.hilvi.org

Rui Maciel 08-16-2012 07:59 PM

Re: Any use for the comma operator?
 
Luca Risolia wrote:

> Sometimes I used it to dynamically initialize some static data before
> main(), for example:
>
> // .h
> struct S {
> static void compute() { // non-const expr
> // ...
> }
> static int x[], y[];
> static bool init;
> };
>
> // .cpp
> int S::x[100], y[100];
> bool S::init = (S::compute(), true);


Nice one. It also looks like a nifty way to add some semantics to an
assignment.


Rui Maciel

Victor Bazarov 08-16-2012 09:33 PM

Re: Any use for the comma operator?
 
On 8/16/2012 1:45 PM, Luca Risolia wrote:
> On 16/08/2012 18:20, Rui Maciel wrote:
>> Did anyone ever used the comma operator in some place other than a loop
>> condition?

>
> Sometimes I used it to dynamically initialize some static data before
> main(), for example:
>
> // .h
> struct S {
> static void compute() { // non-const expr
> // ...
> }
> static int x[], y[];
> static bool init;
> };
>
> // .cpp
> int S::x[100], y[100];
> bool S::init = (S::compute(), true);


Seems that if you just make your 'compute' return a bool, then there is
no need in the comma:

bool S::init = S::compute();

V
--
I do not respond to top-posted replies, please don't ask

88888 Dihedral 08-16-2012 11:26 PM

Re: Any use for the comma operator?
 
Scott Lurndal於 2012年8月17日星期五UTC+8上午1時10分50秒 寫道:
> Rui Maciel <rui.maciel@gmail.com> writes:
>
> >Did anyone ever used the comma operator in some place other than a loop

>
> >condition?

>
>
>
> Bjarne Stroustrup used it extensively in the C-code generated by cfront.
>
>
>
> http://en.wikipedia.org/wiki/Cfront
>
>
>
> Very extensively, to the point that it exposed various bugs in PCC on certain
>
> architectures. In a box somewhere, I still have a diagram of an expression
>
> tree generated by PCC when compiling cfront-generated code. PCC would run out
>
> of temporary registers (on a motorola 88100) trying to generate code for the
>
> expression (I used sethi-ullman numbers to fix this).
>
>
>
> http://en.wikipedia.org/wiki/Portable_C_Compiler
>
>
>
> I still use it to group related items.
>
>
>
> example:
>
>
>
> len = snprintf(bp, remaining, "fmt", ...);
>
> bp += len, remaining -= len;
>
>
>
> scott


I'll vote for the old style main(int argc, void** argv) for
varried number of arguments.

But the callee has to know the rules to get the
varied number of terms from the caller.

Luca Risolia 08-16-2012 11:41 PM

Re: Any use for the comma operator?
 
On 16/08/2012 23:33, Victor Bazarov wrote:
> On 8/16/2012 1:45 PM, Luca Risolia wrote:
>> On 16/08/2012 18:20, Rui Maciel wrote:
>>> Did anyone ever used the comma operator in some place other than a loop
>>> condition?

>>
>> Sometimes I used it to dynamically initialize some static data before
>> main(), for example:
>>
>> // .h
>> struct S {
>> static void compute() { // non-const expr
>> // ...
>> }
>> static int x[], y[];
>> static bool init;
>> };
>>
>> // .cpp
>> int S::x[100], y[100];
>> bool S::init = (S::compute(), true);

>
> Seems that if you just make your 'compute' return a bool, then there is
> no need in the comma:


Of course, if you can and want to change the return type.



All times are GMT. The time now is 05:54 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.