c++ - VS compiler could access private copy ctor -
possible duplicate:
can objects private copy constructors thrown?
as know, when trow object value, copy should created. copy constructor should called if exist. if copy ctor exists , private should cause compilation error. here code sample
class exception { public: exception() { cout << "exception()" << endl; } ~exception() { cout << "~exception() " << endl; } private: exception(const exception &c) { cout << "exception(c)" << endl; } }; and next code should lead compilation error.
try { exception local; throw local; } catch (...) { } but both in vs 2005 , vs 2008 succesfully compile code , call private ctor. wrong non standard behaviour , error in compiler?
i'm going go out on limb here , legitimate msvc 10 bug. cannot find reference this, however.
here test harness:
#include <cstdlib> #include <string> #include <iostream> #include <iomanip> using namespace std; class exception { public: exception() { cout << "exception()" << endl; } ~exception() { cout << "~exception() " << endl; } private: exception(const exception &c) { cout << "exception(c)" << endl; } }; int main() { try { exception local; int n = 42; throw local; } catch (...) { } } this code should fail compile reason note -- copy constructor private , being called outside context of class.
this code compiles in msvc 10 , msvc 11 dev preview.
gcc 4.4.4 under rhel6.2 emits:
[xxx@yyy ~]$ gcc --version gcc (gcc) 4.4.4 20100726 (red hat 4.4.4-13) copyright (c) 2010 free software foundation, inc. free software; see source copying conditions. there no warranty; not merchantability or fitness particular purpose. [xxx@yyy ~]$ gcc hack.cpp hack.cpp: in function ‘int main()’: hack.cpp:17: error: ‘exception::exception(const exception&)’ private hack.cpp:29: error: within context [xxx@yyy ~]$
Comments
Post a Comment