CoCalc Public Filesxeus-cling-cpp17.ipynbOpen with one click!
Author: Harald Schilly
Views : 300
Compute Environment: Ubuntu 20.04 (Default)

C++ 17 in CoCalc / Ubuntu 20.04

Lambda-capture not only by reference, but also by value!

In [1]:
struct MyObj { int value{ 123 }; auto getValueCopy() { return [*this] { return value; }; } auto getValueRef() { return [this] { return value; }; } };
In [2]:
MyObj mo; auto valueCopy = mo.getValueCopy(); auto valueRef = mo.getValueRef();
In [3]:
mo.value = 31; valueCopy(); // 123
123
In [4]:
valueRef(); // 321
31

Range-based for loop

In [5]:
std::array<int, 5> a{ 1, 2, 3, 4, 5 }; for (int& x : a) { x = 2*x + 1; }
In [6]:
#include <iostream>
In [7]:
for(auto const& x: a) { std::cout << x << " "; }
3 5 7 9 11

for loop

In [8]:
int i; for (i = 0; i < 10; i++) {} int x = i; std::cout << x;
10
In [ ]:

cin text input

In [8]:
int y; std::cin >> y; 2 * y + 1
:2:6: error: redefinition of 'y' int y; ^ input_line_13:2:6: note: previous definition is here int y; ^
Interpreter Error:
In [ ]:
In [ ]: