7

Possible Duplicate:
Can objects with private copy constructors be thrown?

As I know, when you trow object as value, copy should be created. So copy constructor should be called if exist. If copy ctor exists and is private then this should cause compilation error. Here is 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 to compilation error.

try {
        Exception local;

        throw local;
    } catch (...) {
    }

But both in VS 2005 and VS 2008 succesfully compile that code and call private ctor. Am I wrong that this is non standard behaviour and is an error in compiler?

2
  • I spoke too soon. ideone.com/hXrJd Jun 5, 2012 at 13:05
  • 2
    @Ation: VS used to be the worst Standard-compliant C++ compiler among the major ones (gcc, icc, comeau, etc..). Its supporters say that it's much better now, personally I just stopped using it. Jun 5, 2012 at 13:53

2 Answers 2

0

I'm going to go out on a limb here and say that this is probably a legitimate MSVC 10 bug. I cannot find a reference for this, however.

Here is a 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 to compile for the reason you note -- the copy constructor is private and being called from outside the context of the class.

This code compiles successfully in MSVC 10 and 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.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[xxx@yyy ~]$ gcc hack.cpp 
hack.cpp: In function ‘int main()’:
hack.cpp:17: error: ‘Exception::Exception(const Exception&)’ is private
hack.cpp:29: error: within this context
[xxx@yyy ~]$ 
0

The standard says that an exception must be copyable because throw may make a copy. You've made the copy constructor private, so it should not compile.

It does not however state that the implementation of throw is required to make a copy - indeed, it may be elided or in C++11 even moved. So although MSVC should refuse to compile the code on the grounds that it is not standards-compliant, it still does, because it'll work with MSVC's way of doing things.

This isn't likely a bug, but rather just a case of VC being non-compliant with the standard.

4
  • How is being non-compliant not a bug?
    – Mark B
    Jun 5, 2012 at 15:57
  • 1
    @MarkB I suppose that depends on how you define a bug. Here, I'm referring to the point that it seems intentional on Microsoft's part. Speaking from experience this is exactly the kind of thing they would be "lax" on. Jun 5, 2012 at 16:02
  • 1
    The standard explicitly states that even when copies are elided the copy constructor must still be available and accessible. I can't see any way to interpret this behavior as a non-bug.
    – Mark B
    Jun 5, 2012 at 16:04
  • It's a bug in the context of the standard, not in the context of the compiler IMHO Jun 5, 2012 at 16:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.