public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/66632] New: Incorrect use of default constructor to initialize isolated rvalue.
@ 2015-06-22 23:51 jasonxbell at gmail dot com
  2015-06-22 23:57 ` [Bug c++/66632] " jasonxbell at gmail dot com
  2015-06-23 18:14 ` daniel.kruegler at googlemail dot com
  0 siblings, 2 replies; 3+ messages in thread
From: jasonxbell at gmail dot com @ 2015-06-22 23:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66632

            Bug ID: 66632
           Summary: Incorrect use of default constructor to initialize
                    isolated rvalue.
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jasonxbell at gmail dot com
  Target Milestone: ---

Created attachment 35828
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=35828&action=edit
Preprocessed source file

In the following code, the first statement in main() -- 'Q(S1v1)' -- creates an
isolated rvalue; g++ uses the default constructor to initialize it instead of
'Q::Q(S1 x)'. The lvalue q1 is then init'd via copy of the (incorrectly init'd)
rvalue from the previous statement.


bug2.c:
--------------------------------------------------------------------------------
#include <iostream>
using std::cout;
using std::ostream;

enum S1 { S1v0 = 0, S1v1= 10, S1v2, S1v3 };


class Q
{
public:
           Q()               : mySN(nextSN++), s1val(S1v0)      { cout << "+Q
#" << mySN << " Default\n"; }
           Q(const Q  &src ) : mySN(nextSN++), s1val(src.s1val) { cout << "+Q
#" << mySN << " Copy from #" << src.mySN << '\n'; }

  explicit Q(S1 x)           : mySN(nextSN++), s1val(x)         { cout << "+Q
#" << mySN << " from S1\n"; }

private:
  int mySN;
  S1 s1val;

  static int nextSN;

  friend ostream& operator<<( ostream &out, const Q &obj );

};

int Q::nextSN = 1;

ostream& operator<<( ostream &out, const Q &obj )
{
  out << "Q #" << obj.mySN << " == " << obj.s1val << '\n'; 
  return out;
}


int main( void )
{

  Q(S1v1);
  Q q2{S1v1};
  cout << '\n';

  cout << q2;

  return 0;
}
--------------------------------------------------------------------------------



Compile and run:

$ g++ -std=c++11 -pedantic -Wall -Wextra bug2.cpp; ./a.exe
+Q #1 Default
+Q #2 Copy from #1

Q #2 == 0


$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with:
/cygdrive/i/szsz/tmpp/gcc/gcc-4.9.2-3.x86_64/src/gcc-4.9.2/configure
--srcdir=/cygdrive/i/szsz/tmpp/gcc/gcc-4.9.2-3.x86_64/src/gcc-4.9.2
--prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin
--libexecdir=/usr/libexec --datadir=/usr/share --localstatedir=/var
--sysconfdir=/etc --libdir=/usr/lib --datarootdir=/usr/share
--docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C
--build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-pc-cygwin
--without-libiconv-prefix --without-libintl-prefix --libexecdir=/usr/lib
--enable-shared --enable-shared-libgcc --enable-static
--enable-version-specific-runtime-libs --enable-bootstrap --enable-__cxa_atexit
--with-dwarf2 --with-tune=generic
--enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-graphite
--enable-threads=posix --enable-libatomic --enable-libgomp --disable-libitm
--enable-libquadmath --enable-libquadmath-support --enable-libssp
--enable-libada --enable-libgcj-sublibs --disable-java-awt --disable-symvers
--with-ecj-jar=/usr/share/java/ecj.jar --with-gnu-ld --with-gnu-as
--with-cloog-include=/usr/include/cloog-isl --without-libiconv-prefix
--without-libintl-prefix --with-system-zlib --enable-linker-build-id
Thread model: posix
gcc version 4.9.2 (GCC)


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-06-23 18:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-22 23:51 [Bug c++/66632] New: Incorrect use of default constructor to initialize isolated rvalue jasonxbell at gmail dot com
2015-06-22 23:57 ` [Bug c++/66632] " jasonxbell at gmail dot com
2015-06-23 18:14 ` daniel.kruegler at googlemail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).