Newsgroups: comp.lang.c++.moderated
From: "kwikius" <a...@servocomm.freeserve.co.uk>
Date: 17 May 2006 21:39:26 -0400
Local: Thurs 18 May 2006 02:39
Subject: Array index operator in 3d vector class
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: "Victor Bazarov" <v.Abaza...@comAcast.net>
Date: 18 May 2006 17:26:23 -0400
Local: Thurs 18 May 2006 22:26
Subject: Re: Array index operator in 3d vector class
This is suspect. 'sizeof(vect)' is not necessarily 3 * sizeof(T) even
though the only three members are the three T's in it. Generally speaking, there can be padding of indeterminate size. > T * p = & x; [..] > return p[n]; > } > else{ V [ See http://www.gotw.ca/resources/clcm.htm for info about ] You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: "kwikius" <a...@servocomm.freeserve.co.uk>
Date: 20 May 2006 17:18:21 -0400
Local: Sat 20 May 2006 22:18
Subject: Re: Array index operator in 3d vector class
OK, but if 'sizeof(vect)' is not 3 * sizeof(T), then the offending
code is unreachable, so it isnt a problem AFAICS. regards [ See http://www.gotw.ca/resources/clcm.htm for info about ] You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: Antti Virtanen <virta...@tilhi.cs.tut.fi>
Date: 18 May 2006 18:39:28 -0400
Local: Thurs 18 May 2006 23:39
Subject: Re: Array index operator in 3d vector class
On 2006-05-18, kwikius <a...@servocomm.freeserve.co.uk> wrote:
> Disregrading the fact that the array index operator in the following I'm not sure, but I suspect trouble looms ahead. See below. > vect class might technically use undefined behaviour, nevertheless > there isnt a way it can fail (assuming a valid index) , or is there? I don't see why you have the sizeof-if at all since the switch-case does the trick in a safe fashion and efficiently. The compiler ought to figure out how to make the switch-case structure efficient here. Assuming you still want that sizeof-if, I'm not convinced of it's safety. AFAIK it is guaranteed that things are allocated in the order of declaration Also, is the compiler allowed to allocate extra space between the members Then there's the additional issue that someone might pass your template -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: Carl Barron <cbarron...@adelphia.net>
Date: 18 May 2006 18:39:03 -0400
Local: Thurs 18 May 2006 23:39
Subject: Re: Array index operator in 3d vector class
In article <1147911942.723852.258...@y43g2000cwc.googlegroups.com>,
[snip] why not just T &operator [](int n) { switch(n) { case 0: return x; case 1: return y; case 2: return z; default:throw std::logic_error(...); } } similiar for const version. [ See http://www.gotw.ca/resources/clcm.htm for info about ] You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: "kanze" <ka...@gabi-soft.fr>
Date: 20 May 2006 16:47:25 -0400
Local: Sat 20 May 2006 21:47
Subject: Re: Array index operator in 3d vector class
I think that's the real question. I don't think his version
will fail in any reasonable implementation, but I don't see the point of it. Your solution is far easier to read and understand, will result in a smaller program, and will probably be just as fast. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: "kwikius" <a...@servocomm.freeserve.co.uk>
Date: 23 May 2006 07:15:25 -0400
Local: Tues 23 May 2006 12:15
Subject: Re: Array index operator in 3d vector class
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: "kanze" <ka...@gabi-soft.fr>
Date: 24 May 2006 11:39:51 -0400
Local: Wed 24 May 2006 16:39
Subject: Re: Array index operator in 3d vector class
kwikius wrote: The answer to 2 is simply no. But I think you know that; what > kanze wrote: > [...] > > I think that's the real question. I don't think his version > > will fail in any reasonable implementation, but I don't see > > the point of it. Your solution is far easier to read and > > understand, will result in a smaller program, and will > > probably be just as fast. > Right. There are 2 questions. (1) Is the code correct? (2) Why > do this? you mean to ask is will it work anyway? > In this type of expression, losing one execution cycle is You mean that it will probably work on most implementations. > worth spending some time on. The class is a template and > therefore T could be anything form a bool to a long double and > beyond in size. I am making 3 assumptions: > 1) The code is (in practise) correct for various T's, > compilers and OSes. That's probable. Again, however, the question is why? If using an array is the appropriate solution, then just use an array. If not, then use the switch. > 3) That the array access version of the expression is always I'd run some benchmarks before I assumed such. About the only > at least as fast as and maybe faster than the switch > statement. required extra cost you might have in the switch is bounds checking (which is required by the standard in a switch). The array access is probably faster on most modern general purpose machines, but I'd be sceptical in other cases. Note too that as you originally wrote it, a clever compiler > Assuming those 3 assumptions hold I may gain something , but I Well, if performance really is that critical, it's probably > appear to lose nothing though after looking at Carl Barrons > version, I see the scheme may be compromised by the array > bounds checking! Carls code looks cleaner, however if the > first ( correctness) assumption is wrong it isnt worth > considering alternative expressions to the switch AFAICS. worth implementing both versions and benchmarking them on various target machines. However, I would use two versions of the data declaration as well -- array access with the data declared as an array, and struct element access with the data declared as a struct with three elements. Mixing them will certainly confuse the reader, and in some cases, may confuse the compiler as well. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: "kwikius" <a...@servocomm.freeserve.co.uk>
Date: 25 May 2006 23:26:10 -0400
Local: Fri 26 May 2006 04:26
Subject: Re: Array index operator in 3d vector class
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: "Oleg" <behol...@gorodok.net>
Date: 19 May 2006 23:31:12 -0400
Local: Sat 20 May 2006 04:31
Subject: Re: Array index operator in 3d vector class
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
| Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy |
| ©2009 Google |