Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Coffee corner » Wonders of C++14
Wonders of C++14 [message #47098] Mon, 05 December 2016 14:51 Go to next message
mirek is currently offline  mirek
Messages: 13442
Registered: November 2005
Ultimate Member
Did not expected this to compile:

	auto st = []() { struct Foo { int a, b; }; Foo x; x.a = 10; return x; };
	auto v = st();
	DUMP(v.a);


But it does and works... Wow.
Re: Wonders of C++14 [message #47101 is a reply to message #47098] Wed, 07 December 2016 08:14 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3268
Registered: August 2008
Senior Veteran
struct Foo is declared inside st, but after that is assigned to v and used in DUMP().
How is it possible?


Best regards
Iñaki
Re: Wonders of C++14 [message #47103 is a reply to message #47101] Wed, 07 December 2016 09:01 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13442
Registered: November 2005
Ultimate Member
Exactly....

Anyway, it makes sense (sort of). It is return value from st, so it has to exist. You cannot reference Foo outside st, but you get 'unspecified type' returned from 'st', which you can (of course) assign to auto variable.
Re: Wonders of C++14 [message #47104 is a reply to message #47101] Wed, 07 December 2016 09:14 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1781
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi,

This is possible since C++11. In C++14, it actually works for any function, not only lambdas:
auto st() {
  struct Foo { int a, b; };
  Foo x {10, 20};
  return x;
}
int main() {
	auto v = st();
	return v.a;
}


koldo wrote on Wed, 07 December 2016 08:14
struct Foo is declared inside st, but after that is assigned to v and used in DUMP().
How is it possible?


The magic is in the "auto", thanks to automatic return type deduction. The details of type Foo are known to the compiler, the only problem is that it is not declared outside the function, so you can't specify it as a return type. Unless you use auto, in which case compiler uses its knowledge to fill all the necessary data. It works kind of similarly to anonymous structs:
struct {
  int a = 10;
  int b;
} x;

int main() {
  return x.a;
}

You can use the values in x, but you can't simply use it's type (e.g. to create second variable of the same type), since it has no name. The auto just exposes the "anonymous type" returned by the function for the programmer to use.

Best regards,
Honza
Re: Wonders of C++14 [message #47106 is a reply to message #47104] Wed, 07 December 2016 11:56 Go to previous message
koldo is currently offline  koldo
Messages: 3268
Registered: August 2008
Senior Veteran
Ok, thank you Smile

Best regards
Iñaki
Previous Topic: I've just joined the wonderful world of Linux software distribution!
Next Topic: Clang on Windows with U++?
Goto Forum:
  


Current Time: Thu Oct 21 02:55:33 CEST 2021

Total time taken to generate the page: 0.01891 seconds