Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: C++ 11 compatibility  (Read 1842 times)

0 Members and 1 Guest are viewing this topic.

hanik

  • Newbie
  • *
  • Posts: 3
    • View Profile
C++ 11 compatibility
« 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.
« Last Edit: August 24, 2014, 07:24:22 pm by hanik »

georger

  • Guest
Re: C++ 11 compatibility
« Reply #1 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.

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: C++ 11 compatibility
« Reply #2 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.

georger

  • Guest
Re: C++ 11 compatibility
« Reply #3 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.

hanik

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: C++ 11 compatibility
« Reply #4 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!

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: C++ 11 compatibility
« Reply #5 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.

hanik

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: C++ 11 compatibility
« Reply #6 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?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: C++ 11 compatibility
« Reply #7 on: August 27, 2014, 09:17:01 pm »
Yes! C++11/14 is a much better language than C++98/03 ever was.

 

anything