Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
range based for loop temporary object lifetime issue example
// range based for loop temporary object lifetime issue example #include <iostream> class TestRange { bool _alive; public: TestRange() : _alive(true) {} ~TestRange() { _alive = false; } TestRange const &fluentAPIMethod() const { return *this; } TestRange const * begin() const { return this; } TestRange const * end() const { return this + 1; } void checkAlive() const { if (_alive) std::cout << "OK - DTOR not called yet\r\n"; else std::cout << "ERR - DTOR already called on this\r\n"; // this is an error } }; int main() { #ifdef _MSC_FULL_VER std::cout << "MSVC version " << _MSC_FULL_VER << " \r\n"; #elif defined (__clang__) && defined (__clang_major__) && defined (__clang_minor__) && defined (__clang_patchlevel__) std::cout << "CLANG version " << __clang__ << "." << __clang_major__ << "." << __clang_minor__ << "." << __clang_patchlevel__ << " \r\n"; #elif defined (__GNUC__) && defined (__GNUC__) && defined (__GNUC_MINOR__) std::cout << "GCC version " << __GNUC__ << "." << __GNUC_MINOR__ << "." << __GNUC_PATCHLEVEL__ << " \r\n"; #endif for (auto & el : TestRange()) el.checkAlive(); for (auto & el : TestRange().fluentAPIMethod()) el.checkAlive(); return 0; }
run
|
edit
|
history
|
help
0
void pointer
2574 EC
C++ Program to Print Binary
Array-Based Heap Example Starter Code
Throttle Example in C++
bank queue
Non type template argument
HTML Timetable generator.cpp
Calculate H.C.F using recursion
marquee text in C++