[Bug c++/40655] New: The copy constructor is not needed but GCC (C++) don't let me to initialize my object instance!

View: New views
7 Messages — Rating Filter:   Alert me  

[Bug c++/40655] New: The copy constructor is not needed but GCC (C++) don't let me to initialize my object instance!

by Bugzilla from gcc-bugzilla@gcc.gnu.org :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sorry for my long summary text!
Please, look at the following C++ code:

//---------- start of the code

class A
{
    public : A (int i) { }
    private: A (const A& a) { }
};

int main (int argc, char* argv[])
{
    // ERROR: How could i use the auto keyword which exist in GCC 4.4
    auto a1 = A (5);

    // ERROR: Why? Is it logically false? Really i don't know.
    A a2[3] = { A (0), A (1), A (2) };
}

//---------- end of the code

All problems will begin, When the copy constructor is private.


--
           Summary: The copy constructor is not needed but GCC (C++) don't
                    let me to initialize my object instance!
           Product: gcc
           Version: 4.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: MSHojatoleslami at Gmail dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40655


[Bug c++/40655] The copy constructor is not needed but GCC (C++) don't let me to initialize my object instance!

by Bugzilla from gcc-bugzilla@gcc.gnu.org :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



------- Comment #1 from rguenth at gcc dot gnu dot org  2009-07-06 10:17 -------
Because you are doing ... copies!


--

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40655


[Bug c++/40655] The copy constructor is not needed but GCC (C++) don't let me to initialize my object instance!

by Bugzilla from gcc-bugzilla@gcc.gnu.org :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



------- Comment #2 from MSHojatoleslami at Gmail dot com  2009-07-06 17:19 -------
Thanks for your answer.
But there is no need to copy them always as you know:

// ---------- begin
#include <iostream>
class A
{
public :
    A (int i)
    {
        std::cout << "Copy constructor is not used." << std::endl;
    }
};
int main ()
{
    auto a1 = A (5); // Straight Construction
    A a2[3] = { A (0), A (1), A (2) }; // Straight Construction

}
// ---------- end

So, why copy constructor needed to be public, although it's not used!?
Excuse me, If i insist on it. Because i really don't know why it's not a bug,
Specially for "auto" keyword.
There is XFC classes (XFC toolkit that wraps Gtk+) that their copy constructors
are private for some reason, and now, there is no way to use "auto" keyword to
initialize them. Instead, i must to initialize them with the old ways of
standard C++03.


--

MSHojatoleslami at Gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40655


[Bug c++/40655] The copy constructor is not needed but GCC (C++) don't let me to initialize my object instance!

by Bugzilla from gcc-bugzilla@gcc.gnu.org :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



------- Comment #3 from jwakely dot gcc at gmail dot com  2009-07-06 17:21 -------
(In reply to comment #0)
>     // ERROR: How could i use the auto keyword which exist in GCC 4.4
>     auto a1 = A (5);

You need to use -std=c++0x to enable C++0x features.


--


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40655


[Bug c++/40655] The copy constructor is not needed but GCC (C++) don't let me to initialize my object instance!

by Bugzilla from gcc-bugzilla@gcc.gnu.org :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



------- Comment #4 from jwakely dot gcc at gmail dot com  2009-07-06 17:34 -------
See 8.5 [dcl.init] paragraph 12

The initialization that occurs in argument passing, function return, throwing
an exception (15.1), handling an exception (15.3), and brace-enclosed
initializer lists (8.5.1) is called copy-initialization and is equivalent to
the form
T x = a;

and 12.8 [class.copy] paragraphs 14 and 15

A program is ill-formed if the copy constructor or the copy assignment operator
for an object is implicitly used and the special member function is not
accessible
...
When certain criteria are met, an implementation is allowed to omit the copy
construction of a class object, even if the copy constructor and/or destructor
for the object have side effects.

Your code is invalid, this is not a bug in GCC.


--


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40655


[Bug c++/40655] The copy constructor is not needed but GCC (C++) don't let me to initialize my object instance!

by Bugzilla from gcc-bugzilla@gcc.gnu.org :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



------- Comment #5 from paolo dot carlini at oracle dot com  2009-07-06 18:18 -------
Of course.


--

paolo dot carlini at oracle dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40655


[Bug c++/40655] The copy constructor is not needed but GCC (C++) don't let me to initialize my object instance!

by Bugzilla from gcc-bugzilla@gcc.gnu.org :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



------- Comment #6 from MSHojatoleslami at Gmail dot com  2009-07-06 18:38 -------
Thank you :)
I don't know what should i say, but it seems that this was my ... mistake!


--


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40655