• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Ty Typhoon
      I like to build my A - Team now.
       
      I need loyal people who can trust and believe in a dream.
      If you got time and no need for direct pay please contact me now.
       
      We cant pay now, you will recieve a lifetime percentage if the released game will give earnings. 
      If I get the best people together for a team, everything should be possible.
       
       
      What i need:
      - Programmer c++
      - Unity / Unreal - we must check whats possible, please share your experience with me.
      - Sculpter, 3D Artist
      - Animator
      - Marketing / Promotion 
       
       
      What i do:
      - Studio Owner
      - Director
      - Recruit exactly you
      - Sounddesign
      - Main theme composing
      - Vocals
      - Game design
      - Gun, swords, shields and weapon design
      - Character, plants and animal design
       
       
      Please dont ask about the Name of the Game, about Designs or Screenshots.
      The game will be defintitly affected about our and your skills if you join the team.
       
       
      Planned for the big Game:
      - 1st person shooter
      - online multiplayer
      - character manipulation
      - complete big open world with like lifetime actions and reactions
      - gunstore with many items to buy
      - many upgrades for players
      - specials like mini games
       
      So if you are interested in joining a team with a nearly complete game idea, contact me now and tell me what you can do.
       
      discord:
      joerg federmann composing#2898
       
       
    • By codelyoko373
      I wasn't sure if this would be the right place for a topic like this so sorry if it isn't.
      I'm currently working on a project for Uni using FreeGLUT to make a simple solar system simulation. I've got to the point where I've implemented all the planets and have used a Scene Graph to link them all together. The issue I'm having with now though is basically the planets and moons orbit correctly at their own orbit speeds.
      I'm not really experienced with using matrices for stuff like this so It's likely why I can't figure out how exactly to get it working. This is where I'm applying the transformation matrices, as well as pushing and popping them. This is within the Render function that every planet including the sun and moons will have and run.
      if (tag != "Sun") { glRotatef(orbitAngle, orbitRotation.X, orbitRotation.Y, orbitRotation.Z); } glPushMatrix(); glTranslatef(position.X, position.Y, position.Z); glRotatef(rotationAngle, rotation.X, rotation.Y, rotation.Z); glScalef(scale.X, scale.Y, scale.Z); glDrawElements(GL_TRIANGLES, mesh->indiceCount, GL_UNSIGNED_SHORT, mesh->indices); if (tag != "Sun") { glPopMatrix(); } The "If(tag != "Sun")" parts are my attempts are getting the planets to orbit correctly though it likely isn't the way I'm meant to be doing it. So I was wondering if someone would be able to help me? As I really don't have an idea on what I would do to get it working. Using the if statement is truthfully the closest I've got to it working but there are still weird effects like the planets orbiting faster then they should depending on the number of planets actually be updated/rendered.
    • By BenjaminBouchet
      Learning game development in Unreal Engine could be a daunting task for someone who don’t know where to start, and a cumbersome process if you don’t organize your progression correctly. One thing commonly known by experienced developers and by people unfamiliar with coding: mastering a development language is a long and difficult task.
      From blueprints to C++ in Unreal Engine
      If you want to learn fast, you need a good learning strategy. Unreal Engine contains a very powerful tool which you can use to learn C++ faster: its blueprint system. Blueprints are extremely easy to learn (and you may already have a good knowledge of them). Thus you can conveniently use them as a guide for writing code in C++. This is the reason why I am writing a tutorial series on how to make the transition from Unreal Engine blueprints to C++.
      Learn and practice C++
      Following this tutorial, you’ll acquire new concepts of C++ programming in every chapter. Then following chapters will give you reasons to reuse and practice those same concepts. There’s no better way to wire you brain.
      Link to the tutorial: [Tutorial] Learn C++ in Unreal Engine 4 by making a powerful camera
      Please do send me as much feedback as you want. I’ll be considering every constructive remarks and taking them into consideration. Your feedback will help me to improve and update the existing chapters and to make the next one better.

      View full story
    • By BenjaminBouchet
      Learning game development in Unreal Engine could be a daunting task for someone who don’t know where to start, and a cumbersome process if you don’t organize your progression correctly. One thing commonly known by experienced developers and by people unfamiliar with coding: mastering a development language is a long and difficult task.
      From blueprints to C++ in Unreal Engine
      If you want to learn fast, you need a good learning strategy. Unreal Engine contains a very powerful tool which you can use to learn C++ faster: its blueprint system. Blueprints are extremely easy to learn (and you may already have a good knowledge of them). Thus you can conveniently use them as a guide for writing code in C++. This is the reason why I am writing a tutorial series on how to make the transition from Unreal Engine blueprints to C++.
      Learn and practice C++
      Following this tutorial, you’ll acquire new concepts of C++ programming in every chapter. Then following chapters will give you reasons to reuse and practice those same concepts. There’s no better way to wire you brain.
      Link to the tutorial: [Tutorial] Learn C++ in Unreal Engine 4 by making a powerful camera
      Please do send me as much feedback as you want. I’ll be considering every constructive remarks and taking them into consideration. Your feedback will help me to improve and update the existing chapters and to make the next one better.
    • By mrDIMAS
      Hello everyone! I need to fill lua table with functions from script file like this:
      function init() end function update() end I need to create table on stack and fill it with this functions from specified file. How can I do this?
  • Advertisement
  • Advertisement

C++ POD (plain old data) type requirement in union

Recommended Posts

I'm sure many of you will have come across this situation before and am wondering the most elegant way of solving it:

This is an example, of wanting to create a struct/class that is an extension of a smaller version, and wanting to still be able to access the smaller version via a union. However the compiler seems to want the smaller struct to be a POD type (presumably because which constructors / destructors to call in a union is ambiguous). This is what I want, but it won't compile, complaining about Point2 being non-POD:

struct Point2
{
	Point2() {} // want this to be default
	Point2(int a, int b) {x = a; y = b;} // extra constructor for ease of use
	int x, y;
};

struct Point3
{
	union
	{
		struct
		{
			int x, y, z;
		};
		struct
		{
			Point2 xy; // access a Point3 as either a Point3 or a Point2
		};
	};
};

The only problem is it is sometimes very useful to be able to use the 2nd constructor when passing a Point2 as an argument to a function:

DrawLineTo(Point2(10, 20));

void DrawLineTo(const Point2 &pt)
{
	...
}

I am understanding there has been some changes to POD types in later c++ (I now have c++11 available), so is there any way of keeping it as a POD type while still having the secondary constructor available?

I can also see alternative methods of doing the same kind of thing (perhaps not using a union). What do you guys use?

Edited by lawnjelly

Share this post


Link to post
Share on other sites
Advertisement

Instead of giving Point2 an ease of use constructor that lets you write:
p = Point2(a, b);
These days in C++ you can not write any constructors at all and then just write:
p = Point2{a,b}
Or even just:
p = {a,b}
or
DrawLineTo({10, 20});

Share this post


Link to post
Share on other sites

Fantastico! Can't believe I didn't know that worked lol! :D

Edit - Just tested it and my gcc compiler does suggest 'extended initializer lists needs c++11 flag'. So it is available. Out of interest, what would have been a good solution prior to c++11?

I also just found this article:

http://cpp11standard.blogspot.co.uk/2012/11/c11-standard-explained-1-unrestricted.html

Which does suggest you can use non-POD type in a union in c++11, however in his example there is a need to name the union so you can then give it a constructor, which would seem to make the access needlessly complex, so it doesn't seem an ideal solution...

Point3 pt;
pt.myunion.xy.x = 1;

 

Edited by lawnjelly
tested

Share this post


Link to post
Share on other sites

Just tested the c++11 unrestricted union mentioned in the article, and it does work! Just one caveat the original code:

struct Point3
{
	union
	{
		struct
		{
			int x, y, z;
		};
		struct
		{
			Point2 xy; // access a Point3 as either a Point3 or a Point2
		};
	};
};

fails to compile with error: member 'Point2 Point3::<anonymous union>::<anonymous struct>::xy' with constructor not allowed in anonymous aggregate.

However this works:

struct Point3
{
	union
	{
		struct
		{
			int x, y, z;
		};
		Point2 xy;
	};
};

Hooray for c++11! :D
 

Share this post


Link to post
Share on other sites

With C++17 you'll also be able to use std::variant, which is basically a type-safe union type. Not sure if C++17 is an option for you, but I thought I'd mention it.

 

That doesn't allow type punning though.

Edited by rnlf_in_space

Share this post


Link to post
Share on other sites

Problem is not completely solved (except by Hodgman's method)..

This still causes a problem:

struct Rect
{
	union
	{
		struct
		{
			int x, y, w, h;
		};

		struct
		{
			Point2 pos;
			Point2 size;
		};
	};
};

Giving :

error: member 'Point2 Rect::<anonymous union>::<anonymous struct>::pos' with constructor not allowed in anonymous aggregate
    Point2 pos;
 

Whereas:

struct Rect
{
	union
	{
		struct
		{
			int x, y, w, h;
		};

		struct
		{
			Point2 pos;
			Point2 size;
		} ps;
	};
};

*does* compile, but it means you would have to refer to pos and size through ps.pos and ps.size.

The only other way of doing this that springs to mind is a bit more hacky:

struct Rect
{
	Point2 &pos() {return *((Point2 *)&x);}
	Point2 &size() {return *((Point2 *)&w);}

	int x, y, w, h;
};

 

Share this post


Link to post
Share on other sites

Clang will happily accept the first example with one small change:

struct Point2
{
    Point2() = default;
    Point2(int a, int b) {x = a; y = b;} // extra constructor for ease of use
    int x, y;
};
Adding the "=default;" in place of {} makes different C++11 rules apply to it and makes it a POD type.
 
To make gcc happy, you need to go one step further:
 
struct Point2
{
    Point2() = default;
    int x, y;
};
// Non-member function that works like a constructor
inline Point2 Make_Point2(int a, int b) {Point2 result; result.x = a; result.y = b; return result;}
I'm not sure if gcc is being overly strict here, or if Clang is being lenient in regards to the standard.

Share this post


Link to post
Share on other sites

Ah, thanks for the info on the different compilers guys! I'm using gcc on linux, and it has to compile on android compiler too and probably iOS and windows.

Come to think of it, if the goal is simply to reduce verbosity for access, then I could use an accessor to get to the union:

struct Rect
{
	Point2 &pos {return ps.pos;}
	Point2 &size {return ps.size;}
  
	union
	{
		struct
		{
			int x, y, w, h;
		};

		struct
		{
			Point2 pos;
			Point2 size;
		} ps;
	};
};

I have found this doc on unions:

http://en.cppreference.com/w/cpp/language/union

I had no idea about the conventions of calling all the constructors / destructors as the 'lifetime' of the members changes, sounds like a nightmare lol. It almost sounds like simply casting rather than using a union is the most explicit way to do it (treat the data as binary compatible with another struct), or sticking to POD and using Hodgman's technique.

Are there any gotchas (alignment etc) to avoiding the union altogether and simply casting data members to another type, out of interest?

i.e.

struct Rect
{
	Point2 &pos() {return *((Point2 *)&x);}
	Point2 &size() {return *((Point2 *)&w);}

	int x, y, w, h;
};

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Advertisement