Bug 41518 - copy initialization of volatile objects
Summary: copy initialization of volatile objects
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: 4.9.1
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
: 110685 (view as bug list)
Depends on:
Blocks:
 
Reported: 2009-09-30 12:18 UTC by Wolfgang Roehrl
Modified: 2023-07-16 16:50 UTC (History)
4 users (show)

See Also:
Host: i686-pc-mingw32
Target: powerpc-rtems4.9
Build: athlon-redhat-linux
Known to work:
Known to fail: 4.7.0
Last reconfirmed: 2011-12-09 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Wolfgang Roehrl 2009-09-30 12:18:26 UTC
Dear all,

I would like to post a fault report for the GNU C/C++ compiler 4.3.0.

Used invokation line for the GNU C++ compiler:

gcc -c -x c++ -ansi -Wall -Werror -mcpu=603e -fverbose-asm -mbig
    -mmultiple -mstring -mstrict-align -meabi -msdata -fno-common
    -fno-exceptions -fno-rtti -O3 -fno-section-anchors
    -I<different include paths>
    -D<different #define's>
    X.CPP -oX.O


// example program

struct X
{
    X (void*);
};

int i;

volatile X ax1(&i);
volatile X ax2 = &i;           // <--- line 9

const volatile X bx1(&i);
const volatile X bx2 = &i;     // <--- line 12


GNU rejects the code fragment with the following error messages:

X.CPP:9: error: no matching function for call to 'X::X(volatile X)'
X.CPP:3: note: candidates are: X::X(void*)
X.CPP:2: note:                 X::X(const X&)
X.CPP:12: error: no matching function for call to 'X::X(const volatile X)'
X.CPP:3: note: candidates are: X::X(void*)
X.CPP:2: note:                 X::X(const X&)

The initializations in lines 9 and 12 are copy-initializations with a call to
the converting constructor "X::X (void*)". The result of this call is a
temporary of type "X" (and not of type "[const] volatile X"), which can be
copied with the implicitly-generated copy constructor "X::X (const X&)".

BTW, the Comeau online compiler accepts the code above. 

Kind regards
W. Roehrl
Comment 1 Sean McGovern 2011-12-08 18:59:04 UTC
GCC 4.3 has been unsupported for some time.

Go ahead and close this bug.

If this issue can be reproduced with a supported version of GCC, please re-open it.
Comment 2 Richard Biener 2011-12-09 09:08:05 UTC
EDG also accepts this, 4.7.0 still fails to accept it.

Confirmed.
Comment 3 lucdanton 2012-02-15 15:08:11 UTC
Reproducible with const:

$ cat main.cpp
struct copyable {
    copyable() {}
    copyable(copyable&) {}
};

struct wrapper {
    copyable c;

    wrapper(int) {}
};

int
main()
{
    wrapper const w = 0;
}

$ g++-snapshot -Wall -Wextra -pedantic main.cpp 
main.cpp: In function 'int main()':
main.cpp:15:23: error: no matching function for call to 'wrapper::wrapper(const wrapper)'
main.cpp:15:23: note: candidates are:
main.cpp:9:5: note: wrapper::wrapper(int)
main.cpp:9:5: note:   no known conversion for argument 1 from 'const wrapper' to 'int'
main.cpp:6:8: note: wrapper::wrapper(wrapper&)
main.cpp:6:8: note:   no known conversion for argument 1 from 'const wrapper' to 'wrapper&'

$ g++-snapshot -Wall -Wextra -pedantic -std=c++11 main.cpp  
main.cpp: In function 'int main()':
main.cpp:15:23: error: no matching function for call to 'wrapper::wrapper(const wrapper)'
main.cpp:15:23: note: candidates are:
main.cpp:9:5: note: wrapper::wrapper(int)
main.cpp:9:5: note:   no known conversion for argument 1 from 'const wrapper' to 'int'
main.cpp:6:8: note: wrapper::wrapper(wrapper&)
main.cpp:6:8: note:   no known conversion for argument 1 from 'const wrapper' to 'wrapper&'

$ g++-snapshot -v
 ...
gcc version 4.7.0 20120128 (experimental) [trunk revision 183664] (Debian 20120128-1)

I noticed a similar problem when using copy-initialization on a const object of a class that is movable (with move constructor accepting T&&, not T const&&) but not copyable. I noticed that in C++11 8.5 Initializers [dcl.init] takes great care to specify:

> [...] if the function is a constructor, the call initializes a temporary of the cv-unqualified version of the destination type.

on paragraph 16 (I'm using n3290). I'm not as familiar with C++03 but apparently the text only mentions creation of a temporary, without exactly specifying of which type.
Comment 4 Paolo Carlini 2012-02-15 16:04:41 UTC
Let's ask Jason to double check the triage.
Comment 5 Jason Merrill 2012-02-15 19:49:29 UTC
I agree with the analysis.
Comment 6 Andrew Pinski 2021-07-24 02:02:25 UTC
Fixed in GCC 4.9.1.
Comment 7 Andrew Pinski 2023-07-16 16:50:58 UTC
*** Bug 110685 has been marked as a duplicate of this bug. ***