- 1
data Int = -33554432 | -33554431 | ... |-2 | -1 | 0 | 1 | 2 | 3 | ... | 33554431 | 33554432
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
Всего: 223
+129
data Int = -33554432 | -33554431 | ... |-2 | -1 | 0 | 1 | 2 | 3 | ... | 33554431 | 33554432
+58
template <typename T>
class MySharedPtr{
public:
explicit MySharedPtr(T* obj) : _obj(obj){}
// --> я дописал
MySharedPtr(const MySharedPtr& other) : _obj(other._obj){ inc_ref_count(_obj);}
// <-- я дописал
MySharedPtr& operator=(const MySharedPtr& other) {
// --> я дописал
if (this == &other)
return *this;
// <-- я дописал
_obj = other._obj;
inc_ref_count(_obj);
}
~MySharedPtr(){
dec_ref_count(_obj);
}
private:
static void inc_ref_count(T* obj){
std::lock_guard<std::mutex> lock(_mutex);
_ref_count[obj] ++ ;
}
static void dec_ref_count(T* obj){
std::lock_guard<std::mutex> lock(_mutex);
if (--_ref_count[obj]){
delete obj;
_ref_count.erase(_ref_count.find(obj));
}
}
T* _obj;
static std::mutex MySharedPtr<T>::_mutex;
static std::map<T*,int> MySharedPtr<T>::_ref_count;
};
template <typename T>
std::map<T*,int> MySharedPtr<T>::_ref_count;
template <typename T>
std::mutex MySharedPtr<T>::_mutex;
сегодня приходил чел-выпускник, написал на листочке shared_ptr, какое ваше мнение?
+127
import std.stdio;
struct Vector
{
private static bool checkOpDispatch(in string str)
{
if(str.length != 4) return false;
foreach(c; str)
{
if(c != 'x' && c != 'y' && c != 'z' && c != 'w') return false;
}
return true;
}
float x,y,z,w;
@property auto opDispatch(string s)() const if(checkOpDispatch(s))
{
return Vector(mixin(s[0..1]),
mixin(s[1..2]),
mixin(s[2..3]),
mixin(s[3..4]));
}
void print() const
{
writefln("Vector: %f, %f, %f, %f", x, y, z, w);
}
}
void main()
{
//vector swizzling
Vector v = {1,2,3,4};
v.print();
auto v1 = v.wzyx;
v1.print();
auto v2 = v.xyxy;
v2.print();
}
http://ideone.com/bfA9gI
+63
#include <iostream>
using namespace std;
int main() {
// your code goes here
float f = 267.0f;
unsigned char c = f;
cout << (int)c << endl;
return 0;
}
Кресты помогают обнаруживать ошибки в логике программы. За это Страуструпу респект и уважуха.
http://ideone.com/V9rgSC
+131
#include <stdio.h>
#include <stdlib.h>
/*
Declare array of functions that return array of functions with
one parameter - function accepting array of functions returning
a pointer to function void(void). No typedefs.
Decoded to the following:
1) An array of E.
2) E = a function that takes void and returns an array of D (returns an array taken to mean a pointer to D).
3) D = a function that takes C and returns void.
4) C = a function that takes an array of B and returns void.
5) B = a function that takes void and returns A.
6) A = a pointer to void(void).
*/
/* Using typedefs */
typedef void (*tA) (void);
typedef tA (*tB) (void);
typedef void (*tC) (tB b[]);
typedef void (*tD) (tC c);
typedef tD* (*tE) (void);
tE tArray[2];
/* Not using typedefs */
void (*_A) (void);
void (* (*_B) (void) ) (void);
void (*_C) ( void (* (*_B[]) (void) ) (void) );
void (*_D) ( void (*_C) ( void (* (*_B[]) (void) ) (void) ) );
void (** (*_E) (void) ) ( void (*_C) ( void (* (*_B[]) (void) ) (void) ) );
void (** (*_Array[2]) (void) ) ( void (*_C) ( void (* (*_B[]) (void) ) (void) ) );
/* Some concrete functions for testing */
void fA(void)
{
printf("fA\n");
}
tA fB(void)
{
printf("fB\n");
return &fA;
}
void fC(tB b[])
{
tA _a;
printf("fC\n");
_a = (*b[0])();
(*_a)();
}
void fD(tC c)
{
tB b[1];
printf("fD\n");
b[0] = &fB;
(*c)(b);
}
tD* fE(void)
{
tD *d;
printf("fE\n");
d = malloc(sizeof(tD));
d[0] = &fD;
return d;
}
int main()
{
tA _a;
tB _b;
tC _c;
tD _d;
tE _e;
tB b[1];
tD *d;
_a = &fA;
_A = &fA;
printf("_a\n");
(*_a)();
printf("_A\n");
(*_A)();
_b = &fB;
_B = &fB;
printf("_b\n");
_a = (*_b)();
(*_a)();
printf("_B\n");
_a = (*_B)();
(*_a)();
_c = &fC;
_C = &fC;
b[0] = _b;
printf("_c\n");
(*_c)(b);
printf("_C\n");
(*_C)(b);
_d = &fD;
_D = &fD;
printf("_d\n");
(*_d)(&fC);
printf("_D\n");
(*_D)(&fC);
_e = &fE;
_E = &fE;
printf("_e\n");
d = (*_e)();
(*d[0])(&fC);
free(d);
printf("_E\n");
d = (*_E)();
(*d[0])(&fC);
free(d);
printf("tArray\n");
tArray[0] = &fE;
d = (*tArray[0])();
(*d[0])(&fC);
free(d);
printf("_Array\n");
_Array[0] = &fE;
d = (*_Array[0])();
(*d[0])(&fC);
free(d);
return 0;
}
+128
mixin template GenerateAutoDispose()
{
void dispose()
{
foreach_reverse(i,t;this.tupleof)
{
static if(staticIndexOf!(auto_dispose,__traits(getAttributes, this.tupleof[i])) != -1)
{
static if(isArray!(typeof(t)))
{
foreach(t1;t)
{
if(t1 !is null)
{
t1.dispose();
}
}
}
else
{
if(t !is null)
{
t.dispose();
}
}
}
}
}
}
http://pastebin.com/2x2k7ngR
+56
#include <iostream>
using namespace std;
int main() {
int i = 5;
int* p1 = &i;
volatile int* p2 = &i;
cout << p1 << endl;
cout << p2 << endl;
return 0;
}
http://ideone.com/hpw4CB
+51
#include <iostream>
using namespace std;
int main() {
const int ci = 42;
auto f = [ci]() mutable { std::cout << ++ci << '\n'; };
f();
return 0;
}
http://ideone.com/0P72sN
А слона то я и не приметил.
+42
#include <time.h>
#include <string>
#include <iostream>
#include <functional>
using namespace std::placeholders;
class F
{
int inc;
public:
F( int inc_v ): inc( inc_v ) {};
int calc( int x )
{
return x + inc;
};
};
F a1( -10 );
F a2( 11 );
int f1( int x )
{
return x - 10;
};
int f2( int x )
{
return x + 11;
};
struct my_ftor
{
F *obj;
int (F::*meth)(int);
int operator()(int x)
{
return (obj->*meth)( x );
};
my_ftor() {};
my_ftor( F *x, int(F::*y)(int) ) : obj(x), meth(y) {};
};
template<typename functor_type>
void test( std::function<functor_type(int)> filler, char *name )
{
const int size = 1000;
const int iters = 10000;
int beg_time, end_time;
functor_type funcs[ size ];
beg_time = clock();
for ( int i = 0; i < iters; i++ )
{
for ( int j = 0; j < size; j++ )
{
funcs[ j ] = filler(j);
}
};
end_time = clock();
float creation = ((float)(end_time - beg_time) / CLOCKS_PER_SEC);
beg_time = clock();
int res = 0;
for ( int i = 0; i < iters; i++ )
{
for ( int j = 0; j < size; j++ )
{
res = funcs[ j ]( res );
};
};
end_time = clock();
float execution = ((float)(end_time - beg_time) / CLOCKS_PER_SEC);
std::cout << name << " creation time: " << creation << " execution time: " << execution << " result: " << res << "\n";
}
int main(int c, char * * v)
{
test<int(*)(int)>( [](int i) {return i % 2 ? f1 : f2; }, "simple &function test" );
test<std::function<int(int)>>( [](int i) {return i % 2 ? f1 : f2; }, "functor &function test" );
test<std::function<int(int)>>( [](int i) {return i % 2 ? std::bind( &F::calc, &a1, _1 ) : std::bind( &F::calc, &a2, _1 ); }, "functor &object test" );
test<my_ftor>( [](int i) {return i % 2 ? my_ftor( &a1, &F::calc ) : my_ftor( &a2, &F::calc ); }, "obj->*meth struct test" );
std::cout << "END\n";
return 0;
}
http://ideone.com/1iNzR
Чем код так долго занимается?
simple &function test creation time: 0.05 execution time: 0.09 result: 5000000
functor &function test creation time: 0.51 execution time: 0.14 result: 5000000
functor &object test creation time: 1.25 execution time: 0.14 result: 5000000
obj->*meth struct test creation time: 0.12 execution time: 0.05 result: 5000000
END
+39
#include <iostream>
#include <memory>
struct Test {
~Test() { std::cout << "~Test\n"; }
};
int main() {
std::shared_ptr<void> ptr( new Test );
return 0;
}
http://ideone.com/xXPWhE
Но:
#include <iostream>
#include <memory>
struct Test
{
~Test() { std::cout << "~Test\n"; }
};
int main() {
std::shared_ptr<void> ptr( (void*) new Test );
return 0;
}
http://ideone.com/jhNvpJ