1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
int quote_delims = 100'000'000;
int float_delims = 100.00'00;
int hex_delims = 0xFF'Fa12;
int bin_delims = 0b10'1000;
// string literal with encoding-prefix
char str1[] = u8"UTF-8 string";
char16_t str2[] = u"UTF-16 string";
char32_t str3[] = U"UTF-32 string";
// Raw string literal
char raw_str1[] = R"(foo
bar
baz)";
char raw_str2[] = R"delim(\w+ \d+)delim";
char16_t raw_str3[] = uR"(\w+ \d+)";
// time literal
#include <chrono>
using namespace std::chrono_literals;
auto oneDecade = 10y;
auto oneYear = 365.2'425d;
auto oneDay = 24h;
auto halfAnHour = 30min;
auto oneYearInMins = 525'949.2min;
auto halfAnHour = 0.5h;
auto oneMin = 60s;
auto oneSec = 1000ms;
auto oneMilliSec = 1000us;
auto oneMicroSec = 1000ns;
// complex literal
#include <complex>
using namespace std::complex_literals;
auto complexNumber = 1 + 1i;
auto complexNumberl = 1 + 1il;
auto complexNumberf = 1 + 1if;
// null pointer
int *a = nullptr;
// bit field
struct S {
// will usually occupy 2 bytes:
// 3 bits: value of b1
// 2 bits: unused
// 6 bits: value of b2
// 2 bits: value of b3
// 3 bits: unused
unsigned char b1 : 3, : 2, b2 : 6, b3 : 2;
};
std::cout << sizeof(S) << '\n'; // usually prints 2
// loops
int n = 10;
while (n --> 0) {
std::cout << n << '\n';
}
for (int &i : {1, 2, 3, 4}) {
std::cout << i << '\n';
}
// decltype
int num = 1;
decltype(num) total = num;
struct A { double x; };
const A *a = A {0};
decltype(a->x) ax = a->x;
// strongly typed enums
enum class Day {
Mon, Tue, Wed, Thur, Fri, Sat, Sun
};
Day today = Day::Mon;
// lambda function
auto add = [](int x, int y) { return x + y; };
auto noParam = [] { return 5; };
auto invokeNow = [](int x) { return x * 5; }(1);
int captureMe = 5;
auto byCopy = [captureMe] { return captureMe; };
auto byRef = [&captureMe] { return captureMe; };
auto allByCopy = [=] { return captureMe; };
auto allByRef = [&] { return captureMe; };
auto increment = [y = 1](int x) { return x + y; };
double distance = [](double x, double y, double xx, double yy) -> double {
return abs(x-xx) + abs(y-yy);
};
// templates
namespace N
{
template<class T>
class Y // template definition
{
void mf() { }
};
}
template class N::Y<char*>;
template void N::Y<double>::mf();
template struct Z<double>;
template<typename T> concept C1 = sizeof(T) != sizeof(int);
template<C1 T> struct S1 { };
template<C1 T> using Ptr = T*;
template <typename, typename...>
template<class...> struct Tuple { };
template<typename ...Ts> void f(Ts...) {}
// variadic template
template<class F, class... Args>
void forward_args(F f, Args... args) {
f(std::forward<Args>(args)...);
}
// using auto
template<auto F = requires { foo(); }>
void func();
template<auto F = []{}>
void func();
// namespace
namespace printing {
inline namespace latest {
using std::cout;
void print() {
cout << "Latest print\n";
}
}
namespace old {
void print() {
::printf("Old print\n");
}
}
}
printing::print();
printing::latest::print();
printing::old::print();
namespace oldPrint = printing::old;
oldPrint::print();
// attribute
[[noreturn]] void throwError() { throw "error"; }
[[deprecated("useless")]] void doNothing() {}
namespace std {
class thread {
public:
// types:
class id;
typedef void *native_handle_type;
// construct/copy/destroy:
thread() noexcept;
template <class F, class ...Args> explicit thread(F&& f, Args&&... args);
~thread();
thread(const thread&) = delete;
thread(thread&&) noexcept;
thread& operator=(const thread&) = delete;
thread& operator=(thread&&) noexcept;
// members:
void swap(thread&) noexcept;
bool joinable() const noexcept;
void join();
void detach();
id get_id() const noexcept;
native_handle_type native_handle();
// static members:
static unsigned hardware_concurrency() noexcept;
};
}
// try-block
try {
throw std::runtime_error("Runtime error!");
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
constexpr int factorial(int n) {
return n > 0 ? n * factorial(n - 1) : 1;
}
/* foo */ #if 0
this is commented
#endif
/* it shouldn't hang */ /* trying to lex this */
/*{"ahg/awn/xan?", HB_TAG('A','G','W',' ')},*/ /* Agaw */
/*{"gsw?/gsw-FR?", HB_TAG('A','L','S',' ')},*/ /* Alsatian */
/*{"krc", HB_TAG('B','A','L',' ')},*/ /* Balkar */
/*{"??", HB_TAG('B','C','R',' ')},*/ /* Bible Cree */
/*{"sgw?", HB_TAG('C','H','G',' ')},*/ /* Chaha Gurage */
/*{"acf/gcf?", HB_TAG('F','A','N',' ')},*/ /* French Antillean */
/*{"vls/nl-be", HB_TAG('F','L','E',' ')},*/ /* Flemish */
/*{"enf?/yrk?", HB_TAG('F','N','E',' ')},*/ /* Forest Nenets */
/*{"fuf?", HB_TAG('F','T','A',' ')},*/ /* Futa */
/*{"ar-Syrc?", HB_TAG('G','A','R',' ')},*/ /* Garshuni */
/*{"cfm/rnl?", HB_TAG('H','A','L',' ')},*/ /* Halam */
/*{"ga-Latg?/Latg?", HB_TAG('I','R','T',' ')},*/ /* Irish Traditional */
/*{"krc", HB_TAG('K','A','R',' ')},*/ /* Karachay */
/*{"alw?/ktb?", HB_TAG('K','E','B',' ')},*/ /* Kebena */
/*{"Geok", HB_TAG('K','G','E',' ')},*/ /* Khutsuri Georgian */
/*{"kca", HB_TAG('K','H','K',' ')},*/ /* Khanty-Kazim */
/*{"kca", HB_TAG('K','H','S',' ')},*/ /* Khanty-Shurishkar */
class Base
{
public:
Base () = default;
virtual ~Base () = default;
virtual void foo () = 0;
};
class Derived final : public Base
{
public:
Derived () = default;
virtual ~Derived () = default;
virtual void foo () override
{
auto a = 1 + 2;
}
};
#define foo bar
#define baz zot
class Highlighter : public QSyntaxHighlighter
{
class InnerClass {}
Q_OBJECT
public:
Highlighter(QTextDocument *parent = 0);
protected:
void highlightBlock(const QString &text);
private:
struct HighlightingRule
{
QRegExp pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> highlightingRules;
QRegExp commentStartExpression;
QRegExp commentEndExpression;
QTextCharFormat keywordFormat;
QTextCharFormat classFormat;
QTextCharFormat singleLineCommentFormat;
QTextCharFormat multiLineCommentFormat;
QTextCharFormat quotationFormat;
QTextCharFormat functionFormat;
};
switch (foo) {
case Foo::kBar:
break;
}
// modules
module;
import std;
export module Shapes;
export struct Shape {
Shape(int x, int y) : m_x(x), m_y(y) {};
int m_x;
int m_y;
}
float test(8.874L);
float test(8.874l);
float test(8.874F);
float test(8.874f);
float test(8.874U);
float test(8.874u);
// via https://en.cppreference.com/w/cpp/language/floating_literal
int main()
{
std::cout
<< "Literal" "\t" "Printed value" << std::left
<< OUT( 58. ) // double
<< OUT( 4e2 ) // double
<< OUT( 123.456e-67 ) // double
<< OUT( 123.456e-67f ) // float, truncated to zero
<< OUT( .1E4f ) // float
<< OUT( 0x10.1p0 ) // double
<< OUT( 0x1p5 ) // double
<< OUT( 0x1e5 ) // integer literal, not floating-point
<< OUT( 3.14'15'92 ) // double, single quotes ignored (C++14)
<< OUT( 1.18e-4932l ) // long double
<< std::setprecision(39)
<< OUT( 3.4028234e38f ) // float
<< OUT( 3.4028234e38 ) // double
<< OUT( 3.4028234e38l ) // long double
<< '\n';
static_assert(3.4028234e38f == std::numeric_limits<float>::max());
static_assert(3.4028234e38f == // ends with 4
3.4028235e38f); // ends with 5
static_assert(3.4028234e38 != // ends with 4
3.4028235e38); // ends with 5
// Both floating-point constants below are 3.4028234e38
static_assert(3.4028234e38f != // a float (then promoted to double)
3.4028234e38); // a double
}
|