- 1
- 2
- 3
- 4
- 5
- 6
#include <iostream>
int main(){
int std = 10;
std::cout << std << std::endl;
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+17
#include <iostream>
int main(){
int std = 10;
std::cout << std << std::endl;
}
+13
#include <iostream>
using namespace std;
// -- robot`s internal closed implementation --
int data1[] = { 0, 1, 2 }; char data2[] = { 42 };
// -- robot`s API
const int *GetMechaData1() { return data1; }
size_t GetMechaData1Size() { return 3; }
const char *GetMechaData2(){ return data2; }
size_t GetMechaData2Size() { return 1; }
// -- pentagon`s internal closed implementation --
//--------------------если T равно U, то результат будет D, а иначе - G------------//
template<class T, class U, class D, class G> struct SelectIF { typedef G type; };
template<class T, class D, class G> struct SelectIF<T, T, D, G> { typedef D type; };
// -- pentagon`s API
enum { eMAXBUFER = 200 * sizeof(int) };
template<class T, size_t N>void AcceptData(const T(&src)[N])
{
typedef typename SelectIF<T, char, int, T>::type Cast;
enum { is_char = std::is_same<T,char>::value };
cout << "received data:\n";
const size_t num = (N<eMAXBUFER) ? N : eMAXBUFER;
if (is_char)
for (size_t n = 0; n < num; ++n)
cout << "char code = " << (Cast)src[n] << " : char = '" << src[n] << "'\n";
else
for (size_t n = 0; n<num; ++n)
cout << "item = " << src[n] << endl;
}
// client code
template<class T, size_t N> struct Adapter
{
typedef Adapter<T, N + 1> Next;
void Pass(const T* data, const size_t num)
{
if (N < num)
{
Next().Pass(data, num);
return;
}
T(&arr)[N] = reinterpret_cast< T(&)[N] > (mBuf);
for (size_t n = 0; n< N; ++n) arr[n] = data[n];
AcceptData(arr);
}
size_t mLen;
T mBuf[eMAXBUFER];
};
template<class T> struct Adapter<T, eMAXBUFER>
{
void Pass(const T* data, const size_t)
{
for (size_t n = 0; n< eMAXBUFER; ++n) mBuf[n] = data[n];
AcceptData(mBuf);
}
size_t mLen;
T mBuf[eMAXBUFER];
};
Adapter<int, 1> adapter1;
Adapter<char, 1> adapter2;
int main()
{
{
const auto data = GetMechaData1();
const auto num = GetMechaData1Size();
adapter1.Pass(data, num);
}
{
const auto data = GetMechaData2();
const auto num = GetMechaData2Size();
adapter2.Pass(data, num);
}
}
Крестушки раскрестушились, а подраться не решились.
Под катом ещё несколько вариантов.
+124
static class FuncExtention
{
public static Func<T1, Func<T2, T3>> Carry<T1, T2, T3>(this Func<T1, T2, T3> func)
{
return t1 => t2 => func(t1, t2);
}
public static Func<T1, Func<T2, Func<T3, T4>>> Carry<T1, T2, T3, T4>(this Func<T1, T2, T3, T4> func)
{
return t1 => t2 => t3 => func(t1, t2, t3);
}
public static Func<T1, Func<T2, Func<T3, Func<T4, T5>>>> Carry<T1, T2, T3, T4, T5>(this Func<T1, T2, T3, T4, T5> func)
{
return t1 => t2 => t3 => t4 => func(t1, t2, t3, t4);
}
public static Func<T1, Func<T2, Func<T3, Func<T4, Func<T5, T6>>>>> Carry<T1, T2, T3, T4, T5, T6>(this Func<T1, T2, T3, T4, T5, T6> func)
{
return t1 => t2 => t3 => t4 => t5 => func(t1, t2, t3, t4, t5);
}
public static Func<T1, Func<T2, Func<T3, Func<T4, Func<T5, Func<T6, T7>>>>>> Carry<T1, T2, T3, T4, T5, T6, T7>(this Func<T1, T2, T3, T4, T5, T6, T7> func)
{
return t1 => t2 => t3 => t4 => t5 => t6 => func(t1, t2, t3, t4, t5, t6);
}
public static Func<T1, Func<T2, Func<T3, Func<T4, Func<T5, Func<T6, Func<T7, T8>>>>>>> Carry<T1, T2, T3, T4, T5, T6, T7, T8>(this Func<T1, T2, T3, T4, T5, T6, T7, T8> func)
{
return t1 => t2 => t3 => t4 => t5 => t6 => t7 => func(t1, t2, t3, t4, t5, t6, t7);
}
public static Func<T1, Func<T2, Func<T3, Func<T4, Func<T5, Func<T6, Func<T7, Func<T8, T9>>>>>>>> Carry<T1, T2, T3, T4, T5, T6, T7, T8, T9>(this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9> func)
{
return t1 => t2 => t3 => t4 => t5 => t6 => t7 => t8 => func(t1, t2, t3, t4, t5, t6, t7, t8);
}
public static Func<T1, Func<T2, Func<T3, Func<T4, Func<T5, Func<T6, Func<T7, Func<T8, Func<T9, T10>>>>>>>>> Carry<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> func)
{
return t1 => t2 => t3 => t4 => t5 => t6 => t7 => t8 => t9 => func(t1, t2, t3, t4, t5, t6, t7, t8, t9);
}
public static Func<T1, Func<T2, Func<T3, Func<T4, Func<T5, Func<T6, Func<T7, Func<T8, Func<T9, Func<T10, T11>>>>>>>>>> Carry<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> func)
{
return t1 => t2 => t3 => t4 => t5 => t6 => t7 => t8 => t9 => t10 => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10);
}
public static Func<T1, Func<T2, Func<T3, Func<T4, Func<T5, Func<T6, Func<T7, Func<T8, Func<T9, Func<T10, Func<T11, T12>>>>>>>>>>> Carry<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> func)
{
return t1 => t2 => t3 => t4 => t5 => t6 => t7 => t8 => t9 => t10 => t11 => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11);
}
public static Func<T1, Func<T2, Func<T3, Func<T4, Func<T5, Func<T6, Func<T7, Func<T8, Func<T9, Func<T10, Func<T11, Func<T12, T13>>>>>>>>>>>> Carry<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>(this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> func)
{
return t1 => t2 => t3 => t4 => t5 => t6 => t7 => t8 => t9 => t10 => t11 => t12 => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12);
}
public static Func<T1, Func<T2, Func<T3, Func<T4, Func<T5, Func<T6, Func<T7, Func<T8, Func<T9, Func<T10, Func<T11, Func<T12, Func<T13, T14>>>>>>>>>>>>> Carry<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14>(this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> func)
{
return t1 => t2 => t3 => t4 => t5 => t6 => t7 => t8 => t9 => t10 => t11 => t12 => t13 => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13);
}
public static Func<T1, Func<T2, Func<T3, Func<T4, Func<T5, Func<T6, Func<T7, Func<T8, Func<T9, Func<T10, Func<T11, Func<T12, Func<T13, Func<T14, T15>>>>>>>>>>>>>> Carry<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>(this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> func)
{
return t1 => t2 => t3 => t4 => t5 => t6 => t7 => t8 => t9 => t10 => t11 => t12 => t13 => t14 => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14);
}
public static Func<T1, Func<T2, Func<T3, Func<T4, Func<T5, Func<T6, Func<T7, Func<T8, Func<T9, Func<T10, Func<T11, Func<T12, Func<T13, Func<T14, Func<T15, T16>>>>>>>>>>>>>>> Carry<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>(this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> func)
{
return t1 => t2 => t3 => t4 => t5 => t6 => t7 => t8 => t9 => t10 => t11 => t12 => t13 => t14 => t15 => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15);
}
public static Func<T1, Func<T2, Func<T3, Func<T4, Func<T5, Func<T6, Func<T7, Func<T8, Func<T9, Func<T10, Func<T11, Func<T12, Func<T13, Func<T14, Func<T15, Func<T16, T17>>>>>>>>>>>>>>>> Carry<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17>(this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17> func)
{
return t1 => t2 => t3 => t4 => t5 => t6 => t7 => t8 => t9 => t10 => t11 => t12 => t13 => t14 => t15 => t16 => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16);
}
}
А что поделать то....
+7
#include <iostream>
#include <alloca.h>
#include <stdlib.h>
#include <new>
using namespace std;
int main(void) {
const size_t N = 5+rand()%4;
char* arr = ::new (alloca(N)) char[N]{1,2,3,4};
for(size_t i=0; i<N; ++i)
cout<<(int)arr[i]<<endl;
cout<<"ok";
return 0;
}
http://ideone.com/pax1TF
+5
auto q = db.exec("select name from sqlite_master where type='table'");
QStringList tables;
while(q.next()) {
tables.append(q.value(0).toString());
}
if(tables.contains("searchIndex")) {
types.insert(name, DASH);
} else {
types.insert(name, ZDASH);
}
Табличек в sqlite скорее всего мало и код должен работать довольно шустро.
Но сам подход достоен QHP
https://github.com/jkozera/zeal/blob/master/zeal/zealdocsetsregistry.cpp#L27
+15
#include <vector>
#include <iostream>
using namespace std;
// -- tools
// макет времени компиляции определяет количество элементов массива указанного в аргументе
template<class T, size_t N> char (&Size( T (&arr)[N] ) )[N];
// -- internal closed implementation --
int data1[] = {0, 1, 2};
char data2[] = {42};
const int ( &GetMechaData1() ) [ sizeof( Size(data1) ) ] { return data1; }
const char( &GetMechaData2() ) [ sizeof( Size(data2) ) ] { return data2; }
// -- API
//--- функции возвращают ссылки на массивы: например такого: const int[ sizeof( Size(data1) ]
const int ( &GetMechaData1() ) [ sizeof( Size(data1) ) ];
const char( &GetMechaData2() ) [ sizeof( Size(data2) ) ];
// client code
template<class T, size_t N> void ViewArray(const T (&arr)[N])
{
cout<<"data stored: \n";
for( auto& item: arr )
cout<< "item : "<<item<<endl;
}
int main()
{
ViewArray( GetMechaData1() );
ViewArray( GetMechaData2() );
return 0;
}
http://rextester.com/AEINWM88529
+64
#include <iostream>
#include <typeinfo>
struct Test {};
int main()
{
std::cout << typeid(int).name() << ", " << typeid(Test).name() << std::endl;
}
Очередные КРЕСТОПРОБЛЕМЫ.
MSVC: int, struct Test
GCC: i, 4Test
http://ideone.com/KPsIlP
Вот что говорит стандарт:
The class type_info describes type information generated by the implementation. Objects of this class effectively store a pointer to a name for the type, and an encoded value suitable for comparing two types for equality or collating order. The names, encoding rule, and collating sequence for types are all unspecified and may differ between programs.
RTTI ещё бесполезнее, чем я думала.
+125
http://coliru.stacked-crooked.com/a/a5ba510a2075393f
Под другим ником опубликовал чуть раньше:
http://govnokod.ru/12937
+132
rc = system
(
"test "
"`ls -1 $WORKDIR/somedir/ | wc -l` = 1"
" -a "
"`ls -1 $WORKDIR/somedir/*/somefiles.* | wc -l` = 1"
);
ASSERTM( rc != -1, "check for number of <dirs>" );
ASSERTM( rc == 0, "number of <some> files is greater than 1" );
по мотивам http://govnokod.ru/14374
из теста. да, можно было на С написать. да, мне было просто лень.
+128
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<dict>
<key>author</key>
<string>Nobody</string>
<key>name</key>
<string>my-theme</string>
<key>settings</key>
<array>
<!-- ... -->
<dict>
<key>name</key>
<string>Attribute</string>
<key>scope</key>
<string>entity.other.attribute-name</string>
<key>settings</key>
<dict>
<key>fontStyle</key>
<string></string>
<key>foreground</key>
<string>#006E28</string>
</dict>
</dict>
<!-- ... -->
</array>
</dict>
</plist>
XML. Такой загадочный и энтерпрайзный.
// Цветовая схема для Sublime Text (или TextMate)