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

Author Topic: sf::Event return key pressed  (Read 8463 times)

0 Members and 1 Guest are viewing this topic.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::Event return key pressed
« Reply #15 on: May 28, 2011, 03:27:59 pm »
Do whatever you like. You can get a Windows-1252 unsigned char from SFML, what you do then is between you and your code ;)

But... Saying that the STL is horrible sounds like a beginner statement (beginners always fight against the STL). I doubt that you'll write less horrible code with a raw array. Unless you do something very limited or something that will crash at users face.
Laurent Gomila - SFML developer

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
sf::Event return key pressed
« Reply #16 on: May 28, 2011, 03:43:21 pm »
I don't agree. Some very good code can be written without using the STL. I don't need a lot of flexibility (not that STL is that flexible, you must play by its rules), and the added complexity just isn't worth it. Plus, it can be slower than raw arrays at times, especially when you don't need them.
But, I don't want to start a war.
Thanks for the help!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
sf::Event return key pressed
« Reply #17 on: May 28, 2011, 04:46:24 pm »
Quote from: "tntexplosivesltd"
I don't agree. Some very good code can be written without using the STL. I don't need a lot of flexibility (not that STL is that flexible, you must play by its rules), and the added complexity just isn't worth it.
The complexity on user side is actually much lower than when you handcraft it.
Code: [Select]
std::string s = GetString() + "xyz";
s.erase(2);
std::string t = s;
unsigned int n = std::count(t.begin(), t.end(), 'x');
vs.
Code: [Select]
char s[20];
std::strcpy(s, GetString());
std::strcat(s, "xyz");

unsigned int len = std::strlen(s);
for (unsigned int i = 2; i != len; ++i)
    s[i] = s[i+1];

char t[20];
std::strcpy(t, s);

unsigned int n = 0;
for (unsigned int i = 0; i != len; ++i)
    if (t[i] == 'x')
        ++n;

Plus, at every strcpy() and strcat() we have the omnipresent risk of buffer overflows. And this is only a simple example with static arrays. When you need dynamic allocations (because you don't know the string length in advance), the C version is even funnier, since ownership management and exception safety become real issues.

Quote from: "tntexplosivesltd"
Plus, it can be slower than raw arrays at times, especially when you don't need them.
It can of course be, but normally statements like this only arise if the compared functionality is not equivalent. It can as well be faster, as strlen(s) vs. s.length() shows well.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

regicide

  • Newbie
  • *
  • Posts: 1
    • View Profile
sf::Event return key pressed
« Reply #18 on: May 29, 2011, 04:02:48 am »
Quote from: "Laurent"
But... Saying that the STL is horrible sounds like a beginner statement (beginners always fight against the STL). I doubt that you'll write less horrible code with a raw array. Unless you do something very limited or something that will crash at users face.

It depends on what you are doing, however in the case of a string holding user supplied input it is often much 'safer' than a char[]s.

Quote from: "Nexus"
Plus, at every strcpy() and strcat() we have the omnipresent risk of buffer overflows. And this is only a simple example with static arrays.

Would a memcpy not suffice if you know the maximum size?

Quote from: "Nexus"
When you need dynamic allocations (because you don't know the string length in advance),
the C version is even funnier, since ownership management and exception safety become real issues.

Is it not rather redundant talking about exception safety when the point of using the c alternative is to avoid the STL, where exactly would these exceptions be thrown from?
RAII negates most of this issue, unless your destructors are throwing exceptions that is, in which case you are screwed either way.

Quote from: "tntexplosivesltd"
I don't agree. Some very good code can be written without using the STL. I don't need a lot of flexibility and the added complexity just isn't worth it.

Well said. The flexibility can come at a huge cost when you factor in GCC's fantastic compile time messages regarding the STL.

Nexus' point is also valid concerning strings, however the argument does not hold as strongly for the entirety of the STL.

Although it is often silly to avoid using the STL entirely, it is extremely wise to consider the alternatives (even more so in real-time applications). It is rarely a good idea to use a library utility when a language construct achieves the goal sufficiently.
The right tool for the job after all.

~Chris

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::Event return key pressed
« Reply #19 on: May 29, 2011, 10:46:38 am »
Stop focusing on the STL. My point was more about using abstractions when possible, rather than manual memory management. Whether it is the STL, boost, Qt or whatever doesn't matter, but there's really no reason to write complicated, error-prone and unsafe code when easy abstractions are available.

That's the important thing; don't start a war against STL please ;)
Laurent Gomila - SFML developer

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
sf::Event return key pressed
« Reply #20 on: May 29, 2011, 10:49:40 am »
Quote from: "tntexplosivesltd"

But, I don't want to start a war.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
sf::Event return key pressed
« Reply #21 on: May 29, 2011, 12:09:47 pm »
Quote from: "regicide"
Would a memcpy not suffice if you know the maximum size?
Yes, but then there is the risk that the null termination is not copied (or some nonsense after it is copied, which is less problematic).

Quote from: "regicide"
Is it not rather redundant talking about exception safety when the point of using the c alternative is to avoid the STL, where exactly would these exceptions be thrown from?
The STL hardly throws exceptions. std::out_of_range, std::length_error, std::bad_alloc occur very rarely when your program is correct. User-defined exceptions are the bigger problem.

Quote from: "regicide"
Nexus' point is also valid concerning strings, however the argument does not hold as strongly for the entirety of the STL.
But there are a lot of examples. E.g. std::vector has so many advantages over new[] and delete[]. And when one needs more specific data structures like linked lists or maps, reinventing the wheel is even less worth the trouble.

It is true, the right tool for the right job. The STL is not always the best solution, but it covers a lot of cases. And before one falls back to error-prone low-level mechanisms, there should be good reasons. And "having heard that char[] is faster than std::string" is none ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
sf::Event return key pressed
« Reply #22 on: May 29, 2011, 02:14:53 pm »
Quote from: "tntexplosivesltd"

But, I don't want to start a war.

Quote from: "Laurent"

don't start a war against STL please

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
sf::Event return key pressed
« Reply #23 on: May 29, 2011, 02:31:12 pm »
War? I just explain how I see things. When you say "the STL is unflexible, more complex and slower than the manual approach", you don't have to wonder when you're corrected. Myths like this are already spread enough in the C++ community.

By the way, there is no need to play the policeman :roll:
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::Event return key pressed
« Reply #24 on: May 29, 2011, 03:01:37 pm »
I will play the policeman. The OP clearly said "no war" and people can't stop debating about this, so I lock the topic.

There are too many endless topics like this one on the internet, there's really no need to start a new one.
Laurent Gomila - SFML developer

 

anything