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

    +21

    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
    template <int N>
    void Ololo ()
    {
    	var
    		i : integer;
    	begin
    		for i := 0 to N-1 do begin
    			WriteLn(i, ' ');
    		end;
    	end;
    }
    
    int main () 
    {
    	return 0;
    }
    
    Compiling...
    Test.cpp
    Linking...
    
    Build log was saved at "file://c:\Users\TarasB\Documents\Visual Studio Projects\Test\Debug\BuildLog.htm"
    Test - 0 error(s), 0 warning(s)
    
    
    ---------------------- Done ----------------------
    
        Build: 1 succeeded, 0 failed, 0 skipped

    MSVS2003

    TarasB, 15 Января 2014

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

    +14

    1. 1
    std::thread_fence(get_current_memory_order());

    LispGovno, 14 Января 2014

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

    +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
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    #include <iostream>
    using namespace std;
    struct T
    {
      int a, b, &c;
      T():a(0), b(1), c(a){cout<<"dc"<<endl;}
      T(const T& a):a(a.a), b(a.b), c(&a.c == &a.a ? this->a : b){cout<<"cc"<<endl;}
      T& operator=(T a)
      {
        ::new((void*)(&b+1)) int*(&a.c == &a.a ? &this->a : &b);
        //asm volatile ("" : : : "memory");
        cout<<"co"<<endl;
        return *this;
      }
      void Switch()
      {
        ::new((void*)(&b+1)) int*(&c == &a ? &b : &a);
        //asm volatile ("" : : : "memory");
        cout<<"sw"<<endl;
      }
    } __attribute__((aligned(1))) ;
    
    int main() {
      T a;
      cout<<a.a<<endl;
      cout<<a.b<<endl;
      cout<<a.c<<endl;
      a.Switch();
      cout<<a.c<<endl;
      T b;
      cout<<b.c<<endl;
      b=a;
      cout<<b.c<<endl;
      b.b=666;
      cout<<b.c<<endl;
      return 0;
    }

    Очевидно откуда это.

    LispGovno, 10 Января 2014

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

    +18

    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
    template <typename T>
      struct canref {
        struct yes { uint8_t bytes[1]; };
        struct no  { uint8_t bytes[2]; };
        template <typename U>    static yes test(U*p);
        template <typename U>    static no  test(...);
        static const bool value = sizeof(test<T>(NULL)) == sizeof(yes);
      };
      template <typename T, int N, bool CanRef=canref<T>::value>
      class array; 
      // class for all types
      template <typename T, int N>
      class array <T,N,true>
      {
        struct Bytes
        {
          uint8_t bytes[sizeof(T)];
        };
        struct TStruct
        {
          T t;
          TStruct(T t):t(t){}
        };
    
        Bytes elements[N];
        int count;
        void copy_ctor (int index, const T& t)
        {
          new (&((*this)[index])) T(t);      
        }
        void copy (int index, const T& t)
        {
          (*this)[index] = t;
        }
    
        void dtor (int index)
        {
          (*this)[index].~T();
        }
      public:
        array () : count(0) {}
    
        ~array () 
        {
          resize(0);
        }
        T& operator [] (int index) 
        {
          assert (index>=0 && index<count);      
          return ((TStruct*)(&elements[index]))->t;
        }
        const T& operator [] (int index) const 
        {
          assert (index>=0 && index<count);      
          return ((TStruct*)(&elements[index]))->t;
        }
        template <int M>
        array (const array<T,M> &a)
        {
          assert(a.count<=N);
          count = a.count;
          for (int i=0; i<count; ++i)
            copyctor(i, a[i]);
        }
        template <int M>
        array& operator = (const array<T,M> &a)
        {
          if (this != &a)
          {
            if (count>a.count)
            {
              for (int i=0;       i<a.count; ++i) copy(i, a[i]);
              for (int i=a.count; i<count;   ++i) dtor(i);
              count = a.count;
            } else
            {
              assert(a.count<=N);
              int ocount = count;
              count = a.count;
              for (int i=0;      i<ocount; ++i) copy(i, a[i]);
              for (int i=ocount; i<count;  ++i) copy_ctor(i, a[i]);
            }
          }
        }
        int size() 
        {
          return count;
        }

    Скоро даже сратору станет очевидно откуда это.

    LispGovno, 10 Января 2014

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

    +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
    struct Point3D {
      float x,y,z;
    
      float& operator [] (int i) {
        switch (i) {
        case 0: return x;
        case 1: return y;
        case 2: return z;
        default: assert(false);
        }
      }
    };

    Писал Жабапоглащенный.

    LispGovno, 08 Января 2014

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

    +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
    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
    // https://github.com/mono/moon/blob/master/src/list.h#L87
    
    class Queue {
    protected:
            MoonMutex lock;
            List *list;
            
    public:
            Queue ();
            ~Queue ();
            
            // convenience properties
            bool IsEmpty ();
            int Length ();
            
            // convenience methods
            void Clear (bool freeNodes);
            
            void Push (List::Node *node);
            List::Node *Pop ();
            
            void Lock ();
            void Unlock ();
            
            // accessing the internal linked list directly requires manual Locking/Unlocking.
            List *LinkedList ();
    
            // copies the queue and empties the original
            void  MoveTo (Queue &queue);
    };
    
    // https://github.com/mono/moon/blob/master/src/list.cpp#L391
    
    Queue::Queue ()
      : lock (true)
    {
            list = new List ();
    }
    
    int
    Queue::Length ()
    {
            int length;
            
            Lock ();
            length = list->Length ();
            Unlock ();
            
            return length;
    }
    
    void
    Queue::MoveTo (Queue &queue)
    {
            List::Node *node;
            while ((node = list->First ())) {
                    list->Unlink (node);
                    queue.Push (node);
            }
    }

    Во имя луны!

    Xom94ok, 07 Января 2014

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

    +27

    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
    int enumDevices(DevInfo* &lst) {
        int count = 0;
        DevInfo* tmp = NULL;
        Device device;
        for (int i = 0; i < MAXDEVICES; i++)
            if (device = OpenDevice(i)) {
                count++;
                realloc(tmp, sizeof(DevInfo)*count);
                ReadInfo(device, &tmp[count-1]
            }
        if (count == 0) return 0;
        lst = new DevInfo[count];
        for (int i = 0; i < count; i++)
            lst[i] = tmp[i];
        free(tmp);
        return count;
    }
    
    //................
    
    DevInfo* list;
    int devcount = enumDevices(list);
    /* работаем со списком */
    delete[] list;

    Самому стыдно.

    Vindicar, 06 Января 2014

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

    +34

    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
    class cxx_query {
        elements operator()(const std::string &css_query);
        void operator()(std::function<void()> callback);
        http_request get(const std::string &url);
        // ...
    } $;
    
    #define function []
    
    $(function() {
        $.get(some_url, function(const std::string &data) {
            $("#result").html(data);
        });
    });

    bormand, 30 Декабря 2013

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

    +17

    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
    #include <boost/spirit/include/phoenix_fusion.hpp>
    #include <boost/fusion/include/adapt_struct.hpp>
    
    #include <iostream>
    
    
    #define GEN_MACRO_0(X, Y)                             \
        ((X, Y)) GEN_MACRO_1
    #define GEN_MACRO_1(X, Y)                             \
        ((X, Y)) GEN_MACRO_0
    #define GEN_MACRO_0_END
    #define GEN_MACRO_1_END
    
    #define SHOW(name,val)                               \
            std::cout << name << " : " << val ;
    
    #define SHOW_TUPLE(r,_,tupple)                       \
           SHOW( "1st",BOOST_PP_TUPLE_ELEM(2,0,tupple))  \
           SHOW("\t2nd",BOOST_PP_TUPLE_ELEM(2,1,tupple)) \
           std::cout << std::endl;
    #define OUTPUT (ADD_PAREN_1 INPUT,_END) 
    
    
    #define SHOW_NODES(seq)                              \
            BOOST_PP_SEQ_FOR_EACH(SHOW_TUPLE,_,          \
                     BOOST_PP_CAT(GEN_MACRO_0 seq,_END) )
    
    int main() {
        SHOW_NODES(
            ("cock","unskill")
            ("rock","hard")
            ("price","over 100500")
            )
    }

    Годнокодец из boost::fusion.
    http://coliru.stacked-crooked.com/a/c516a67930a9c1a8

    crastinus, 29 Декабря 2013

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

    +18

    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
    #include <iostream>
    
    #define PRINT(val) std::cout << #val \
                                 << " value: " << val \
                                 << " address: " << &val << std::endl; 
    int& constRef(){
        int var = 10;
        PRINT(var)
        return var;
    }
    
    int main() {
       const int a = 'a', b = 'bcde';
       std::cout << a << " " << b << std::endl;
       const int& ref = constRef();  
       PRINT(ref)  
    }

    Малоизвестный синтаксис плюсов.
    1. Инициализация целочисленного значения из строки (используется в строках-классах времени компиляции).
    2. Константная ссылка на стековый объект (используется для создания DSL, в т.ч. boost::spirit).
    P.S.: Более полный вариант со строками лежит на форуме.

    crastinus, 28 Декабря 2013

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