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

    +1

    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
    // https://godbolt.org/z/QAR_nT
    // https://govnokod.ru/26701#comment550329
    #include <cstddef>
    #include <string>
    #include <cassert>
    
    
    struct assert_failure
    {
        explicit assert_failure(const char *sz)
        {
            std::fprintf(stderr, "Assertion failure: %s\n", sz);
            std::quick_exit(EXIT_FAILURE);
        }
    };
    
    // эта херня не совсем корректно будет обрабатывать всякую хрень вроде ", , , " - оно это посчитает за 4 аргумента,
    // и если считать " скобочки, тогда еще надо запилить обработку эскейп-последовательности, для хуйни типа "pidor\" govno"
    // но мне лень эту хуйню допиливать.
    constexpr std::size_t count_args(const char *s, std::size_t depth = 0, std::size_t pos = 0, std::size_t count = 0)
    {
      if (s[pos] == '\0'){
        if(depth != 0)
          throw assert_failure("kakoi bagor)))\n");
        if(pos == 0)
          return 0;
        return count+1;
      }
      else if(s[pos] == '{')
      {
        return count_args(s, depth + 1, pos + 1, count);
      }
      else if(s[pos] == '}')
      {
        if(depth == 0)
          throw assert_failure("kakoi bagor)))\n");
        return count_args(s, depth - 1, pos + 1, count);
      }
      else if(depth == 0)
      {
        if(s[pos] == ',')
        {
          return count_args(s, depth, pos + 1,  count + 1);
        }
      }
      return count_args(s, depth, pos+1,  count); 
    }
    
    #define TO_STR(...) #__VA_ARGS__
    #define ARGNUM(...) count_args(TO_STR(__VA_ARGS__))
    
    #define krestogovnotypeof(a) std::remove_reference<a>::type
    
    #define FOR_RANGE(type, varname, ...) for(struct {size_t cnt; krestogovnotypeof(type) arr[ ARGNUM(__VA_ARGS__)  ];  } varname = {0, {__VA_ARGS__}}; varname.cnt < sizeof(varname.arr)/sizeof(type); ++varname.cnt )
    
    int main(void)
    {
      FOR_RANGE(int[2], k, {1,2}, {3,4}, {5,6}, {7,8})
        printf("{%d %d},\n", k.arr[k.cnt][0], k.arr[k.cnt][1]);
      return EXIT_SUCCESS;
    }

    Какая крестопараша((( В вижуальхуюдии вроде собирается, но проверить корректность работы не могу. Как вы этим днищекомпилятором вообще пользуетесь?

    Было б круто, если бы были такие constexpr функции, которые в компилтайме могут куски исходного кода высирать как бы прямо в исходник, и потом уже чтоб это компилировалось

    j123123, 29 Мая 2020

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

    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
    int ACMEncoder::Encode(int framepos, void *in, int in_avail, int *in_used, void *out, int out_avail)
    {
    	char *pin = (char *)in;
    	char *pout = (char *)out;
    	int retval = 0;
    
    	if (!m_did_header && do_header)
    	{
    		int s = 44;
    		s = 4 + 4 + 12 - 4;
    
    		int t;
    		if (m_convert_wfx.wfx.wFormatTag == WAVE_FORMAT_PCM) t = 0x10;
    		else t = sizeof(WAVEFORMATEX) + m_convert_wfx.wfx.cbSize;
    		s += 4 + t;
    		if (s&1) s++;
    
    		if (m_convert_wfx.wfx.wFormatTag != WAVE_FORMAT_PCM)
    			s += 12;
    
    		s += 8;
    
    		if (out_avail < s) return 0;
    		//xx bytes of randomness
    		m_hlen = s;
    		m_did_header = 1;
    		out_avail -= s;
    		pout += s;
    		retval = s;
    	}

    «Winamp» спиздили.

    gost, 27 Мая 2020

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

    +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
    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
    template <typename F, class = decltype(F()(E()))>
        auto map(F p) -> std::vector< decltype(p(E())) >
        {        
            std::vector< decltype(p(E())) > result;
            std::transform(get().begin(), get().end(), std::back_inserter(result), [=](auto &v) {
                return mutable_(p)(v);
            });
    
            return result;
        }
    
        template <typename F, class = decltype(F()(E(), 0))>
        auto map(F p) -> std::vector< decltype(p(E(), 0)) >
        {        
            std::vector< decltype(p(E(), 0)) > result;
            auto first = &(get())[0];
            std::transform(get().begin(), get().end(), std::back_inserter(result), [=](auto &v) {
                auto index = &v - first;
                return mutable_(p)(v, index);
            });
    
            return result;
        }
    
    // и применение (e) => f()
        auto strs = (array<int>{ 1, 2, 3 }).map([](auto x)
        {
            return "X" + x;
        });
    
    // или (e, index) => f()
    
        auto strs = (array<int>{ 1, 2, 3 }).map([](auto x)
        {
            return x + i;
        });

    как я выкрутился бля... с разными маперами... как генерики в c#

    ASD_77, 27 Мая 2020

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

    +5

    1. 1
    https://github.com/ASDAlexander77/TypeScript2Cxx/blob/master/cpplib/core.h

    Нужна помощь смелых и умных людей, надо сделать review кода и посоветовать что там по стандартам улучшить... короче любая помощь welcome

    https://github.com/ASDAlexander77/TypeScript2Cxx/blob/master/cpplib/core.h

    ASD_77, 27 Мая 2020

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

    +1

    1. 001
    2. 002
    3. 003
    4. 004
    5. 005
    6. 006
    7. 007
    8. 008
    9. 009
    10. 010
    11. 011
    12. 012
    13. 013
    14. 014
    15. 015
    16. 016
    17. 017
    18. 018
    19. 019
    20. 020
    21. 021
    22. 022
    23. 023
    24. 024
    25. 025
    26. 026
    27. 027
    28. 028
    29. 029
    30. 030
    31. 031
    32. 032
    33. 033
    34. 034
    35. 035
    36. 036
    37. 037
    38. 038
    39. 039
    40. 040
    41. 041
    42. 042
    43. 043
    44. 044
    45. 045
    46. 046
    47. 047
    48. 048
    49. 049
    50. 050
    51. 051
    52. 052
    53. 053
    54. 054
    55. 055
    56. 056
    57. 057
    58. 058
    59. 059
    60. 060
    61. 061
    62. 062
    63. 063
    64. 064
    65. 065
    66. 066
    67. 067
    68. 068
    69. 069
    70. 070
    71. 071
    72. 072
    73. 073
    74. 074
    75. 075
    76. 076
    77. 077
    78. 078
    79. 079
    80. 080
    81. 081
    82. 082
    83. 083
    84. 084
    85. 085
    86. 086
    87. 087
    88. 088
    89. 089
    90. 090
    91. 091
    92. 092
    93. 093
    94. 094
    95. 095
    96. 096
    97. 097
    98. 098
    99. 099
    100. 100
    struct any
        {
            std::any _val;
    
            inline constexpr any() : _val{}
            {
            }
    
            inline constexpr any(undefined_t) noexcept : _val{}
            {
            }
    
            inline constexpr any(std::nullopt_t) noexcept : _val{}
            {
            }
    
            template <typename T>
            inline constexpr any(const T &val) : _val{val}
            {
            }
    
            inline any(const any &val) noexcept : _val{val._val}
            {
            }
    
            inline any(any &&val) noexcept : _val{std::move(val._val)}
            {
            }
    
            inline any &operator=(const any &val)
            {
                _val = val._val;
                return *this;
            }
    
            inline operator bool()
            {
                if (!_val.has_value())
                {
                    return false;
                }
    
                auto type_index = std::type_index(_val.type());
                if (type_index == std::type_index(typeid(int)))
                {
                    return std::any_cast<int>(_val) > 0;
                }
                else if (type_index == std::type_index(typeid(double)))
                {
                    return std::any_cast<double>(_val) > 0;
                }
                else if (type_index == std::type_index(typeid(tstring)))
                {
                    return std::any_cast<tstring>(_val).size() > 0;
                }
                else if (type_index == std::type_index(typeid(tstring_view)))
                {
                    return std::any_cast<tstring_view>(_val).size() > 0;
                }
                else if (type_index == std::type_index(typeid(const char_t *)))
                {
                    auto v = std::any_cast<const char_t *>(_val);
                    return !v;
                }
                else if (type_index == std::type_index(typeid(std::nullptr_t)))
                {
                    return false;
                }
                else if (type_index == std::type_index(typeid(bool)))
                {
                    return std::any_cast<bool>(_val);
                }
    
                return true;
            }
    
            template <typename N = void>
            requires Arithmetic<N> any operator+(N n)
            {
                return any{std::any_cast<N>(this->_val) + n};
            }
    
            friend std::ostream &operator<<(std::ostream &os, const any &val_)
            {
                auto val = val_._val;
                if (val.has_value())
                {
                    auto type_index = std::type_index(val.type());
                    if (type_index == std::type_index(typeid(int)))
                    {
                        os << std::any_cast<int>(val);
                    }
                    else if (type_index == std::type_index(typeid(double)))
                    {
                        os << std::any_cast<double>(val);
                    }
                    else if (type_index == std::type_index(typeid(tstring)))
                    {
                        os << std::any_cast<tstring>(val);
                    }

    нужна помощь упростить этот ад.

    ASD_77, 21 Мая 2020

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

    0

    1. 1
    reinterpret_cast<ppu_function_t>(static_cast<std::uintptr_t>(ppu_ref<u32>(cia)))(*this);

    Лопни глазоньки. Шаблоны увечат С++ как бог черепаху.
    https://github.com/RPCS3/rpcs3/blob/91d06a97296e5e418fb601284577475d8c7dfbaf/rpcs3/Emu/Cell/PPUThread.cpp

    Psionic, 21 Мая 2020

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

    +1

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    if(object)
        if(object->isSolid)
            event.isCancelled = true;
        ;else //<
            throw NullEventArgumentException("object == nullptr");

    Новый токен в C++ про который знает только 0.6% говнокодеров

    digitalEugene, 21 Мая 2020

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

    +1

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    template <class... Args>
        void log(Args&&... args)
        {
            auto dummy = { (std::clog << args, 0)... };
            std::clog << std::endl;
        }

    новый printf на с++

    ASD_77, 12 Мая 2020

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

    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
    #define DECLARE_TYPE(Name, Type) \
        using TYPE = Type; \
        ObjectInterface s_##Type##ObjectInterface (Name, []()->ObjectType* { return new Type;
    
    #define DECLARE_METHODS \
            }, {
    
    #define DECLARE_PROPERTIES \
            }, {
    
    #define DECLARE_METHOD(Method) \
            { #Method, reinterpret_cast <_ObjectMethodProc> (&TYPE::Method) },
    
    #define DECLARE_CUSTOM_NAME_METHOD(Name, Method) \
            { Name, reinterpret_cast <_ObjectMethodProc> (&TYPE::Method) },
    
    #define DECLARE_METHOD_MAPPER(_1,_2,DECLARE_METHOD,...) DECLARE_METHOD
    #define METHOD(...) DECLARE_METHOD_MAPPER(__VA_ARGS__, DECLARE_CUSTOM_NAME_METHOD, DECLARE_METHOD)(__VA_ARGS__)
    
    #define PROPERTY(Name, Get, Set) \
            { Name, { reinterpret_cast <_PropertyGettingProc> (&TYPE::Get), reinterpret_cast <_PropertySettingProc> (&TYPE::Set) } },
    
    #define END_DECLARE_TYPE \
            });

    ???

    codemonkey, 07 Мая 2020

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

    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
    #include <iostream>
    #include <chrono> 
    #include <thread>
    #include <fstream>
    #include <string>
    
    using namespace std;
    float Cheking = 0;
    
    void read_file()
    {
        string line;
        ifstream file("text.bin");
        if (file)
        {
            while (getline(file, line))
            {
                file >> Cheking;
            }
            file.close();
        }
    }
    void write_file()
    {
        ofstream myfile("text.bin");
        if (myfile.is_open())
        {
            myfile << "\n";
            myfile << Cheking << "\n";
            myfile.close();
        }
        else 
        {
            cout << "Unable to open file";
            myfile.close();
        }
    }
    
    int main()
    {
        using namespace chrono;
        using namespace this_thread;
        setlocale(LC_ALL, "ukr");
        read_file();
        cout << "Число: " << Cheking << "\t\t";
        cout << endl;
        if (Cheking == 0)
        {
            cin >> Cheking;
        }   
        write_file();
        sleep_for(milliseconds(1000));
        return 0;
    }

    Простой гавнокод для храненния переменных в файле.

    vova09082000, 06 Мая 2020

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