Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Re: Inaccessible but unused copy constructor causes compile fail (http://www.velocityreviews.com/forums/t954784-re-inaccessible-but-unused-copy-constructor-causes-compile-fail.html)

goran.pusic@gmail.com 11-23-2012 03:05 PM

Re: Inaccessible but unused copy constructor causes compile fail
 
On Wednesday, November 21, 2012 10:30:47 PM UTC+1, Andy Champ wrote:
> Compiling this...
>
>
>
> struct ncnd
>
> {
>
> ncnd()
>
> {
>
> }
>
> private:
>
> ncnd(const ncnd& other);
>
> };
>
>
>
> void main()
>
> {
>
> ncnd x = ncnd();
>
> }
>
>
>
>
>
> Comeau complains:
>
> line 12: error: "ncnd::ncnd(const ncnd &)" (declared at line 7),
>
> required for copy that was eliminated, is inaccessible
>
> ncnd x = ncnd();
>
>
>
> (it also doesn't like void main() but that's beside the point)
>
>
>
> MSVC (2010 and 2012) also fails - but when I comment out the private it
>
> compiles, links and runs just fine. Why does it need the copy
>
> constructor when it's about to eliminate it?


Because compiler wants to use copy ctor where it looks like you have assignment.

Compiler is treating

type t = type()

like

type t(type())

(Hence the error).

Think about the following: your code looks like it should do the default ctor + assignment. Why would you want this? It's longer to write and less efficient than copy construction, but amounts to same (your object ends up being a copy of another one).

So the standard sayt that the compiler should use copy ctor when it sees assignment like yours.

If you __must__ have default construction and assignment (hint: you don't, consider rule of three[1]), you can:

ncnd x;
x = ncnd();


HTH,

Goran.

[1] http://en.wikipedia.org/wiki/Rule_of_Three


All times are GMT. The time now is 05:13 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.