473,320 Members | 2,048 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,320 software developers and data experts.

union float[3] and x,y,z

Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;

somehow, so I can use:
mything[0] = mything.y;
instead of mything.x = mything.y

Thanks in advice,

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

Sep 25 '07 #1
8 3685
On Sep 25, 10:26 pm, "Gernot Frisch" <M...@Privacy.netwrote:
Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;

somehow, so I can use:

mything[0] = mything.y;
instead of mything.x = mything.y

Thanks in advice,

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
Hi Gernot,

Just a thought. How about making a structure and overriding operators?
Cheers,

Sep 25 '07 #2
Gernot Frisch wrote:
Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;

somehow, so I can use:
mything[0] = mything.y;
instead of mything.x = mything.y

Thanks in advice,
Hasn't this been discussed here a couple of times over the past
two-three months? No, there is no sense in making a union. The
only way you can manage that is if you stuff 'x', 'y', and 'x' in
some kind of struct inside that union. In structs members can
have padding, and in arrays they don't. So, it is conceivable
that your union trick is not going to work. A solution might be

struct Blah {
float pos[3];
float &x, &y, &z;
Blah() : x(pos[0]), y(pos[1]), z(pos[2]) {}
};

which makes it non-POD (members that are references). Or agree
to define 'x' as a function:

struct Blah {
float pos[3];
float& x() { return pos[0]; }
float x() const { return pos[0]; }
};

In any case, search the archives for "union padding array" and
some other keywords you can come up with.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 25 '07 #3
On 2007-09-25 14:26, Gernot Frisch wrote:
Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;

somehow, so I can use:
mything[0] = mything.y;
instead of mything.x = mything.y
No, however you can do something like this:

struct thing
{
float arr[3];
float& x;
float& y;
float& z;

thing() : x(arr[0]), y(arr[1]), z(arr[2]) {}
};

Which allows you to access x through either t.x or through t.arr[0]
where t is an object of type thing:

int main()
{
thing t;
t.x = 1;
t.y = 2;
t.arr[2] = 3;

int i = 0;
}

If you want to be able to use t[0] instead of t.arr[0] you have to
overload the [] operator as Alexander Dong Back Kim suggested.

--
Erik Wikström
Sep 25 '07 #4
Erik Wikström wrote:
On 2007-09-25 14:26, Gernot Frisch wrote:
>Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;

somehow, so I can use:
mything[0] = mything.y;
instead of mything.x = mything.y

No, however you can do something like this:

struct thing
{
float arr[3];
float& x;
float& y;
float& z;

thing() : x(arr[0]), y(arr[1]), z(arr[2]) {}
};

Which allows you to access x through either t.x or through t.arr[0]
where t is an object of type thing:

int main()
{
thing t;
t.x = 1;
t.y = 2;
t.arr[2] = 3;

int i = 0;
}

If you want to be able to use t[0] instead of t.arr[0] you have to
overload the [] operator as Alexander Dong Back Kim suggested.
Actually, could still do that if you do this trick:

struct thing {
float x, y, z;
struct thing_indexing {
thing& t;
thing_indexing(thing& t) : t(t) {}
float& operator[](int i) {
return i < 2 ? i < 1 ? t.x : t.y : t.z;
}
} arr;
thing() : arr(*this) {}
thing& operator=(thing const& t) {
x = t.x;
y = t.y;
z = t.z;
}
};

int main() {
thing t;
t.x = 3.1415926;
t.arr[1] = t.x;
}

RATS! I got pulled in and am writing code when I didn't want to....

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 25 '07 #5
On 2007-09-25 15:26, Victor Bazarov wrote:
Erik Wikstr�m wrote:
>On 2007-09-25 14:26, Gernot Frisch wrote:
>>Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;

somehow, so I can use:
mything[0] = mything.y;
instead of mything.x = mything.y

No, however you can do something like this:

struct thing
{
float arr[3];
float& x;
float& y;
float& z;

thing() : x(arr[0]), y(arr[1]), z(arr[2]) {}
};

Which allows you to access x through either t.x or through t.arr[0]
where t is an object of type thing:

int main()
{
thing t;
t.x = 1;
t.y = 2;
t.arr[2] = 3;

int i = 0;
}

If you want to be able to use t[0] instead of t.arr[0] you have to
overload the [] operator as Alexander Dong Back Kim suggested.

Actually, could still do that if you do this trick:

struct thing {
float x, y, z;
struct thing_indexing {
thing& t;
thing_indexing(thing& t) : t(t) {}
float& operator[](int i) {
return i < 2 ? i < 1 ? t.x : t.y : t.z;
}
} arr;
thing() : arr(*this) {}
thing& operator=(thing const& t) {
x = t.x;
y = t.y;
z = t.z;
}
};

int main() {
thing t;
t.x = 3.1415926;
t.arr[1] = t.x;
}

RATS! I got pulled in and am writing code when I didn't want to....
Sorry, but I just cannot understand the reason for this construct. To me
it looks like it provides exactly the same functionality as my code,
except that I used an array and you used a struct with an overloaded []
operator. Please, enlighten me.

--
Erik Wikström
Sep 25 '07 #6
Erik Wikström wrote:
On 2007-09-25 15:26, Victor Bazarov wrote:
>Erik Wikstr?m wrote:
>>On 2007-09-25 14:26, Gernot Frisch wrote:
Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;

somehow, so I can use:
mything[0] = mything.y;
instead of mything.x = mything.y

No, however you can do something like this:

struct thing
{
float arr[3];
float& x;
float& y;
float& z;

thing() : x(arr[0]), y(arr[1]), z(arr[2]) {}
};

Which allows you to access x through either t.x or through t.arr[0]
where t is an object of type thing:

int main()
{
thing t;
t.x = 1;
t.y = 2;
t.arr[2] = 3;

int i = 0;
}

If you want to be able to use t[0] instead of t.arr[0] you have to
overload the [] operator as Alexander Dong Back Kim suggested.

Actually, could still do that if you do this trick:

struct thing {
float x, y, z;
struct thing_indexing {
thing& t;
thing_indexing(thing& t) : t(t) {}
float& operator[](int i) {
return i < 2 ? i < 1 ? t.x : t.y : t.z;
}
} arr;
thing() : arr(*this) {}
thing& operator=(thing const& t) {
x = t.x;
y = t.y;
z = t.z;
}
};

int main() {
thing t;
t.x = 3.1415926;
t.arr[1] = t.x;
}

RATS! I got pulled in and am writing code when I didn't want to....

Sorry, but I just cannot understand the reason for this construct. To
me it looks like it provides exactly the same functionality as my
code, except that I used an array and you used a struct with an
overloaded [] operator. Please, enlighten me.
It was a facetious suggestion, so that the OP could use 'arr' to get
to the members, not just the indexing operator (working from your
original suggestion to use 'thing.arr[0]' syntax).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 25 '07 #7
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fd**********@news.datemas.de...
Erik Wikström wrote:
>On 2007-09-25 15:26, Victor Bazarov wrote:
>>Erik Wikstr?m wrote:
On 2007-09-25 14:26, Gernot Frisch wrote:
Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;
>
somehow, so I can use:
>
>
mything[0] = mything.y;
instead of mything.x = mything.y

No, however you can do something like this:

struct thing
{
float arr[3];
float& x;
float& y;
float& z;

thing() : x(arr[0]), y(arr[1]), z(arr[2]) {}
};

Which allows you to access x through either t.x or through t.arr[0]
where t is an object of type thing:

int main()
{
thing t;
t.x = 1;
t.y = 2;
t.arr[2] = 3;

int i = 0;
}

If you want to be able to use t[0] instead of t.arr[0] you have to
overload the [] operator as Alexander Dong Back Kim suggested.

Actually, could still do that if you do this trick:

struct thing {
float x, y, z;
struct thing_indexing {
thing& t;
thing_indexing(thing& t) : t(t) {}
float& operator[](int i) {
return i < 2 ? i < 1 ? t.x : t.y : t.z;
}
} arr;
thing() : arr(*this) {}
thing& operator=(thing const& t) {
x = t.x;
y = t.y;
z = t.z;
}
};

int main() {
thing t;
t.x = 3.1415926;
t.arr[1] = t.x;
}

RATS! I got pulled in and am writing code when I didn't want to....

Sorry, but I just cannot understand the reason for this construct. To
me it looks like it provides exactly the same functionality as my
code, except that I used an array and you used a struct with an
overloaded [] operator. Please, enlighten me.

It was a facetious suggestion, so that the OP could use 'arr' to get
to the members, not just the indexing operator (working from your
original suggestion to use 'thing.arr[0]' syntax).
Isn't this functionality what the OP asked for, what Alexander was
suggesting?

struct thing
{
float x, y, z;
float& operator[]( size_t Index )
{
switch ( Index )
{
case 0:
return x;
case 1:
return y;
case 2:
return z;
default:
throw "Bad Index";
}
}
};

I'm confused where this "arr" come in, I don't see it in the OP.
Sep 26 '07 #8
Gernot Frisch wrote:
Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;

somehow, so I can use:
mything[0] = mything.y;
instead of mything.x = mything.y
Why stop there? why not make point a container:

#include <cstddef>
#include <cassert>
#include <iterator>
#include <functional>
#include <algorithm>

template < typename T >
struct const_qualifier {

typedef T type;

typedef T mutable_type;
typedef T const const_type;

static bool const is_const = false;

};

template < typename T >
struct const_qualifier< T const {

typedef T const type;

typedef T mutable_type;
typedef T const const_type;

static bool const is_const = true;

};

template < typename T, bool is_const >
struct pointer_parameter;

template < typename T >
struct pointer_parameter<T,true{

typedef T const * type;

};

template < typename T >
struct pointer_parameter<T,false{

typedef T * type;

};

template < typename T, bool is_const >
struct reference_parameter;

template < typename T >
struct reference_parameter<T,true{

typedef T const & type;

};

template < typename T >
struct reference_parameter<T,false{

typedef T & type;

};

template < typename Target, typename C, bool is_const >
struct indexing_iterator_base;

template < typename Target, typename C >
struct indexing_iterator_base<Target,C,false:
public std::iterator< std::random_access_iterator_tag,
typename C::difference_type,
typename C::pointer,
typename C::reference >
{
C * the_container;
typename C::size_type the_index;

indexing_iterator_base ( C * c,
typename C::size_type i )
: the_container ( c )
, the_index ( i )
{}

typename C::reference operator* ( void ) {
return ( the_container->operator[]( the_index ) );
}

typename C::pointer operator-( void ) {
return ( & this->operator*() );
}

};

template < typename Target, typename C >
struct indexing_iterator_base<Target,C,true:
public std::iterator< std::random_access_iterator_tag,
typename C::difference_type,
typename C::const_pointer,
typename C::const_reference >
{

C const * the_container;
typename C::size_type the_index;

indexing_iterator_base ( C const * c,
typename C::size_type i )
: the_container ( c )
, the_index ( i )
{}

typename C::const_reference operator* ( void ) const {
return ( the_container->operator[]( the_index ) );
}

typename C::const_pointer operator-( void ) const {
return ( & this->operator*() );
}

};

template < typename IndexedCont,
bool is_const = const_qualifier<IndexedCont>::is_const >
struct indexing_iterator
: public indexing_iterator_base< indexing_iterator<IndexedCont,is_const>,
IndexedCont, is_const >
{

typedef IndexedCont container_type;
typedef typename container_type::size_type size_type;

private:

typedef indexing_iterator_base< indexing_iterator<IndexedCont,is_const>,
IndexedCont, is_const my_base;

using typename my_base::reference;
using typename my_base::pointer;

public:

indexing_iterator ( typename reference_parameter< container_type, is_const
>::type the_cont,
size_type the_index )
: my_base ( &the_cont, the_index )
{}

indexing_iterator & operator++ ( void ) {
++ my_base::the_index;
return ( *this );
}

indexing_iterator & operator-- ( void ) {
-- my_base::the_index;
return ( *this );
}

indexing_iterator operator++ ( int ) {
indexing_iterator result ( *this );
++ my_base::the_index;
return ( result );
}

indexing_iterator operator-- ( int ) {
indexing_iterator result ( *this );
-- my_base::the_index;
return ( result );
}

indexing_iterator operator+= ( typename my_base::difference_type dist ) {
my_base::the_index += dist;
return ( *this );
}

indexing_iterator operator-= ( typename my_base::difference_type dist ) {
my_base::the_index -= dist;
return ( *this );
}

indexing_iterator operator+ ( typename my_base::difference_type dist )
const {
return
( indexing_iterator
( &my_base::the_container, my_base::the_index + dist ) );
}

indexing_iterator operator- ( typename my_base::difference_type dist )
const {
return
( indexing_iterator
( &my_base::the_container, my_base::the_index - dist ) );
}

};

template < typename C, bool b >
typename indexing_iterator<C,b>::difference_type
operator- ( indexing_iterator<C,bconst & lhs,
indexing_iterator<C,bconst & rhs ) {
assert( lhs.the_container == rhs.the_container );
return ( lhs.the_index - rhs.the_index );
}

template < typename C, bool b >
bool operator== ( indexing_iterator<C,bconst & lhs,
indexing_iterator<C,bconst & rhs ) {
assert( lhs.the_container == rhs.the_container );
return ( lhs.the_index == rhs.the_index );
}

template < typename C, bool b >
bool operator!= ( indexing_iterator<C,bconst & lhs,
indexing_iterator<C,bconst & rhs ) {
assert( lhs.the_container == rhs.the_container );
return ( lhs.the_index != rhs.the_index );
}

template < typename C, bool b >
bool operator< ( indexing_iterator<C,bconst & lhs,
indexing_iterator<C,bconst & rhs ) {
assert( lhs.the_container == rhs.the_container );
return ( lhs.the_index < rhs.the_index );
}

template < typename C, bool b >
bool operator<= ( indexing_iterator<C,bconst & lhs,
indexing_iterator<C,bconst & rhs ) {
assert( lhs.the_container == rhs.the_container );
return ( lhs.the_index <= rhs.the_index );
}

template < typename C, bool b >
bool operator( indexing_iterator<C,bconst & lhs,
indexing_iterator<C,bconst & rhs ) {
assert( lhs.the_container == rhs.the_container );
return ( lhs.the_index rhs.the_index );
}

template < typename C, bool b >
bool operator>= ( indexing_iterator<C,bconst & lhs,
indexing_iterator<C,bconst & rhs ) {
assert( lhs.the_container == rhs.the_container );
return ( lhs.the_index >= rhs.the_index );
}
struct point {

typedef float value_type;

typedef value_type & reference;
typedef value_type const & const_reference;
typedef value_type * pointer;
typedef value_type const * const_pointer;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;

value_type x, y, z;

point ( float x_, float y_, float z_ )
: x ( x_ )
, y ( y_ )
, z ( z_ )
{}

value_type const & operator[] ( std::size_t i ) const {
assert( i < 3 );
return ( (*this).*proxy() [i] );
}

value_type & operator[] ( std::size_t i ) {
assert( i < 3 );
return ( (*this).*proxy() [i] );
}

typedef indexing_iterator<pointiterator;

iterator begin ( void ) {
return ( iterator( *this, 0 ) );
}

iterator end ( void ) {
return ( iterator( *this, 3 ) );
}

typedef indexing_iterator<point constconst_iterator;

const_iterator begin ( void ) const {
return ( const_iterator( *this, 0 ) );
}

const_iterator end ( void ) const {
return ( const_iterator( *this, 3 ) );
}
private:

static
value_type point::* const * const proxy ( void ) {
static value_type point::* const dummy [3] =
{ &point::x, &point::y, &point::z };
return ( dummy );
}

};

namespace std {

template <>
struct less< point {

bool operator() ( point const & lhs, point const & rhs ) {
return ( std::lexicographical_compare( lhs.begin(), lhs.end(),
rhs.begin(), rhs.end() ) );
}

};

} // namespace std

#include <iostream>
#include <iomanip>
int main ( void ) {
point p ( 1, 1, 1 );
point q ( 1, 1, 1.1 );
std::cout << std::boolalpha << std::less<point>()( p, q ) << '\n';
p.x = q[2];
std::cout << p.x << '\n';
}
Just kidding :-)

However: hidden in the above code is yet another answer to your question.
Best

Kai-Uwe Bux
Sep 26 '07 #9

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

Similar topics

9
by: (Pete Cresswell) | last post by:
I've got some SQL that works as far as returning a recordset from a series of UNION statements. viz: SELECT whatever UNION this UNION that UNION other
5
by: Ricky Lung | last post by:
struct Foo { union { int& i; float& j; }; Foo(int& val) : i((int&)val), j((float&)val) {} };
6
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
16
by: Bun Head | last post by:
Does anyone see a problem with.. #include <stdio.h> typedef struct { float x; float y; float z; } _XYZ_;
10
by: tapeesh | last post by:
I created a C file say struct.c with the following structure declarations in the same file struct A { union key { int i; float f; }k1;
56
by: ccwork | last post by:
Hi all, Here is a sample code segment: .... typedef PACKED struct { union { PACKED struct { char red:1;
16
by: tedu | last post by:
does anyone know of a platform/compiler which will place union elements to not overlap? as in union u { int a; long b; size_t c; }; in my limited experience, writing to any of (a, b, or c)...
10
by: piboye | last post by:
Hi ! I'm a academician in china. I have been intereted in C++ lasting. In reading the C++ Primer book, i have a trouble about union. In the book ,it said that union can have constructors and...
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.