[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

In terms of games when or how should I use const and constexpr

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 63
Thread images: 5

File: Dumbhset.png (24KB, 300x300px) Image search: [Google]
Dumbhset.png
24KB, 300x300px
In terms of games when or how should I use const and constexpr properly?
>>
>>61098822
Duh. Whenever you like, of course.
>>
>>61098822
In terms of games don't use c++ ever
>>
>>61098950

Sure thing Pajeet, use a garbage collected language with a runtime like C# that way your game will run like ass, but atleast it's easier to write right?
>>
>>61098977
Use C retard
>>
>>61098982
go away linus
>>
>>61098950
My school teaches game development using unreal engine with c++
>>
>>61098822
when to use const:
> showing that a pointer/reference parameter doesn't get mutated by the function being called:
int foo(const float &bar);

> showing that a member method in a class doesn't mutate an object instance's state:
struct Foo {
int x;
int getX() const { return x; }
};
const F foo; // can't change foo
int fx = foo.getX(); // can only call methods that externally promise not to change anything

> declaring a variable instance that won't get changed after initialization (as with "foo" shown above), including members of a class

when to use constexpr:
> something needs to be computed at compile time, such as the size of an array, a integral type template parameter, an enum value, etc.

it generally doesn't hurt to make something constexpr if you can (accesses no static or class instance state if a member method), but the restrictions aren't really relaxed until C++17, and even then there aren't exactly a huge amount of opportunities to do so unless you're really going out of your way to write functionally pure stuff.
>>
>>61098982
Show me one good game made in the last 10 years that used C.
>>
>>61100923
C++ is written in C
>>
>>61098977
C++ is a garbage collected language
>>
>>61100970
>not a game
>not within the last 10 years
Try again.
>>61100978
Wrong.
>>61098950
Also wrong.
>>
>>61100989
>Wrong.
http://en.cppreference.com/w/cpp/memory#Garbage_collector_support
>>
>>61098822
>how should I use const and constexpr
By not using C pee pee
>>
>>61100970
kek
It's actually the reverse for both gcc, clang and vc.
>>
>>61101030
>support
I don't know what I even have to point this stuff out to you. That's like saying D is a non-garbage collected language just because it can be turned off.
>>
>>61101030
At this point calling you a pajeet would be an insult to actual pajeets.
>>
>>61101051
>GCC is written in C
I hear this FUD quite a lot, is Microsoft running a mass visual C++ shilling campaign recently?

>>61101061
C++ is for indian brainlets that can't into C

>>61101056
True, both D and C++ are garbage collected language now
>>
>>61101056
C++ standard library has built in GC, and it's only going to grow.
>>
Using constexpr in every instance where it can be used, and using const in every instance where const can be used but constexpr cannot, cannot in any way negatively impact your program's runtime. Of course, this may be tedious, so I recommend that all compile-time constants be declared constexpr, rather than using #define (this gives them proper typing), and that for all function/method parameters where a pointer or reference is taken as an argument, const should be the default unless writing to the data pointed at is necessary. That is, const vs non-const should be used to document whether or not a function writes to its arguments. Furthermore, methods should be declared const if they do not write to the object they are called upon. Lastly, string literals should be declared as const char* when read-only, char[] when write access is necessary, and NEVER as char*.

Everything else is a stylistic choice.
>>
>>61098822
Use const for things that should never change. It's a replacement to having strings or magic numbers appearing in your code.

Resr
>>
File: 14640674302780.jpg (105KB, 646x1024px) Image search: [Google]
14640674302780.jpg
105KB, 646x1024px
>>61101157
This guy gets it.
>>
Do everything in SQL
>>
C is for brainlets.
>>
>>61101157
Can I get a tl;dr on constexpr?
>>
File: >brainlets.jpg (2MB, 2606x2087px) Image search: [Google]
>brainlets.jpg
2MB, 2606x2087px
>>61101487
C is dumb enough to be useful
>>
>>61100978
RAII ≠ GC
>>
>>61100978
lol
>>
>>61101528

A constexpr is just a compile time constant and can be used anywhere any other compile time constant. So you can use it as a template parameter or array size.

A constexpr function is a function that can be computed at compile time to transform one constexpr value into another. On paper, these functions are turing complete. In practice, your compiler will throw an error if you exceed a particular recursion depth. It is not possible to use a constexpr to generate an infinite loop in GCC and Clang yo my knowledge.

TL;DR: Think of it like a type safe #define

>>61101571

So... either C++11 or C++14 was supposed to have had an optional garbage collector. I'm not sure if it made it into the final standard or not, but either way, GCC doesn't support it. I guess you could say that C++ has a garbage collector in the same way that C has thread support.
>>
>>61098822
Whenever you can, but don't force it. By forcing it I mean majorly rewriting efficient methods to be able to slap constexpr on it. Sometimes it doesn't worth it.
Also const and constexpr are interface, you are not writing a library, so that's okay.
>>
>>61101114
completely optional to use, try again
>b-but D's GC is optional too
Yeah, and C++ exceptions and rtti are optional too. Have fun with your third party library dependencies though.
>>
>>61098822

Use const everywhere possible, especially when dealing with references/pointers.
Use constexpr for compile time constants instead of macro defines.
>>
>>61101102
>True, both D and C++ are garbage collected language now
For some reason, /g/ was never able to really grasp what C++ really is or looks like, or how to use it properly. Most of them are still stuck on the "C with classes" analogy. Don't get me wrong, bashing a language for being ugly is a legit thing to do, to a certain point, that is. If the king is ugly, that's not going to change the fact that he'll be better suited to rule the kingdom.

I'd bet like 80% of people bashing c++ don't know what a copy constructor is.

Ever since C++11 there's no reason to use anything else if you have a c++ compiler at hand.
>>
>>61102320
>copy constructor
A move constructor, I meant, not even /g/ is that dumb.
>>
>>61102320
I think you're going a bit far, but it's clear that a lot of /g/ either doesn't know C++ or only knows what it used to be.
>>
Use Rust.
>>
>>61100923
SDL is written in C, and that is used by a lot of games (all valve games, for example)
>>
>>61102320
It's not just "ugly", it's an undesigned mess. Typical C++ code looks ugly because C++ is being used in ways no one could have foreseen 30 years ago. It's just a grinding tangle of useless keywords and a monstrously bloated standard library.
>>
>>61102984
This. I don't really know what C++ done right looks like though.
>>
>>61102984
>"no"
>>
>>61102320
>Most of them are still stuck on the "C with classes" analogy.
It really is C with Classes. It's called C++. (I call it POO garbage). C++ is ugly, crippled and it appeals to substandard programmer and C rejects.

It's watered down C with high level features.
>>
>>61101622
exdee
>>
>>61103946
if your idea of writing C++ is writing C with Classes then it's no surprise you're having a bad time.
>>
wow didn't know games require different coding standards

stay away from /g/, they make you stupid
>>
>>61099272
Please note ue4 "c++" is there own version of c++ with more hacked on stuff
>>
>>61100923
Quake, the grandfather of all fps, was made with C
>>
>>61098982
Whatever you do, don't do this.
>>
>>61104780
>last ten years
Games have gotten a little more complex in the twenty years since Quake came out and that's exactly what C can't cope with.
>>
>>61102435
SDL is also just a wrapper around various OS APIs for creating an OpenGL context and collecting inputs, hardly the most complex part of a game.
>>
>>61104850
every good fps uses an engine based off the original quake engine
>>
>>61104866
>the fundamentals of 3D rendering don't really change
No shit really? Doesn't make trying to write a procedural city simulation in a language without objects, namespaces, templates function overloading or generally anything else that might make your life tolerable any less of a stupid idea.
>>
>>61104891
>objects
learn to code

>namespaces
what are header files

>templates
what are void*

>function overloading
literally who cares
>>
>>61104891
t. never wrote a procedural city simulation in C
it's actually REALLY easy, you're probably a brainlet though
>>
File: 1377540756453.jpg (11KB, 224x225px) Image search: [Google]
1377540756453.jpg
11KB, 224x225px
>>61104937
>>
>>61104934
>learn to code
Learn to memory leak

>what are header files
The express route to namespace exhaustion

>what are void*
An infinitely inferior half assed replacement to proper templated containers like std::array.

>literally who cares
Anyone who actually wants to enjoy programming.
>>
>>61104934
>what are header files
>what are void*
Not even close to the same thing
>>
>>61104964
why would I want to use std::array?
>>
>>61104934
>what are void*
Since when can void* be used for code generation?
>>
>>61101051
When wikipedia/some mailing lists say GCC is written in C++, they mean g++ support was allowed to use C++ awhile back. Saying GCC is written in C++ is just as accurate as saying GCC is writteb in FORTRAN. Before g++ was written only in C. GCC is still mostly C.
>>
Constexpr is to name compile-time constants and things that would be magic numbers. You can use it to precompute things like lookup tables as well, to save the program from doing work at runtime.

Const is for data structures or objects that need to be immutable, to maintain your sanity. High quality industrial code is usually expected to be const correct, i.e. every variable that can be set to const without any compiler error should be set to const.
>>
>>61107506
Also, a useful tip which you need to keep in mind: a const declaration does not mean that the given variable can't change, it means that this particular piece of code can't change it.

Const variables can still be changed by code that doesn't know that the variable is const, for example if you are implicitly using unsafe pointer casts.
>>
>>61098822

> in terms of games

you must be 10 years old
>>
>>61107728
Nah he's just a C++ "programmer"
Thread posts: 63
Thread images: 5


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.