Re: private, undefined copy ctors
Re: private, undefined copy ctors
- Subject: Re: private, undefined copy ctors
- From: "Clark Cox" <email@hidden>
- Date: Mon, 19 Mar 2007 10:43:47 -0700
On 3/18/07, Steve Checkoway <email@hidden> wrote:
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.
The standard allows the compiler to make a judgement call as to
whether or not to call the copy constructor here (When binding a
temporary to a non-const reference). However, the standard does *not*
allow the compiler to not check for the existence of a callable copy
constructor.
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.
If you forgo the use of a temporary, it should work:
struct Bar
{
Bar( const int &i ) { }
private:
Bar( const Bar &b );
};
static void Foo( const Bar &f ) {}
int main()
{
Bar b(5);
Foo(b);
return 0;
}
--
Clark S. Cox III
email@hidden
_______________________________________________
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