[Bug c++/14779] New: the copy constructor for a templatized class is not used in array initialization unless the constructor was previously referenced
jonathanu at apptechsys dot com
gcc-bugzilla@gcc.gnu.org
Tue Mar 30 01:05:00 GMT 2004
The copy constructor for a templatized class is not used for struct or array
initialization unless the copy constructor has already been instantiated.
The following code compiles cleanly. However, the copy operator for std::pair is
not used in the std::pair array initialization, causing internal member pointers
to be incorrectly initialized.
If the noted line is uncommented, the copy constructor will be used.
/* Demonstration of a bug with struct and array initialization
* when using an object with templatized copy constructor.
*/
#include <iostream>
#include <utility>
class show_bug
{
public:
show_bug () : m_ptr (this) {}
show_bug (const show_bug &sb) : m_ptr (this) {}
show_bug* m_ptr;
};
typedef std::pair<show_bug, show_bug> pair_bug_t;
const show_bug bug_a, bug_b;
/* UNCOMMENT THE LINE BELOW TO FIX THE BUG
* This line instantiates the copy constructor for std::pair,
* fixing the bug
*/
//const pair_bug_t fix_bug (pair_bug_t (bug_a, bug_b));
const pair_bug_t pba[] = { pair_bug_t (bug_a, bug_b) };
//-----------------------------
int main (int argc, char **argv)
{
std::cout << "copy constructor called: ";
std::cout << (pba[0].first.m_ptr == &pba[0].first) << std::endl;
}
--
Summary: the copy constructor for a templatized class is not used
in array initialization unless the constructor was
previously referenced
Product: gcc
Version: 3.3.2
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: jonathanu at apptechsys dot com
CC: gcc-bugs at gcc dot gnu dot org
GCC build triplet: gcc (GCC) 3.3.2 20031218 (Gentoo Linux 3.3.2-r5,
propolice-3.3-7
GCC host triplet: gentoo linux, 2.6.3 kernel, dual xeon
GCC target triplet: gentoo linux, 2.6.3 kernel, dual xeon
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14779
More information about the Gcc-bugs
mailing list