SFML community forums

Help => General => Topic started by: hanik on August 24, 2014, 06:33:09 pm

Title: C++ 11 compatibility
Post by: hanik on August 24, 2014, 06:33:09 pm
I rewrote sample game from SFML Game Development book using C++ 11 standart. Specifically, I used uniform initialization syntax {} in constructor. But in one place it didn't work correctly.

Button::Button(const FontHolder& fonts, const TextureHolder& textures)
: mCallback{}
, mNormalTexture{textures.get(Textures::ButtonNormal)}               // here
, mSelectedTexture{textures.get(Textures::ButtonSelected)}
, mPressedTexture{textures.get(Textures::ButtonPressed)}
, mSprite()
, mText{"", fonts.get(Fonts::Main), 16}
, mIsToggle{false}
{
        mSprite.setTexture(mNormalTexture);

        sf::FloatRect bounds = mSprite.getLocalBounds();
        mText.setPosition(bounds.width / 2.f, bounds.height / 2.f);
}
 

Game works, but textures don't render on window. Instead of them there is only white rectangle. However, when I use old style () initialization everything is OK.

I use Code::Blocks with MinGW compiler (version is 4.5.2), Windows 7.

I wonder where is error? Maybe it is in MinGW?

Thank you.
Title: Re: C++ 11 compatibility
Post by: georger on August 25, 2014, 06:05:10 pm
I tried this with TDM-GCC 4.8.1 and VS2013 on my laptop (Windows 8.1 64-bit):
- TDM-GCC: same error, the textures aren't rendered.
- VS2013: works fine.

I suppose this is a bug in MinGW/GCC, but I haven't tested this on any other platform.

I like C++11 brace initialization, and I also like std::make_unique, which VS2013 has but TDM-GCC 4.8.1 doesn't; and I also find VS to be a very good IDE. Since the Express Edition of VS2013 is free, consider using it.
Title: Re: C++ 11 compatibility
Post by: victorlevasseur on August 25, 2014, 08:39:58 pm
Note that std::make_unique is not in the C++11 standard but in the C++14.
Title: Re: C++ 11 compatibility
Post by: georger on August 26, 2014, 12:22:11 am
Note that std::make_unique is not in the C++11 standard but in the C++14.

While that's nice to know, I didn't state it was C++11; I merely mentioned it as one of the reasons @hanik should consider VS2013, the main one being that brace initialization works in his use case.
Title: Re: C++ 11 compatibility
Post by: hanik on August 27, 2014, 08:21:16 pm
Thanks for your replies!
I am already using Code::Blocks/VS2012 interchangeably. But I will consider VS2013 also!
Title: Re: C++ 11 compatibility
Post by: Jesper Juhl on August 27, 2014, 08:30:38 pm
If all you did was use braced initializers, then I'd argue that "that's not rewriting the code in C++11".
There is so much more to C++11 than just that one little feature. It's almost a completely new language and {} initializers is just a tiny (and far from the most interesting) part of it.
Title: Re: C++ 11 compatibility
Post by: hanik on August 27, 2014, 09:12:36 pm
As you mentioned, c++ 11 is a new language, so is it reasonable to learn to write code in this new laguage, instead of old one?
Title: Re: C++ 11 compatibility
Post by: Jesper Juhl on August 27, 2014, 09:17:01 pm
Yes! C++11/14 is a much better language than C++98/03 ever was.