- 1
for (double x = xn, p = 1, n = 0, y = 0; x > xk ? cout<<p<<' '<<n, false : true; x += dx, y = cos(x) + x*5. / 2, y > 0 ? p *= y : y < 0 ? n++ : n );
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+1
for (double x = xn, p = 1, n = 0, y = 0; x > xk ? cout<<p<<' '<<n, false : true; x += dx, y = cos(x) + x*5. / 2, y > 0 ? p *= y : y < 0 ? n++ : n );
Попросили помочь с задачей:
> Определить произведение негативных и количество позитивных значений функции y=cos(x)+x*5./2, если значение x изменяется от xn до xk с шагом dx.
Я постарался, как мог, всё-таки язык мне не очень близок (немного помогли). Возможно, ещё есть, куда "улучшить".
0
а давайте сравним размеры скомпилированного EXE под Винду и Убунту и посмотрим сколько лет надо ждать когда это Г будет собранно в ЕХЕ файл
windows: (+pdb)
tsc.exe 159M
tsc.pdb 872M
ubuntu: (+debug info)
tsc 1290M (из них debug info 1153M)
а вы знаете сколько лет мне надо ждать что бы скомпилять такие файлы? особенно на убунте?
И какой "ур-д" предлагал мне использовать убунту а не винду :)?
сабж в теме
−1
Если у меня есть приватный метод, то зачем мне его объявлять в .h-файле?
Хочу только в .cpp написать вспомогательный метод и только там его использовать.
А вынужден копипастить сигнатуру ещё и в .h.
+4
struct Foo {
};
struct Bar {
int x[0];
};
https://ideone.com/upuoOg
Какая консистентность )))
0
const int sum(int a, int b){
return a+b;
}
int a(int x)
{
const std::function<int(int)> sum4 = std::bind(sum,_1, 4);
return sum4(123);
}
int b(int x)
{
puts("bagor");
const std::function<int(int)> sum4 = std::bind(sum,_1, 4);
return sum4(123);
}
int c(int x)
{
const std::function<int(int)> sum4 = std::bind(sum,_1, 4);
puts("bagor");
return sum4(123);
}
// Функции a и b нормально инлайнятся. Ассемблерный выхлоп:
sum(int, int): # @sum(int, int)
lea eax, [rdi + rsi]
ret
a(int): # @a(int)
mov eax, 127
ret
b(int): # @b(int)
push rax
mov edi, offset .L.str
call puts
mov eax, 127
pop rcx
ret
//А вот int c(int x)
c(int): # @c(int)
push rbx
sub rsp, 32
mov edi, 16
call operator new(unsigned long)
mov rbx, rax
mov qword ptr [rax], offset sum(int, int)
mov dword ptr [rax + 8], 4
mov qword ptr [rsp], rax
mov qword ptr [rsp + 24], offset std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_invoke(std::_Any_data const&, int&&)
mov qword ptr [rsp + 16], offset std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_manager(std::_Any_data&, std::_Any_data const&, std::_Manager_operation)
mov edi, offset .L.str
call puts
mov esi, dword ptr [rbx + 8]
mov edi, 123
call qword ptr [rbx]
mov ebx, eax
mov rax, qword ptr [rsp + 16]
test rax, rax
je .LBB3_3
mov rdi, rsp
mov rsi, rdi
mov edx, 3
call rax
.LBB3_3:
mov eax, ebx
add rsp, 32
pop rbx
ret
mov rdi, rax
call __clang_call_terminate
mov rbx, rax
mov rax, qword ptr [rsp + 16]
test rax, rax
je .LBB3_6
mov rdi, rsp
mov rsi, rdi
mov edx, 3
call rax
.LBB3_6:
mov rdi, rbx
call _Unwind_Resume@PLT
mov rdi, rax
call __clang_call_terminate
__clang_call_terminate: # @__clang_call_terminate
push rax
call __cxa_begin_catch
call std::terminate()
std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_invoke(std::_Any_data const&, int&&): # @std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_invoke(std::_Any_data const&, int&&)
mov rax, qword ptr [rdi]
mov rcx, qword ptr [rax]
mov edi, dword ptr [rsi]
mov esi, dword ptr [rax + 8]
jmp rcx # TAILCALL
std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_manager(std::_Any_data&, std::_Any_data const&, std::_Manager_operation): # @std::_Function_handler<int (int), std::_Bind<int const (*(std::_Placeholder<1>, int))(int, int)> >::_M_manager(std::_Any_data&, std::_Any_data const&, std::_Manager_operation)
Решил попробовать std::bind.
https://godbolt.org/z/eW5eT5oj4
+1
struct file_id
{
uint64_t persistent;
uint64_t volatile_;
static const size_t RAW_LENGTH = 16;
operator std::string() const
{ return std::string(reinterpret_cast<const char*>(&persistent), RAW_LENGTH); }
// Операторы для использования file_id в качестве ключа map и
// unordered_map
struct hash
{
std::size_t operator()(const ntdec_smb2_file_id& file_id) const
{
std::string s_file_id = file_id;
std::hash<std::string> hasher;
return hasher(s_file_id);
}
};
bool operator == (const ntdec_smb2_file_id& other)
{
return std::string(*this) == std::string(other);
}
bool operator < (const ntdec_smb2_file_id& other)
{
return std::string(*this) < std::string(other);
}
bool operator > (const ntdec_smb2_file_id& other)
{
return std::string(*this) > std::string(other);
}
};
operator std::string тоже UB?
+2
// https://github.com/seanbaxter/circle/blob/master/examples/README.md#tldr
// ...
// Circle's primary syntactic element is the @meta keyword, which runs the prefixed statement
// during source translation (or during template instantiation in dependent contexts).
// https://github.com/seanbaxter/circle/blob/master/examples/README.md#same-language-reflection
// duff1.cxx
void duff_copy1(char* dest, const char* source, size_t count) {
const char* end = source + count;
while(size_t count = end - source) {
switch(count % 8) {
case 0: *dest++ = *source++; // Fall-through to case 7
case 7: *dest++ = *source++; // Fall-through to case 6...
case 6: *dest++ = *source++;
case 5: *dest++ = *source++;
case 4: *dest++ = *source++;
case 3: *dest++ = *source++;
case 2: *dest++ = *source++;
case 1: *dest++ = *source++;
break;
}
}
}
// Reproduced above is a simplified version of Duff's device, an infamous memcpy function designed
// to reduce the amount of branching in the operation. (The loop is optimally interleaved with the switch,
// but I'm trying to illustrate some other points and don't want to add to the confusion.) Once we enter the
// switch, perform an assignment and unconditionally progress to the next case statement. This algorithm
// cries out for automation. The case statements have indices that run from 8 down to 1, modulo 8. Can we give it the Circle treatment?
// duff2.cxx
void duff_copy2(char* dest, const char* source, size_t count) {
const char* end = source + count;
while(size_t count = end - source) {
switch(count % 8) {
@meta for(int i = 8; i > 0; --i)
case i % 8: *dest++ = *source++;
break;
}
}
Но гомоиконности таким подкостыливанием вы естественно не добавите!
+1
QSqlQuery& SQLConnect::get()
{
if ( makeConnection() ) {
query = QSqlQuery(mDb);
return query;
}
QSqlQuery empty;
return empty;
}
bool SQLConnect::makeConnection()
{
mDb = SQLConnectPool::Instance().get();
return true;
}
Раньше компилилось и не замечал, а тут на новом компиляторе начал кидать ошибки и решил посмотреть, что же там напроектировали
+1
class Solution {
public:
std::vector<std::vector<int>> diagonalSort(std::vector<std::vector<int>> & mat) {
if (!mat.size()) return mat;
const size_t rl = mat[0].size();
const size_t cl = mat.size();
sort(mat, rl, cl, 0, 0);
for (size_t i = 1; i < rl; ++i) {
sort(mat, rl, cl, 0, i);
}
for (size_t i = 1; i < cl; ++i) {
sort(mat, rl, cl, i, 0);
}
return mat;
}
private:
void sort(std::vector<std::vector<int>> & mat, size_t rl, size_t cl, size_t i, size_t j) {
const size_t len = std::min(rl - j, cl - i);
const size_t endj = j + len;
const size_t endi = i + len;
std::sort(diag_iter<false>{&mat, i, j}, diag_iter<false>{&mat, endi, endj});
}
template <bool isConst>
class diag_iter {
std::vector<std::vector<int>> *base;
size_t i, j;
using T = int;
public:
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = T*;
using reference = typename std::conditional<isConst, const T&, T&>::type;
diag_iter(std::vector<std::vector<int>> *base, size_t i, size_t j) : base(base), i(i), j(j) { }
diag_iter(const diag_iter&) = default;
diag_iter& operator=(const diag_iter&) = default;
~diag_iter() = default;
reference operator*() const { return (*base)[i][j]; }
diag_iter& operator++() { i++; j++; return *this; }
friend bool operator== (const diag_iter& a, const diag_iter& b) { return a.i == b.i && a.j == b.j; };
friend bool operator!= (const diag_iter& a, const diag_iter& b) { return !(a == b); };
pointer operator->() const { return &(this->operator*()); }
diag_iter operator++(int) { diag_iter tmp = *this; ++(*this); return tmp; }
diag_iter() = default;
diag_iter& operator--() { i--; j--; return *this; }
diag_iter operator--(int) { diag_iter tmp = *this; --(*this); return tmp; }
diag_iter& operator+=(difference_type n) { i += n; j += n; return *this; }
friend diag_iter operator+(diag_iter it, difference_type n) { return it += n; }
diag_iter& operator-=(difference_type n) { i -= n; j -= n; return *this; }
diag_iter operator-(difference_type n) const { return diag_iter(*this) -= n; }
friend difference_type operator-(const diag_iter& a, const diag_iter& b) { return (b.j * b.base->size() + b.i) - (a.j * a.base->size() + a.i); }
reference operator[](difference_type n) const { return *(*this + n); }
friend bool operator<(const diag_iter& a, const diag_iter& b) { return b - a > 0; }
friend bool operator>(const diag_iter& a, const diag_iter& b) { return b < a; }
friend bool operator>=(const diag_iter& a, const diag_iter& b) { return !(a < b); }
friend bool operator<=(const diag_iter& a, const diag_iter& b) { return !(a > b); }
};
};
https://leetcode.com/problems/sort-the-matrix-diagonally/
Сортировка через итераторы оказалась примерно в три раза медленнее, чем через копирование в вектор, сортировку его и копирование обратно.
+1
#include <iostream>
#include <functional>
#define STD_FUNCTION(a, ...) typeof( a (*) __VA_ARGS__ )
template<typename T>
T do_op_t(T a, T b, STD_FUNCTION(T,(T,T)) op)
{
return op(a,b);
}
template
<
typename T,
STD_FUNCTION(
T,
(
T,T,
STD_FUNCTION(
T,
(T,T)
)
)
) F1,
STD_FUNCTION(
T,
(T,T)
) F2
>
T do_op_spec(T a, T b)
{
return F1(a, b, F2);
}
int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }
std::function<int(int,int)> fnc = \
do_op_spec\
<
int,
do_op_t<int>,
add
>;
int main()
{
std::cout << do_op_t<int>(9, 9, add) << "\n";
std::cout << do_op_t<int>(9, 9, mul) << "\n";
std::cout << do_op_spec<int, do_op_t<int>,add>(9,9) << "\n";
std::cout << do_op_spec<int, do_op_t<int>,mul>(9,9) << "\n";
std::cout << fnc(9,9) << "\n";
}
Какая крестопараша )))