Попортила мне кровь, съела пару часов и заставила сомневаться в собственной адекватности.
Приведенный ниже код не компилируется. Причем не всегда, а при определенных условиях
- gcc
- Qt 4.8.6 (?)
- не оптимизированная сборка (-O0)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <QList> #include <list> #include <iostream> class A { public: A(int i): mI(i) {} const int mI; }; int main() { std::list<A> stdlist; stdlist.push_back(A(3)); QList<A> qlist; qlist.push_back(A(5)); return 1; } |
1 2 3 |
>g++ -c -O0 -DQT_CORE_LIB -I${YOUR_QT_DIR}/include/QtCore main.cpp ... main.cpp:11: error: non-static const member 'const int A::mI', can't use default assignment operator |
А вот и замечательный кусочек из исходников Qt. Не кодите, дети, reinterpet_cast.
1 2 3 4 5 |
#if (defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__IBMCPP__)) && !defined(__OPTIMIZE__) // This violates pointer aliasing rules, but it is known to be safe (and silent) // in unoptimized GCC builds (-fno-strict-aliasing). The other compilers which // set the same define are assumed to be safe. else *reinterpret_cast<T*>(n) = t; |