[won't fix] vector2d won't accept non-int types.

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
DtD
Posts: 264
Joined: Mon Aug 11, 2008 7:05 am
Location: Kansas
Contact:

[won't fix] vector2d won't accept non-int types.

Post by DtD »

In my development of a UI system that is apart from the default Irrlicht GUI system, I discovered that vector2d and dimension2d can not be used with non-integer types.

For example:

Code: Select all

struct unit
{
 irr::f32 value;
 EPantheon_Units units;
};

typedef irr::core::vector2d<unit> vector2dunit;
Using vector2dunit will result in a an error that basically says "Error on line 25 of vector2d.h can not convert int to type 'unit'"

This is what is on line 25:

Code: Select all

//! Default constructor (null vector)
vector2d() : X(0), Y(0) {}
So it is assigning 0 (and integer) to X and Y (which are "unit's")

The following will fix the problem.

Code: Select all

//! Default constructor (null vector)
vector2d() : X(T()), Y(T()) {}
As far as I know, this does not interfere with Irrlicht's internals in any way. However, I have not done very extensive testing.

I also applied the same patch for dimension2d.h (Line 24)

Code: Select all

//! Default constructor for empty dimension
dimension2d() : Width(T()), Height(T()) {}
~DtD
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Your patch no longer does the same thing. The vector is initialized with 0,0 while after your patch it is uninitialized. Every code depending on that would break - not really an option. But you don't need that anyway. Allow your type to be initialized with a float.

edit: Sorry, seems that was wrong. Using constructor as below will still work.

So all you need is a constructor:

Code: Select all

unit(f32 val) : value(val) {}
Last edited by CuteAlien on Thu Oct 08, 2009 4:21 am, edited 1 time in total.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Actually, DtD is correct. Did you test this, or do you have a testcase to prove otherwise?

The C++ language has very specific rules for what should happen when value initializing objects. There is a big difference between the two lines of code below...

Code: Select all

T a;
T b = T();
In the first creates a default-initialized object. The second creates a value-initialized object. If T above is int, then a would have indeterminate value, but b would be zero. If T were a class type with a default constructor, that default constructor would be invoked in both cases.

A simpler and potentially more efficient way to write this would be...

Code: Select all

vector2d() :X(), Y() { }
Travis
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Hm, I wondered if there was something to the () when I typed my answer. I think if read about that once, but just plain didn't remember what it was. Also didn't see anything in my Stroustroup book when looking. But now you mentioned it and I googled it up and you are basically correct.

Though it seems I'm not alone in missing that c++ feature as the same happened to several compiler writers including the VS 2005 and gcc 4.2 and borland builder 11 teams. So it's probably nothing we should rely on.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply