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

    +1

    1. 1
    2. 2
    3. 3
    4. 4
    void* createMap() {
        mp = new Map();    //it's a global variable
        return reinterpret_cast<void*> (new Map());
    }

    ooki2day, 10 Августа 2017

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

    +1

    1. 1
    2. 2
    3. 3
    CoolIntf::GetInstance().DoSomething();
    CoolIntf::GetInstance().DoSomethingElse();
    CoolIntf::GetInstance().DoAnything()

    для случая когда GetInstance() не инлайнится, кто-нибудь в крестах какое решение (без ручного введения временной переменной) для такого кода придумал?
    единственное что нашел это вот это: https://stackoverflow.com/a/2279253 .
    потому что "with" слишком общее слово которое в ж не гуглится.

    Dummy00001, 04 Августа 2017

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

    0

    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>
    #include <type_traits>
    #include <functional>
    
    template <typename Function, typename... Args>
    auto call(Function function, Args&&... args) {
        return std::move(function)(std::forward<Args>(args)...);
    }
    
    class Foo {
    public:
        void say(int a) const { std::cout << "Foo::say(int a = " << a << ")\n"; }
    };
    
    int main() {
        call(std::mem_fn(&Foo::say), Foo(), 42);
    }

    Ничего особенного. Просто ЙЦУКЕН!!!!111, как оно вообще работает?

    Elvenfighter, 28 Июля 2017

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

    −2

    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
    #include <iostream>
    #include <typeinfo>
    
    class S
    {
    public:
    	S* _next;
    };
    
    int main (int argc, char **argv)
    {
        for (S* sw1 = new S(), sw2 = sw1->_next;;)
    	{
    		std::cout << typeid(sw1).name() << std::endl;  
    		std::cout << typeid(sw2).name() << std::endl;  
    		break;
    	}
    
        return 0;
    }

    какого хрена этот говнокод не хочет скомпилиться :)

    ASD_77, 26 Июля 2017

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

    0

    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
    #include <iostream>
    
    template < typename T >
    struct Static
    {
        T t;
    };
    
    template < typename T >
    struct Test
    {
        static Static<Test<T>> t;
    };
    
    template < typename T >
    Static< Test<T> > Test<T>::t;
    
    int main (int argc, char **argv)
    {
        Test<int> t;
        return 0;
    }

    Попробуйте скопилять этот код на G++ (даю подсказку - Test is fully defined type - потому что static не в ходит в размер структуры)

    ASD_77, 26 Июля 2017

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

    0

    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
    bool NextLBYUpdate()
    {
    int tick = *(int*)((DWORD)G::LocalPlayer + Offsets.m_nTickBase);
    float flServerTime = (float)(tick * I::Globals->interval_per_tick);
    if (_DEBUGMODE_)
    {
    printf("%f\n", G::LocalPlayer->GetLowerBodyYaw());
    }
    
    if (OldLBY != G::LocalPlayer->GetLowerBodyYaw())
    {
    LBYBreakerTimer++;
    OldLBY = G::LocalPlayer->GetLowerBodyYaw();
    bSwitch = !bSwitch;
    LastLBYUpdateTime = flServerTime;
    }
    
    if (CurrentVelocity(G::LocalPlayer) > 0.5)
    {
    LastLBYUpdateTime = flServerTime;
    return false;
    }
    
    if ((LastLBYUpdateTime + 1 - (GetLatency() * 2) < flServerTime) && (G::LocalPlayer->GetFlags() & FL_ONGROUND))
    {
    if (LastLBYUpdateTime + 1.1 - (GetLatency() * 2) < flServerTime)
    {
    LastLBYUpdateTime += 1.1;
    }
    return true;
    }
    return false;
    }
    float GetLatency()
    {
    INetChannelInfo *nci = I::Engine->GetNetChannelInfo();
    if (nci)
    {
    float Latency = nci->GetAvgLatency(FLOW_OUTGOING) + nci->GetAvgLatency(FLOW_INCOMING);
    return Latency;
    }
    else
    {
    return 0.0f;
    }
    }
    float GetOutgoingLatency()
    {
    INetChannelInfo *nci = I::Engine->GetNetChannelInfo();
    if (nci)
    {
    float OutgoingLatency = nci->GetAvgLatency(FLOW_OUTGOING);
    return OutgoingLatency;
    }
    else
    {
    return 0.0f;
    }
    }
    float GetIncomingLatency()
    {
    INetChannelInfo *nci = I::Engine->GetNetChannelInfo();
    if (nci)
    {
    float IncomingLatency = nci->GetAvgLatency(FLOW_INCOMING);
    return IncomingLatency;
    }
    else
    {
    return 0.0f;
    }
    }

    https://yougame.biz/threads/19903/

    somerholder1337, 25 Июля 2017

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

    0

    1. 1
    https://github.com/catboost/catboost

    Похоже яндекс заопенсорсил свежую версию аркадии. Давайте обсирать эти их велосипеды.

    FrauSchweinhund, 19 Июля 2017

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

    0

    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
    void PPU::scanline() noexcept
    {
        if (control & CONTROL_MASK_BG_DISPLAY)
        {
            const int st = control & CONTROL_MASK_BG_WINDOW_TILE_DATA_SELECT ? 0x0000 : 0x0800;
            const int sm = control & CONTROL_MASK_BG_TILE_MAP_DISPLAY_SELECT ? 0x1C00 : 0x1800;
            for (int i = 0; i < 160; ++i)
            {
                const int im = sm + (scx + i) / 8 % 32 + (scy + ly) / 8 % 32 * 32;
                const int vm = st == 0x0000 ?               vram[im] :
                                              (signed char) vram[im] + 128;
                const unsigned px =
                    (vram[st + vm * 16 + (scy + ly) % 8 * 2    ] >> (7 - (scx + i) % 8) & 1) << 1 |
                    (vram[st + vm * 16 + (scy + ly) % 8 * 2 + 1] >> (7 - (scx + i) % 8) & 1);
    
                framebuffer[(i + ly * 160) / 8 * 2] &= ~((px << 6) >> i % 8 * 2);
                framebuffer[(i + ly * 160) / 8 * 2] |=   (px << 6) >> i % 8 * 2;
    
                framebuffer[(i + ly * 160) / 8 * 2 + 1] &= ~((px << 14) >> (i % 8 * 2));
                framebuffer[(i + ly * 160) / 8 * 2 + 1] |=   (px << 14) >> (i % 8 * 2);
            }
        }
    ...

    jangolare, 16 Июля 2017

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

    0

    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
    #include <type_traits>
    
    struct TPred1 {};
    struct TPred2 {};
    template<typename P1, typename P2> struct TAnd1: P1, P2 {TAnd1(P1, P2) {}};
    template<typename P1, typename P2> struct TAnd2: P1, P2 {TAnd2(P1, P2) {}};
    
    template<typename T, T v> struct my_integral_constant {enum {value=v};};
    template<class T, class From> struct my_is_base_of:
      my_integral_constant<bool, __is_base_of(T, From)> {};
    
    template<typename P1, typename P2> std::enable_if_t<
      my_is_base_of<TPred1, P1>::value &&
      my_is_base_of<TPred1, P2>::value,
    TAnd1<P1, P2>> operator&&(P1 p1, P2 p2)
    {return {p1, p2};}
    
    template<typename P1, typename P2> std::enable_if_t<
      my_is_base_of<TPred2, P1>::value &&
      my_is_base_of<TPred2, P2>::value,
    TAnd2<P1, P2>> operator&&(P1 p1, P2 p2)
    {return {(p1, p2};}
    
    
    template<typename T> struct is_callable
    {
      template<typename T1> struct dummy;
      template<typename CheckType> static short check(dummy<decltype(
        std::declval<std::remove_reference_t<CheckType>>()())>*);
      template<typename CheckType> static char check(...);
      enum: bool {value = sizeof(check<T>(nullptr)) == sizeof(short)};
    };
    
    struct IntellisenseKiller
    {
      template<typename T, typename = std::enable_if_t<
        std::is_function<T>::value &&
        is_callable<T>::value
      >> IntellisenseKiller(const T&) {}
      IntellisenseKiller(bool) {}
    };
    IntellisenseKiller eat4GbRam = true;

    Обнаружился ещё один способ превратить Visual Studio 2017 в тормозной, пожирающий 4 ГБ оперативы, блокнот с подсветкой синтаксиса.
    Это минимальный пример, который я выделил из своей библиотеки и порезал всё, что не вызывает баг. Вроде бы не связанные между собой куски кода, но глюки студии вызывают только вместе. Целый день потратил на поиск причины глюка.
    Случайно обнаружил, что длина получившегося фрагмента кода 42 строки. Я не подгонял, честно. Так что можно сказать, что ошибка в 42 строке. Без этой строки Intellisense заработает.

    gammaker, 12 Июля 2017

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

    0

    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
    #include <algorithm>
    #include <exception>
    #include <iostream>
    #include <memory>
    #include <string>
    #include <vector>
    #include <random>
    #include <chrono>
    #define __CL_ENABLE_EXCEPTIONS
    #include "cl.hpp"
    
    using namespace std::string_literals;
    
    
    int main(int argc, char * argv[]) {
      
      
      size_t data_len = (1024 * 1024 * strtoul(argv[1], nullptr, 10)) / sizeof(uint32_t),
             out_len = strtoul(argv[2], nullptr, 10),
             iter = strtoul(argv[3], nullptr, 10),
             block_size = strtoul(argv[4], nullptr, 10);
      
      std::string src = R"(
        typedef unsigned int uint32_t;
    __kernel void bench(global const uint32_t * data, global uint32_t * out) {
        
        uint32_t res = 0, id = get_global_id(0), next = id;
        for(uint32_t i = 0; i < )"s + std::to_string(iter) + R"(; ++i) {
            for(uint32_t j = 0; j < )" + std::to_string(block_size / sizeof(uint32_t)) +  R"(; ++j)
                res ^= data[next + j];
            next = data[next];
        }
        out[id] = res;
        
    }
      
      )"s;
      
      cl::Program::Sources sources = {{src.data(), src.size()}};
    
    
      std::vector<cl::Platform> platforms;
      cl::Platform::get(&platforms);
      
      std::vector<uint32_t> data(data_len);
      std::vector<uint32_t> out(out_len);
      
      std::generate(std::begin(data), std::end(data), [=,gen = std::mt19937{}]() mutable {
        return gen() % (data_len - (block_size / sizeof(uint32_t)));
      });
      
      for(auto & platform : platforms) {
        std::cout << "Using platform: " << platform.getInfo<CL_PLATFORM_NAME>() << "\n";
        std::vector<cl::Device> devices;
        platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
        for(auto & device : devices) {
          try {
            std::cout << "Using device: " << device.getInfo<CL_DEVICE_NAME>() << "\n";
            cl::Context ctx({device});
            cl::Program program(ctx, sources);
            program.build({device});
            
            cl::Buffer data_buffer(ctx, CL_MEM_READ_WRITE, data.size() * sizeof(uint32_t));
            cl::Buffer out_buffer(ctx, CL_MEM_READ_WRITE, out.size() * sizeof(uint32_t));
            cl::CommandQueue queue(ctx, device);
            queue.enqueueWriteBuffer(data_buffer, CL_TRUE, 0, data.size() * sizeof(uint32_t), data.data());
            
            cl::make_kernel<cl::Buffer &, cl::Buffer &> bench(program, "bench");
            cl::EnqueueArgs eargs(queue,cl::NullRange,cl::NDRange(out.size()),cl::NullRange);
            
            auto start = std::chrono::high_resolution_clock::now();
            bench(eargs, data_buffer, out_buffer).wait();
            auto time = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now() - start).count();
            
            size_t ops = out_len * iter;
            size_t total_tp = ops * block_size;
            double miops = (ops / time) / (1000 * 1000);
            double tpgbps = (total_tp / time) / (1024 * 1024 * 1024);
            fprintf(stderr, "Result: %.2fMIOPS, %.2fGB/s, %.2fsec\n", miops, tpgbps, time);
            
            
            queue.enqueueReadBuffer(out_buffer, CL_TRUE, 0, out.size() * sizeof(uint32_t), out.data());   
          
          } catch(cl::Error e) {
            std::cout << e.what() << " : " << e.err() << std::endl;
            std::terminate();
          }
        }
        
      }
    }

    Код Царя
    https://www.linux.org.ru/forum/development/13489159
    https://github.com/superhackkiller1997/gpu_mem_benchmark/blob/master/main.cpp

    j123123, 12 Июля 2017

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