- 1
- 2
- 3
} catch (...) {
// ¯\_(ツ)_/¯
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+4
} catch (...) {
// ¯\_(ツ)_/¯
}
Ну заебись комментарий
−1
#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <queue>
std::condition_variable messages_cv;
std::mutex messages_mtx;
std::queue<std::string> messages;
std::atomic_bool running = true; // TODO: stop_token when c++20
void pull_messages_thread() {
bool should_run = running;
while (should_run) {
std::unique_lock lock(messages_mtx);
messages_cv.wait(lock, []{ return !messages.empty() || !running; });
if (messages.empty()) return;
auto message = std::move(messages.front());
messages.pop();
should_run = running || !messages.empty();
lock.unlock();
std::cout << "Processing\t" + message + "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(20));
std::cout << "Processed\t" + message + "\n";
}
}
int main() {
std::thread puller(pull_messages_thread);
for (std::size_t i = 0; i < 10; ++i) {
std::string message = "Message<" + std::to_string(i) + ">";
std::unique_lock lock(messages_mtx);
std::cout << "Storing \t" + message + "\n";
messages.emplace(std::move(message));
lock.unlock();
messages_cv.notify_all();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
// std::this_thread::sleep_for(std::chrono::milliseconds(1000));
running = false;
messages_cv.notify_all();
puller.join();
}
/*
Storing Message<0>
Processing Message<0>
Storing Message<1>
Storing Message<2>
Processed Message<0>
Processing Message<1>
Storing Message<3>
Processed Message<1>
Processing Message<2>
Storing Message<4>
Storing Message<5>
Processed Message<2>
Processing Message<3>
Storing Message<6>
Storing Message<7>
Processed Message<3>
Processing Message<4>
Storing Message<8>
Storing Message<9>
Processed Message<4>
Processing Message<5>
Processed Message<5>
Processing Message<6>
Processed Message<6>
Processing Message<7>
Processed Message<7>
Processing Message<8>
Processed Message<8>
Processing Message<9>
Processed Message<9>
*/
−1
#include <iostream>
// Truthiness
struct true_type{static const bool value = true;};
struct false_type{ static const bool value = false;};
// Pick type based on bool value
template <bool B, typename T, typename U>struct conditional{typedef T type;};
template <typename T, typename U>struct conditional<false, T, U>{typedef U type;};
// Compare two types
template <typename T, typename U>struct is_same : public false_type{};
template <typename T>struct is_same<T, T> : public true_type{};
// Division predicate
template <int a, int b>struct divides{static const bool value = (a % b == 0);};
// Type traits
template <typename T, int N = 0>
struct fizzbuzz_traits{
typedef T type;
static const int value = N;
};
template <> struct fizzbuzz_traits<char*, 0>{typedef char* type;};
// fizzbuzz type initializers
template <bool A, bool B> struct fizzbuzz{};
template <> struct fizzbuzz<true, false> : public fizzbuzz_traits<char*> {static const char* value;};
typedef fizzbuzz<true, false> fizz_type;
const char* fizz_type::value = "fizz"; // static const char* can only be initialized out of line
template <> struct fizzbuzz<true, true> : public fizzbuzz_traits<char*> {static const char* value;};
typedef fizzbuzz<true, true> fizzbuzz_type;
const char *fizzbuzz_type::value = "fizzbuzz";
template <> struct fizzbuzz<false, true> : public fizzbuzz_traits<char*> {static const char* value;};
typedef fizzbuzz<false, true> buzz_type;
const char *buzz_type::value = "buzz";
template <> struct fizzbuzz<false, false> : fizzbuzz_traits<void>{};
// FizzBuzz solver
template <int N>
class FizzBuzz{
private:
struct is_divisible : public true_type{
struct by3 : public divides<N, 3>{};
struct by5 : public divides<N, 5>{};
};
public:
typedef fizzbuzz< is_divisible::by3::value, is_divisible::by5::value > solution_type;
// stores string or 'false'
typedef fizzbuzz_traits<int, N> non_divisible_type;
// stores number
typedef typename conditional< !is_same<void, typename solution_type::type>::value,
solution_type,
non_divisible_type>::type print_type;
// stores value to print
};
// Looping from N to M
template <int N, int M>
struct static_for{
static void value(){
std::cout << FizzBuzz<N>::print_type::value << std::endl;
static_for<N + 1, M>::value();
}
};
template <>
struct static_for<101, 100>
{
static void value(){};
};
int main(){
static_for<1, 100>::value();
}
Физзбазз на шаблонах без type_traits
0
// Calc.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//
#include <iostream>
using namespace std;
int main()
{
int one;
int two;
int plus;
int minus;
int nas;
int del;
int zbdel;
cout << "Zadejte prvni cislo: \n";
cin >> one;
cout << "\n";
cout << "Zadejte druhe cislo: \n";
cin >> two;
plus = one + two;
minus = one - two;
nas = one * two;
del = one / two;
zbdel = one % two;
cout << "Scitani: " << plus << "\n";
cout << "Odcitani: " << minus << "\n";
cout << "Nasobeni: " << nas << "\n";
cout << "Deleni: " << del << "\n";
cout << "Zbytek deleni: " << zbdel << "\n";
}
0
#include <stdio.h>
int main(int argc, char* argv[]) {
scanf_s("%s", (char*)argv);
return 1;
}
Нашел у себя в какой-то папке Memecode Junks 2014 года, не помню зачем это там. Но выглядит весело =))
0
typedef struct {
std::shared_ptr<char*>my_arr;
}MyInputs;
...
std::shared_ptr<MyInputs> MainDoc (static_cast<MyInputs*>(malloc(sizeof (*MainDoc))),free);
std::shared_ptr<char*> Z (static_cast<char**>(malloc(sizeof (**MainDoc->my_arr) * 10)),free);
std::shared_ptr<char> Z[0](static_cast<char*>(malloc(sizeof (char *) * 10)),free);
memcpy(Z[0].get(), (char*)"salut\0", 6);
cout << Z[0] << endl;
...
https://stackoverflow.com/questions/27201555/c-array-of-shared-ptr
0
#include <clcpp/clcpp.h>
#include <clcpp/FunctionCall.h>
// Reflect the entire namespace and implement each class
clcpp_reflect(TestClassImpl)
namespace TestClassImpl
{
class A
{
public:
A()
{
x = 1;
y = 2;
z = 3;
}
int x, y, z;
};
struct B
{
B()
{
a = 1.5f;
b = 2.5f;
c = 3.5f;
}
float a, b, c;
};
}
clcpp_impl_class(TestClassImpl::A)
clcpp_impl_class(TestClassImpl::B)
void TestConstructorDestructor(clcpp::Database& db)
{
const clcpp::Class* ca = clcpp::GetType<TestClassImpl::A>()->AsClass();
const clcpp::Class* cb = clcpp::GetType<TestClassImpl::B>()->AsClass();
TestClassImpl::A* a = (TestClassImpl::A*)new char[sizeof(TestClassImpl::A)];
TestClassImpl::B* b = (TestClassImpl::B*)new char[sizeof(TestClassImpl::B)];
CallFunction(ca->constructor, a);
CallFunction(cb->constructor, b);
CallFunction(ca->destructor, a);
CallFunction(cb->destructor, b);
delete [] (char*) a;
delete [] (char*) b;
}
https://github.com/Celtoys/clReflect/blob/master/src/clReflectTest/TestClassImpl.cpp
−1
/**
* @throw std::system_error
*/
auto udp_echo_service(int64_t sd) -> no_return_t {
sockaddr_in remote{};
io_work_t work{};
io_buffer_t buf{}; // memory view to the 'storage'
io_buffer_reserved_t storage{}; // each coroutine frame contains buffer
while (true) {
// packet length(read)
auto len = co_await recv_from(sd, remote, buf = storage, work);
// instead of length check, see the error from the 'io_work_t' object
if (work.error())
goto OnError;
buf = {storage.data(), static_cast<size_t>(len)};
len = co_await send_to(sd, remote, buf, work);
if (work.error())
goto OnError;
assert(len == buf.size_bytes());
}
co_return;
OnError:
// expect ERROR_OPERATION_ABORTED (the socket is closed in this case)
const auto ec = work.error();
const auto emsg = system_category().message(ec);
fputs(emsg.c_str(), stderr);
}
https://github.com/luncliff/coroutine/blob/main/test/net_socket_udp_echo.cpp
0
bool addPlayer(const Addr & addr,
Poco::Nullable<const std::string> serverAddr,
Poco::Nullable<bool> isKeyReceived,
Poco::Nullable<std::string> key,
Poco::Nullable<time_t> lastHashCheck,
Poco::Nullable<std::string> digest)
{
bool isPlaying = !serverAddr.isNull();
bool isKeyReceivedReal = isKeyReceived.isNull();
time_t lastHashCheckReal = lastHashCheck.isNull() ? time(0) : lastHashCheck.value();
std::string keyReal(key.isNull() ? "" : key.value());
std::string playerAddr = addr.getHost();
std::string serverAddrReal(serverAddr.isNull() ? "" : serverAddr.value());
std::string digestReal = digest.isNull() ? "" : digest.value();
Statement insert(*playersSession);
insert << "INSERT INTO Players VALUES(?, ?, ?, ?, ?, ?, ?)",
use(playerAddr), // Addr
use(serverAddrReal), // Server
use(isPlaying),
use(isKeyReceivedReal),
use(keyReal), // Key
use(lastHashCheckReal),
use(digestReal);
insert.execute();
return true;
}
0
#define MIRAGE_COFU(T, name, ...) \
inline struct _##name##cofu { T instance{ __VA_ARGS__ }; T& operator()(void) { return instance; }; \
static bool destructed; ~_##name##cofu(void) { destructed = true; } static bool isDestructed(void) \
{ return destructed; } } name; inline bool _##name##cofu::destructed = false
кофу