Re: Inaccessible but unused copy constructor causes compile fail

Discussion in 'C++' started by goran.pusic@gmail.com, Nov 23, 2012.

  1. Guest

    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
    , Nov 23, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Shohet
    Replies:
    1
    Views:
    479
    William F. Robertson, Jr.
    Aug 1, 2003
  2. Replies:
    7
    Views:
    480
    Default User
    Aug 4, 2006
  3. Victor Bazarov
    Replies:
    15
    Views:
    760
    Greg Comeau
    Mar 4, 2007
  4. cinsk
    Replies:
    35
    Views:
    2,603
    James Kanze
    Oct 11, 2010
  5. Ian Collins
    Replies:
    0
    Views:
    226
    Ian Collins
    Nov 21, 2012
Loading...

Share This Page