1. C++ / Говнокод #12061

    +16

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    void keyStart(int key,void (*func)())
    {
      while(SDL_PollEvent(&keyEvent))
      {
        switch(keyEvent.type)
        {
          case SDL_KEYDOWN:
            switch(keyEvent.key.keysym.sym)
            {
              /*Выход*/
              case SDLK_ESCAPE:
              func();
              break;
              case SDLK_w:
              func();
              break;
              case SDLK_q:
              func();
              break;
              default:
              break;
            }
          break;
          default:
          break;
        }
      }
          
    }

    Функция высшего порядка, мужики. Сами знаете откуда.

    LispGovno, 06 Ноября 2012

    Комментарии (11)
  2. C++ / Говнокод #12047

    +26

    1. 1
    2. 2
    3. 3
    // ...
    c=getchar();
    if(c==*" ") cout << "space";

    Tanger, 02 Ноября 2012

    Комментарии (7)
  3. C++ / Говнокод #12038

    +23

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    94. 94
    95. 95
    96. 96
    97. 97
    98. 98
    99. 99
    #include <iostream>
    #include <stdlib.h>
    #include <typeinfo>
     
    using namespace std;
     
    #define ololo for(volatile register int i=0;i<10;++i);
     
    struct VB
    {
            virtual void f() const =0;
    };
     
    class V1: public VB
    {
            void f() const {ololo}
    };
     
    class V2: public VB
    {
            void f() const {ololo}
    };
     
    struct TU1
    {
            inline void f() const {ololo}
    };
     
    struct TU2
    {
            inline void f() const {ololo}
    };
     
    struct TUB
    {
            const type_info* type;
            union 
            {
                    TU1 tu1;
                    TU2 tu2;
            };
     
            template<class T>
            void ctor()
            {
                    this->type=&typeid(T);
            }
            
            template<class T>
            inline void call(const T& f)
            {
                    if(this->type==&typeid(TU1))
                            f(this->tu1);
                    else
                            f(this->tu2);
            }
    };
     
    enum {N=1000, N2=N*50};
     
    int main() {
            cout<<"ok"<<endl;
            {
                    VB*v[N];
                    for(int i=0;i<N;++i)
                            if(rand()%2)
                                    v[i]=new V1;
                            else
                                    v[i]=new V2;
                    volatile clock_t a=clock();
                    for(int j=0;j<N2;++j)
                            for(int i=0;i<N;++i)
                                    v[i]->f();
                    volatile clock_t b=clock();
                    cout<< (double)(b - a) / CLOCKS_PER_SEC<<endl;
            }
            cout<<"ok"<<endl;
            {
                    TUB v[N];
                    for(int i=0;i<N;++i)
                            if(rand()%2)
                                    v[i].ctor<TU1>();
                            else
                                    v[i].ctor<TU2>();
                    struct Continuation
                    {
                            inline void operator()(const TU1& a) const {a.f();}
                            inline void operator()(const TU2& a) const {a.f();}
                    } cps;
                    volatile clock_t a=clock();
                    for(int j=0;j<N2;++j)
                            for(int i=0;i<N;++i)
                                    v[i].call(cps);
                    volatile clock_t b=clock();
                    cout<< (double)(b - a) / CLOCKS_PER_SEC<<endl;
            }
            cout<<"ok"<<endl;
            return 0;
    }

    http://ideone.com/plFaLM
    Тут в соседней теме разгорелся спор, что быстрее - полиморфизм виртуальных функций или полиморфизм tagget union. По сути последнее - выбор по if нужной виртуальной функции. Говорят в Java быстрее второе.
    Тема родилась из http://govnokod.ru/12025#comment158188
    Получилось по результатам измерений:
    Виртуальные функции: 1.8 секунд.
    tagget union: 1.94 секунд.
    Притом это всего 2 полиморфных типа в tagget union, а если рост числа полиморфных классов будет расти, то разрыв между виртуальными функциями и tagget union только увеличится. Притом производительность tagget union будет только падать.
    Тема поднята ещё со взглядом на функциональные языки. Это ведь там так модны ADT с постоянным внутри ifподобным паттернматчингом по ним.
    Жду указания на косяки или способы поднять производительность tagget union.

    LispGovno, 31 Октября 2012

    Комментарии (94)
  4. C++ / Говнокод #12023

    +20

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    void __fastcall TfРѕrm1::SpeedButton1Click(TobjРµct *Sender)
    {
    SpeedButton1->Enabled=false;
     SpeedButton1->Caption=Edit1->Text;
     if(Edit1->Text=="X")
     Edit1->Text="0";
     else
     Edit1->Text="X";
     if(SpeedButton1->Caption=="X" && SpeedButton2->Caption=="X" && SpeedButton3->Caption=="X" ||
     SpeedButton4->Caption=="X" && SpeedButton5->Caption=="X" && SpeedButton6->Caption=="X" ||
     SpeedButton7->Caption=="X" && SpeedButton8->Caption=="X" && SpeedButton9->Caption=="X" ||
     SpeedButton1->Caption=="X" && SpeedButton4->Caption=="X" && SpeedButton7->Caption=="X" ||
      SpeedButton2->Caption=="X" && SpeedButton5->Caption=="X" && SpeedButton8->Caption=="X" ||
       SpeedButton3->Caption=="X" && SpeedButton6->Caption=="X" && SpeedButton9->Caption=="X" ||
        SpeedButton1->Caption=="X" && SpeedButton5->Caption=="X" && SpeedButton9->Caption=="X" ||
         SpeedButton3->Caption=="X" && SpeedButton5->Caption=="X" && SpeedButton7->Caption=="X")
    
      ShowMessage("Ea?a caeii?aia, e?anoeee auea?aee");
    
      else
       if(SpeedButton1->Caption=="0" && SpeedButton2->Caption=="0" && SpeedButton3->Caption=="0" ||
     SpeedButton4->Caption=="0" && SpeedButton5->Caption=="0" && SpeedButton6->Caption=="0" ||
     SpeedButton7->Caption=="0" && SpeedButton8->Caption=="0" && SpeedButton9->Caption=="0" ||
     SpeedButton1->Caption=="0" && SpeedButton4->Caption=="0" && SpeedButton7->Caption=="0" ||
      SpeedButton2->Caption=="0" && SpeedButton5->Caption=="0" && SpeedButton8->Caption=="0" ||
       SpeedButton3->Caption=="0" && SpeedButton6->Caption=="0" && SpeedButton9->Caption=="0" ||
        SpeedButton1->Caption=="0" && SpeedButton5->Caption=="0" && SpeedButton9->Caption=="0" ||
         SpeedButton3->Caption=="0" && SpeedButton5->Caption=="0" && SpeedButton7->Caption=="0")
      ShowMessage("Ea?a caeii?aia, iieeee auea?aee");
    
    }

    jQuery, 29 Октября 2012

    Комментарии (9)
  5. C++ / Говнокод #12019

    +18

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    template <class TBitMap, int MMCROffset>
    class TControllerMemoryMappedRegister: public TBaseControllerMemoryMappedRegister<MMCROffset>
    {
    public:
    	static void set(TBitMap::E Bit) { setBit(Bit); }
    	static void reset(TBitMap::E Bit) { resetBit(Bit); }
    };

    Говногость, 29 Октября 2012

    Комментарии (32)
  6. C++ / Говнокод #12017

    +15

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    #include <iostream>
    #include <cxxabi.h>
    #include <typeinfo>
    
    int main(void)
    {
       uint8_t i = 63;
       int  status;
    
       std::cout << i << std::endl;
    
       char *realname = abi::__cxa_demangle(typeid(i).name(), 0, 0, &status);
       std::cout << "Real type of uint8_t is: " << realname << std::endl;
       delete (realname);
    
       std::cout << (unsigned int)i << std::endl;
    
    return 0;
    }

    ...нативный 8bit-ый беззнаковый int.

    sayidandrtfm, 28 Октября 2012

    Комментарии (33)
  7. C++ / Говнокод #12009

    +16

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    #include <functional>
    
    using namespace std;
    
    class O{};
    
    class foo
    {
    public:
       constexpr static auto anyGarbage = O(O(O(O())));//:Жаль, что написать auto anyGarbage = O(O(O(O()))); нельзя.
       O anyGarbage2 = O(O(O(O())));
       
    private:
       int var;
       
    public:
       std::function<void(int)> setter=[this](int s){(void)s;/*var=s;*/};
    };

    Я хочу написать свои property, принимающие лямбды в качестве параметра setter и getter. Как сделать friend лямбду?
    http://liveworkspace.org/code/39082e70108502c2e44c4fe6c5762d9a

    USB, 26 Октября 2012

    Комментарии (34)
  8. C++ / Говнокод #11983

    +30

    1. 1
    if( str[0] == '<' || str[1] == '?' || str[2] == 'x' || str[3] == 'm' || str[4] == 'l') {...}

    Проверка на xml-ность

    bazhenovc, 24 Октября 2012

    Комментарии (20)
  9. C++ / Говнокод #11975

    +25

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    template <typename T> struct Pool { static std::vector<T> data; };
    
    std::vector<tbphys::Body> Pool<tbphys::Body>::data;
    std::vector<Ball>         Pool<Ball>        ::data;
    std::vector<Wall>         Pool<Wall>        ::data;
    std::vector<Bat>          Pool<Bat>         ::data;
    std::vector<Brick>        Pool<Brick>       ::data;
    
    template <typename T, typename U> struct List
    {
    	typedef T Head;
    	typedef U Tail;
    };
    
    struct End {};
    
    typedef List<tbphys::Body, List<Brick, List<Bat, List<Ball, List<Wall, End> > > > > Components;
    typedef List<Brick, List<Bat, List<Ball, List<Wall, End> > > > DrawableComponents;
    typedef List<Brick, List<Ball, List<Bat, End> > > MovableComponents;
    
    const size_t badindex = size_t(-1);
    
    struct GameBody
    {
    	size_t self,body,bat,brick,wall,ball;
    
    	template <typename T> size_t& IndexOf ();
    	template <> size_t& IndexOf <tbphys::Body> () {return body;}
    	template <> size_t& IndexOf <Bat  >        () {return bat;}
    	template <> size_t& IndexOf <Brick>        () {return brick;}
    	template <> size_t& IndexOf <Wall >        () {return wall;}
    	template <> size_t& IndexOf <Ball >        () {return ball;}
    
    	template <typename T>
    	T& AddComponent ()
    	{
    		size_t index = Pool<T>::data.size();
    		Pool<T>::data.resize(index+1);
    		T& result = Pool<T>::data[index];
    
    		result.hostIndex = self;	
    		IndexOf<T>() = index;
    		return result;
    	}
    
    	template <typename T> void SetBadIndex ()
    	{
    		IndexOf<T::Head> () = badindex;
    		SetBadIndex<T::Tail> ();
    	}
    	template <> void SetBadIndex <End> () {}
    };

    Сколько тут говнопаттернов крестоблядского крестоязыка?

    TarasB, 22 Октября 2012

    Комментарии (146)
  10. C++ / Говнокод #11967

    +9

    1. 1
    http://ideone.com/vvkWO4

    Fai, 21 Октября 2012

    Комментарии (8)