Click here to Skip to main content
15,116,654 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: New code in C? Where and why, versus C++? Pin
honey the codewitch3-Mar-21 1:29
mvahoney the codewitch3-Mar-21 1:29 
AnswerRe: New code in C? Where and why, versus C++? Pin
Stuart Dootson3-Mar-21 1:01
professionalStuart Dootson3-Mar-21 1:01 
GeneralRe: New code in C? Where and why, versus C++? Pin
honey the codewitch3-Mar-21 1:28
mvahoney the codewitch3-Mar-21 1:28 
GeneralRe: New code in C? Where and why, versus C++? Pin
Stuart Dootson3-Mar-21 2:10
professionalStuart Dootson3-Mar-21 2:10 
GeneralRe: New code in C? Where and why, versus C++? Pin
honey the codewitch3-Mar-21 2:19
mvahoney the codewitch3-Mar-21 2:19 
GeneralRe: New code in C? Where and why, versus C++? Pin
Stuart Dootson3-Mar-21 2:24
professionalStuart Dootson3-Mar-21 2:24 
GeneralRe: New code in C? Where and why, versus C++? Pin
honey the codewitch3-Mar-21 2:29
mvahoney the codewitch3-Mar-21 2:29 
GeneralRe: New code in C? Where and why, versus C++? Pin
Stuart Dootson3-Mar-21 2:54
professionalStuart Dootson3-Mar-21 2:54 
There's more than one way to skin that cat Smile | :) decltype(auto) is used instead of auto here because auto strips references from the type of the thing it's inferring from (e.g. if your function returned a std::string const&, using auto would cause it to return by value, whereas decltype(auto) would cause it to return the reference). However, in this case, auto&& has the same semantics as decltype(auto), and will return by reference if that's what's wanted.

auto return types are useful for places where the return type is difficult (or impossible) to state statically - consider this code (which is valid c++17), which returns a string if it's instantiated for std::string and an integer otherwise... There are situations where something like this is useful.
C++
template <class T>
auto foo(T const& t) {
  if constexpr (std::is_same_v<T, std::string>) {
    return "Hello";
  } else {
    return 3;
  }
}

int main() {
  std::cout << foo(12) << std::endl;
  std::cout << foo(std::string("a")) << std::endl;
}

outputs
3
Hello
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

GeneralRe: New code in C? Where and why, versus C++? Pin
honey the codewitch3-Mar-21 3:06
mvahoney the codewitch3-Mar-21 3:06 
AnswerRe: New code in C? Where and why, versus C++? Pin
Gary R. Wheeler3-Mar-21 3:57
MemberGary R. Wheeler3-Mar-21 3:57 
AnswerRe: New code in C? Where and why, versus C++? Pin
mischasan3-Mar-21 7:46
Membermischasan3-Mar-21 7:46 
GeneralRe: New code in C? Where and why, versus C++? Pin
honey the codewitch3-Mar-21 7:50
mvahoney the codewitch3-Mar-21 7:50 
AnswerRe: New code in C? Where and why, versus C++? Pin
Chad3F9-Mar-21 12:46
MemberChad3F9-Mar-21 12:46 
GeneralIs this right place? Pin
Nagy Vilmos2-Mar-21 3:14
professionalNagy Vilmos2-Mar-21 3:14 
GeneralRe: Is this right place? Pin
den2k882-Mar-21 3:16
professionalden2k882-Mar-21 3:16 
GeneralRe: Is this right place? Pin
OriginalGriff2-Mar-21 3:17
mveOriginalGriff2-Mar-21 3:17 
GeneralRe: Is this right place? Pin
Nagy Vilmos2-Mar-21 3:27
professionalNagy Vilmos2-Mar-21 3:27 
GeneralRe: Is this right place? Pin
OriginalGriff2-Mar-21 3:28
mveOriginalGriff2-Mar-21 3:28 
GeneralRe: Is this right place? Pin
Nagy Vilmos2-Mar-21 3:46
professionalNagy Vilmos2-Mar-21 3:46 
GeneralRe: Is this right place? Pin
OriginalGriff2-Mar-21 3:50
mveOriginalGriff2-Mar-21 3:50 
GeneralRe: Is this right place? Pin
W Balboos, GHB2-Mar-21 3:18
mveW Balboos, GHB2-Mar-21 3:18 
GeneralRe: Is this right place? Pin
OriginalGriff2-Mar-21 3:29
mveOriginalGriff2-Mar-21 3:29 
GeneralRe: Is this right place? Pin
W Balboos, GHB2-Mar-21 3:33
mveW Balboos, GHB2-Mar-21 3:33 
GeneralRe: Is this right place? Pin
OriginalGriff2-Mar-21 3:51
mveOriginalGriff2-Mar-21 3:51 
GeneralRe: Is this right place? Pin
W Balboos, GHB2-Mar-21 4:12
mveW Balboos, GHB2-Mar-21 4:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.