private, undefined copy ctors
private, undefined copy ctors
- Subject: private, undefined copy ctors
- From: Steve Checkoway <email@hidden>
- Date: Sun, 18 Mar 2007 20:07:54 -0700
I am getting a very strange error with g++. The code in question is
struct Bar
{
Bar( const int &i ) { }
private:
Bar( const Bar &b );
};
static void Foo( const Bar &f ) {}
int main() { Foo( Bar(5) ); return 0; }
The error I am getting is
huh.cc: In function ‘int main()’:
huh.cc:5: error: ‘Bar::Bar(const Bar&)’ is private
huh.cc:8: error: within this context
The copy ctor should _not_ be invoked here since Foo takes a constant
reference to a Bar. If you change the code to just pass 5 to Foo,
then the Bar is constructed as would be expected and there is no
problem with the copy ctor.
The _very_ strange part is that if I make the copy ctor public but
leave it undefined, then I get no error and it is never called.
So why is g++ checking the protection of the copy ctor if it is not
going to use it anyway? In addition, why does it only do it when
constructing the Bar object explicitly and not when doing it implicitly?
The obvious work around is to make the copy ctor public but
undefined. The only problem is that I've turned something that would
be a compile time error into a link time error.
--
Steve Checkoway
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden