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

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

Other urls found in this thread:

en.cppreference.com/w/cpp/memory#Garbage_collector_support
twitter.com/NSFWRedditVideo

Duh. Whenever you like, of course.

In terms of games don't use c++ ever

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?

Use C retard

go away linus

My school teaches game development using unreal engine with c++

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.

Show me one good game made in the last 10 years that used C.

C++ is written in C

C++ is a garbage collected language

>not a game
>not within the last 10 years
Try again.
Wrong.
Also wrong.

>Wrong.
en.cppreference.com/w/cpp/memory#Garbage_collector_support

>how should I use const and constexpr
By not using C pee pee

kek
It's actually the reverse for both gcc, clang and vc.

>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.

At this point calling you a pajeet would be an insult to actual pajeets.

>GCC is written in C
I hear this FUD quite a lot, is Microsoft running a mass visual C++ shilling campaign recently?

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

True, both D and C++ are garbage collected language now

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.

Use const for things that should never change. It's a replacement to having strings or magic numbers appearing in your code.

Resr

This guy gets it.

Do everything in SQL

C is for brainlets.

Can I get a tl;dr on constexpr?

C is dumb enough to be useful

RAII ≠ GC

lol

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

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.

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.

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.

Use const everywhere possible, especially when dealing with references/pointers.
Use constexpr for compile time constants instead of macro defines.

>True, both D and C++ are garbage collected language now
For some reason, Sup Forums 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.

>copy constructor
A move constructor, I meant, not even Sup Forums is that dumb.

I think you're going a bit far, but it's clear that a lot of Sup Forums either doesn't know C++ or only knows what it used to be.

Use Rust.

SDL is written in C, and that is used by a lot of games (all valve games, for example)

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.

This. I don't really know what C++ done right looks like though.

>"no"

>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.

exdee

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 Sup Forums, they make you stupid

Please note ue4 "c++" is there own version of c++ with more hacked on stuff

Quake, the grandfather of all fps, was made with C

Whatever you do, don't do this.

>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.

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.

every good fps uses an engine based off the original quake engine

>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.

>objects
learn to code

>namespaces
what are header files

>templates
what are void*

>function overloading
literally who cares

t. never wrote a procedural city simulation in C
it's actually REALLY easy, you're probably a brainlet though

...

>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.

>what are header files
>what are void*
Not even close to the same thing

why would I want to use std::array?

>what are void*
Since when can void* be used for code generation?

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.

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.

> in terms of games

you must be 10 years old

Nah he's just a C++ "programmer"