C++ syntax doubts

Programmers discuss here anything related to FreeOrion programming. Primarily for the developers to discuss.

Moderator: Committer

Post Reply
Message
Author
User avatar
Oberlus
Cosmic Dragon
Posts: 5715
Joined: Mon Apr 10, 2017 4:25 pm

C++ syntax doubts

#1 Post by Oberlus »

Having trouble to find an answer for this out there:

Is there a reason to prefer one syntax over the other?

Code: Select all

                bool can_attack{ObjectCanAttack(obj)};
                if (can_attack) { ... }
                DebugLogger(combat) << (can_attack ? "... can attack" : "");

Code: Select all

                bool can_attack = ObjectCanAttack(obj);
                if (can_attack) { ... }
                DebugLogger(combat) << (can_attack ? "... can attack" : "");
Last edited by Oberlus on Mon Feb 07, 2022 11:23 am, edited 1 time in total.

User avatar
adrian_broher
Programmer
Posts: 1156
Joined: Fri Mar 01, 2013 9:52 am
Location: Germany

Re: C++ syntax doubts

#2 Post by adrian_broher »

{} prevents the compiler from narrowing (e.g putting a value into a variable that cannot represent it properly).

https://en.cppreference.com/w/cpp/langu ... ialization
Resident code gremlin
Attached patches are released under GPL 2.0 or later.
Git author: Marcel Metz

Post Reply