- 1
#define $(x) [&](void)->auto{try{return (x);}catch(...){return decltype(x){};}}()
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
0
#define $(x) [&](void)->auto{try{return (x);}catch(...){return decltype(x){};}}()
Собачка
0
#include <iostream>
#include <vector>
#include <string>
int main()
{
int n = 6;
std::vector<int> vec;
for (int i = 0; i < n; i++)
{
if ((i % 2) == 0)
{
std::reverse(vec.begin(), vec.end());
}
vec.push_back(i);
if ((i % 2) != 0)
{
std::reverse(vec.begin(), vec.end());
}
for (int j = 0; j < vec.size(); j++)
{
std::cout << vec[j] + 1;
if (j != vec.size() - 1)
{
std::cout << "-";
}
}
std::cout << "\n";
}
}
Цель напечатать на экране следующее:
1
2-1
1-2-3
4-3-2-1
1-2-3-4-5
6-5-4-3-2-1
насколько это говнокод от 0 до 10?
0
// Ручной режим
if (!botMode) {
cout << "Manual mode activated\n";
Sleep(1000);
while (1) { // Основной цикл
MAP = modMap(pos, MAP, PVM);
auto moveField = moveMapField(MAP);
auto TVM = waveViewCheck(pos, MAP); // Вызов функции проверки видимости элементов
PVM = refreshMap(PVM, TVM);
system("cls"); // Очистка поля с каждой итерацией
coutMAP(pos, MAP); // Вывод всей карты
cout << endl;
coutMAP(pos, PVM); // Вывод видимой карты
if (MAP[pos.y][pos.x] == END) {
cout << endl << "Level Complete!\n";
Sleep(3000);
return 0;
}
int k = getch();
if (k == 113 || k == 81) break; // Press Q - exit
if (k == 224) k = getch();
switch (k) { // Перемещение стрелочками
case 72: // Вверх
if ((moveField[pos.y-1][pos.x] == BLANK || (moveField[pos.y-1][pos.x] > 0 && MAP[pos.y-1][pos.x] % 2 == 1)) && pos.y > 0) {
pos.y -= 1;
}
break;
case 75: // Влево
if ((moveField[pos.y][pos.x-1] != WALL || (moveField[pos.y][pos.x-1] > 0 && MAP[pos.y][pos.x-1] % 2 == 1)) && pos.y > 0) {
pos.x -= 1;
}
break;
case 77: // Вправо
if ((moveField[pos.y][pos.x+1] != WALL || (moveField[pos.y][pos.x+1] > 0 && MAP[pos.y][pos.x+1] % 2 == 1)) && pos.y > 0) {
pos.x += 1;
}
break;
case 80: // Вниз
if ((moveField[pos.y+1][pos.x] != WALL || (moveField[pos.y+1][pos.x] > 0 && MAP[pos.y+1][pos.x] % 2 == 1)) && pos.y > 0) {
pos.y += 1;
}
break;
}
}
}
0
#include <iostream>
#include <string_view>
#include <source_location>
void log(const std::string_view message,
const std::source_location location =
std::source_location::current())
{
std::cout << "file: "
<< location.file_name() << "("
<< location.line() << ":"
<< location.column() << ") `"
<< location.function_name() << "`: "
<< message << '\n';
}
template <typename T> void fun(T x)
{
log(x);
}
int main(int, char*[])
{
log("Hello world!");
fun("Hello C++20!");
}
file: main.cpp(23:8) `int main(int, char**)`: Hello world!
file: main.cpp(18:8) `void fun(T) [with T = const char*]`: Hello C++20!
Previously, functions that desire to obtain this information about the call site (for logging, testing, or debugging purposes) must use macros so that predefined macros like __LINE__ and __FILE__ are expanded in the context of the caller. The source_location class provides a better alternative.
0
#include <iostream>
#include <vector>
#include <thread>
int f()
{
static int i = 0;
synchronized { // begin synchronized block
std::cout << i << " -> ";
++i; // each call to f() obtains a unique value of i
std::cout << i << '\n';
return i; // end synchronized block
}
}
int main()
{
std::vector<std::thread> v(10);
for(auto& t: v)
t = std::thread([]{ for(int n = 0; n < 10; ++n) f(); });
for(auto& t: v)
t.join();
}
0 -> 1
1 -> 2
2 -> 3
...
99 -> 100
https://en.cppreference.com/w/cpp/language/transactional_memory
−1
#include "property.hpp"
class person {
public:
person() = default;
~person() = default;
SETTER_PRIM(int, id);
SETTER_FLAG(bool, merried);
SETTER_ENUM(human, type);
SETTER_PTR(int, next);
SETTER_ARR(std::string, address, 3);
SETTER_OBJ_LR(std::string, name);
SETTER_OBJ_CLR(std::string, name);
SETTER_OBJ_RR(std::string, name);
GETTER_PRIM(int, id);
GETTER_FLAG(bool, merried);
GETTER_ENUM(human, type);
GETTER_OBJ_LR(std::string, name);
GETTER_OBJ_CLR(std::string, name);
GETTER_PTR(int, next);
GETTER_ARR(std::string, address);
private:
int id;
human type;
std::string name;
std::string address[5];
bool merried;
int* next;
};
https://habr.com/ru/post/459212/
0
template<typename Event>
void bindEvent(auto function)
{
connectionGuards.emplace_back(
std::make_unique<event::EventConnectionGuard<Event>>(
std::forward(event::emitter().on<Event>(boost::bind(
boost::mem_fn(function), static_cast<T*>(this), _1))
)
)
);
}
0
#ifndef EXCHANGECLIENTBINANCE_H
#define EXCHANGECLIENTBINANCE_H
#include "exchangeclient.h"
#include <QScopedPointer>
#include <QString>
class ExchangeClientBinancePrivate;
class ExchangeClientBinance : public ExchangeClient<ExchangeClientBinance>
{
QScopedPointer<ExchangeClientBinancePrivate> d_ptr;
public:
ExchangeClientBinance() noexcept;
~ExchangeClientBinance();
void connect(const ConnectionCredentials& credentials) noexcept;
};
template<>
struct ExchangeClient<ExchangeClientBinance>::ConnectionCredentials
{
QString apiKey;
QString secretKey;
};
#endif // EXCHANGECLIENTBINANCE_H
0
void remove_spaces(string data)
{ int i=0,j=0;
while(i<data.length())
{
if (isalpha(data[i]))
{
data[i]=data[i];
i++;
}
else
{
data.erase(i,1);}
}
cout<<data;
}
0
auto&& rv = elements | std::ranges::views::values | std::ranges::views::transform([](auto&& a) -> auto
{
StatisticsElementMultiple n = std::move(a);
n.nanosec /= n.count;
return n;
});
std::vector<StatisticsElementMultiple> el(std::begin(rv), std::end(rv));
std::ranges::sort(el, [](auto&& a, auto&& b) -> bool {return a.nanosec > b.nanosec; });
for(auto&& [nanosec, name, prefixes, count] : el)
{
printf("%-10d %04X %12s %6d\n", count, prefixes, name.c_str(), nanosec);
}