« first day (3458 days earlier)      last day (1552 days later) » 

5:44 AM
Hello everyone! Whats the point of returning const in any functions?
 
6:04 AM
in what language?
 
c++
 
you mean, like this: int trueRandom () { return 4; }?
 
6:25 AM
const int truerandom();
 
afaik, if it's neither pointer nor reference, there is no point in const
 
6:41 AM
thanks
 
user92578
7:17 AM
@0x00004 You can in theory prevent expensive calls / assignment on temporaries, but I think returning a const value ruins move semantics on chained calls
 
12:09 PM
@0x00004 the usefulness comes whe you return a pointer to an object stored in the object that has the function.
 
@0x00004 In certain languages, like C++, it makes sense to have functions return consts, with the idea being that sometime in the future you might change the value that it returns, and when re-compiling the program, the function is optimised anyway by the compiler.
They can work like `const` variables, except you should use each where it makes more sense. For example if you want to return the screen's width (for example) and you forced your game to only have 300px width and nothing else, you could `return 300;`, but your code would still call `getGameWidth();` to get that value.

So in the future, if you decide your game can support an arbitrary width, you can change the function to return that instead, and the code of your game doesn't have to change at all.
 
user92578
@TomTsagk But this is 100% an example where returning a const value doesn't make any sense
 
user92578
It should be int getGameWidth(), not const int getGameWidth()
 
user92578
I couldn't find any case where it would be useful to do that, since with const std::vector<int> MakeIntVector() and DoStuff(MakeIntVector()), where DoStuff has two overloads, void DoStuff(std::vector<int>&& v) and void DoStuff(const std::vector<int> v), the compiler has to choose the one that makes a copy instead of being able to move the temporary
 
12:31 PM
Ah I see what you mean,

You can still treat the function as a "variable", so the same way you would make a `const` variable, you can do a function instead, but its purpose is to calculate its value based on other const variables.
If you needed more than 1 statement to calculate the value of a const, or for readability, const functions have their uses, but I do agree, they are not used often
 
user92578
What do you mean with "const functions have their uses"?
 
user92578
I literally can't think of any reasonable reason to return a const value from a function
 
@TomTsagk const int func() is not the same as int func() const
 
12:47 PM
Can you think of a reason to use a `const` variable?
What if calculating that variable needed 10 different `if` statements, all of them based on other `const` variables? Wouldn't it be more readable to have a function? (keep in mind the compiler optimises all of that away, so it makes no difference for the end user)
@Vaillancourt I assume people meant const int func(), as in, return a constant integer value from a function
 
how to mutate an integer then?
 
@trollingchar in what way?
If you save the result of a const function in a non-const variable, then you can mutate it as much as you want
 
nwp
I'm pretty sure int f() and const int f() are the the exact same thing. See this.
It does make a difference for non-builtins though and there are very rare cases when returning a const T is useful. It was something with regards to std::min where you have issues with unnecessary copies and returning references to temporaries.
 
1:03 PM
@TomTsagk yes, and I don't think this makes sense either, if the call site wants to have it const, they can do const int funcRes = func();, forcing the call site to make the variable const is a bit annoying in this case ..
 
nwp
It doesn't force the call site. int funcRes = func(); for const int func(); compiles just fine.
 
user92578
@TomTsagk Yeah I'm not sure if you're aware how C++'s value semantics work
 
@nwp ah ok!
 
nwp
This is probably worth a read. It's a bit old though.
 
user92578
1:19 PM
@nwp I think also applies the other way, returning const prevents choosing an rvalue reference overload at least according to my quick tests
 
@Tyyppi_77 Well, I've made plenty of low-level programs that use even inline functions, I did mention functions returning a const is not something you see every day, but I can see a point in them, if you don't like them just don't use them, other people find them interesting
 
nwp
@Tyyppi_77 It does. Finding a situation where that is intentional is a challenge though.
 
user92578
@TomTsagk In that case I still don't see what you mean with the variable chaning, can you give an example code of that?
 
Are we talking about returning a pointer to a const object? That has a purpose
 
user92578
No, returning a const value
 
1:26 PM
But a copy of a const value is... not-const
You can write it, but it won't actually mean a thing
 
nwp
Unless the thing is non-copyable and has non-const member functions.
 
Then you would be returning a pointer the a const object right?
The pointer itself not being constant because it's just copied
Or a move thing?
 
nwp
So many people jumped in that we don't know anymore what we are talking about. So far we have established that int foo() and const int foo() are the same thing and that std::string foo() and const std::string foo() are different things. And that making const std::string foo() useful and intended is difficult.
 
Ah okay :)
I should shut up :D
 
nwp
We also established that const T *foo() and T *foo() are different and useful, but nobody challenges that, so that conversation died.
 
1:31 PM
It was inevitable.
 
nwp
I remember a cppcon talk where they presented an example where returning a const T is useful and good, but even then it was rather questionable. But I can't remember the talk or the context enough to find a reference.
I'm fairly sure though it was related to std::min. You want to be able to do both int x{}; int y{}; int &m = std::min(x, y); and int m = std::min(x, -1); and implementing that without allowing int &m = std::min(x, -1); where m is an invalid reference is tricky.
And if you ever needed a reason why C++ is awesome: Only in C++ do people debate for week on how to implement a function that simply returns the minimum of 2 values.
 
user92578
Yeah I think only confusion that's left seems to be why @TomTsagk finds returning const int or something useful
 
user92578
@nwp Oh yeah that seems like a case where it would make sense, since you can't return a reference to a temporary or something, so you want to force min to choose an overload that doesn't take an rvalue reference
 
user92578
Params to min need to have the same type?
 
nwp
1:43 PM
Yup.
Spoiler in the message history.
I wanted to complain that we don't have spoiler tags, but I found a workaround.
 
 
3 hours later…
4:45 PM
Hi, is SCTP useful for writing real-time game servers, the kind that usually use UDP?
 
@omrisim210 I don't know, sorry.
 
 
6 hours later…
10:37 PM
@Almo looks like the comments are effectively disabled...
 
yeah
me talking about Dead by Daylight.
I show up after the initial discussion of the archives.
 

« first day (3458 days earlier)      last day (1552 days later) »