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

    −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
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a, b, c;
        cin>>a>>b;
        if(a==0&&b==0&&c==0)
            cout<<"Black";
        if(a==1&&b==0&&c==0)
            cout<<"Red";
            if(a==0&&b==1&&c==0)
            cout<<"Green";
            if(a==0&&b==0&&c==1)
            cout<<"Blue";
            if(a==1&&b==1&&c==0)
            cout<<"Yellow";
            if(a==1&&b==0&&c==1)
            cout<<"Magenta";
            if(a==0&&b==1&&c==1)
            cout<<"Cyan";
            if(a==1&&b==1&&c==1)
            cout<<"White";
    }

    не спрашивайте, для чего

    ramune, 18 Марта 2018

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

    −1

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    Antony Polukhin
     in 
    pro.cxx
    Кстати, в EWG одобрили constexpr контейнеры http://open-std.org/JTC1/SC22/WG21/docs/papers/2018/p0784r1.html
    так что есть все шансы к С++20 писать:
    constexpr std::string result = foo();
    t.me/ProCxx
    /184343
    Mar 16 at 10:47

    Library pragmatism

    Current implementations of standard libraries sometimes perform various raw storage operations through interfaces other than the standard allocator and allocator traits. That may make it difficult to make the associated components usable in constexpr components. Based on a cursory examination of current practices, we therefore propose to start only with the requirement that the container templates in the [containers] clause be usable in constexpr evaluation, when instantiated over literal types and the default allocator. In particular, this excludes std::string, std::variant, and various other allocating components. Again, it is our hope we will be able to extend support to more components in the future.

    With regards to the default allocator and allocator traits implementation, the majority of the work is envisioned in the constexpr evaluator: It will recognize those specific components and implement their members directly (without necessarily regarding the library definition).
    We might, however, consider decorating the class members with the constexpr keyword. Also, some implementations provide extra members in these class templates (such as libc++'s allocator_traits<A>::__construct_forward ) that perform non-constexpr-friendly operations (memcpy, in particular). Lifting such members to standard status would help interoperability between library and compiler implementations.

    j123123, 16 Марта 2018

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

    +4

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    bool MyClass::operator==(int elem){
        if (list.isExist(elem)){
            list.remove(elem);
            return true; // Операция завершена успешно
        }
        return false; // Элемент elem не найден в списке
    }

    (C) https://www.linux.org.ru/forum/development/14063699?cid=14063991

    Вырвано из контекста (треда), но считаю данная кучка должна лежать здесь.

    Elvenfighter, 05 Марта 2018

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

    −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
    #include <iostream>
    using namespace std;
    struct MyType { MyType() {  cout << __PRETTY_FUNCTION__ << endl; }};
    MyType& MyType() { cout << __PRETTY_FUNCTION__ << endl; }
    using MyType2 = struct MyType;
    int main() {
      // MyType t; <- error: expected ‘;’ before ‘t’
      MyType();
      struct MyType t;
      struct MyType t1 = MyType();
      struct MyType t2 = (struct MyType)::MyType();
      struct MyType t3 = MyType2();
      new(&t2) struct MyType();
      return 0;
    }

    Крестоблядство по мотивам #23850.
    https://ideone.com/XcK2hf.
    Особенно меня порадовал каст на 11 строчке.

    Bobik, 03 Марта 2018

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

    +2

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    MyType(const CopyPastedFromSomewhere&) = delete;
    MyType& operator=(const CopyPastedFromSomewhere&) = delete;
    
    MyType& operator=(const MyType*) = delete;
    
    // Winner?
    MyType& MyType(const MyType&) = delete;

    А что можно найти в вашей кодовой базе? К слову, выяснилось, что вариант

    void operator=(const MyType&) = delete;
    вполне себе допустим.

    roman-kashitsyn, 02 Марта 2018

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

    0

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    #include <xmmintrin.h>
    
    void crasher() {
        constexpr __m128 w = {1,2,3,4};
        asm ("addps %[w], %[w]" : : [w] ""(w));
    }

    Крашим GCC
    https://ideone.com/iIAN0i

    Antervis, 28 Февраля 2018

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

    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
    class PriceCache {
    public:
      FlightStorage(const std::size_t count) {
        for (std::size_t i = 0; i < count; ++i) {
          flights.emplace_back(FlightCache::get(i));
          prices.emplace_back(&flights.back(), Price::getFor(flights.back()));
        }
      }
    
    private:
      std::vector<Flight> flights;
      std::vector<const Flight *, double> prices;
    };

    "случайные сегфолты при обращении к PriceCache::prices"

    Elvenfighter, 28 Февраля 2018

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

    0

    1. 1
    return f() <= x->size() ? true:false;

    Классика?

    syoma, 20 Февраля 2018

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

    −1

    1. 1
    2. 2
    3. 3
    4. 4
    // Read option name (can contain spaces)
         while (is >> token && token != "value")
    -        name += string(" ", name.empty() ? 0 : 1) + token;
    +        name += (name.empty() ? "" : " ") + token;

    terminate called after throwing an instance of 'std::length_error'
    what(): basic_string::_M_create


    Replacing string(" ", name.empty() ? 0 : 1) with (name.empty() ? "" : " ") and the same in the while() loop for value fixes the problem (for me).

    Does anyone know if "string(" ", 0)" is invalid C++ ?

    Кресты такие кресты.

    3.14159265, 18 Февраля 2018

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

    0

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    double x = 0, y;
    while (x < 1) {
        y = x;
        x += rand(0, 1);
    }

    Задача на теорию вореации и кобенаторику.
    rand - равномерное распределение
    Нужно найти матожидание y. С пруфами.

    3_dar, 07 Февраля 2018

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