[cfe-dev] Non Static Member Initialization and deleted copy constructor

Richard Smith richard at metafoo.co.uk
Thu Jul 26 13:25:35 PDT 2012


Ouch.

This isn't restricted to non-static member initializers, it's a problem
with the way we instantiate direct-list-initialization in general:

#include <atomic>
template<typename> struct X {
  int f() {
    std::atomic<int> v{0};
    return v;
  }
};
int k = X<int>().f();

<stdin>:4:22: error: copying variable of type 'std::atomic<int>' invokes
deleted constructor
    std::atomic<int> v{0};
                     ^ ~
<stdin>:8:18: note: in instantiation of member function 'X<int>::f'
requested here
int k = X<int>().f();
                 ^
[...]

On Thu, Jul 26, 2012 at 12:30 PM, Howard Hinnant <hhinnant at apple.com> wrote:

> This looks wrong to me.  Is this a known bug?
>
> #include <atomic>
>
> //
> // This class can compile
> //
> class Working
> {
> private:
>     std::atomic<int> value_{0};
> };
>
> //
> // This class cannot compile
> //
> template <typename Ty1>
> class NotWorking
> {
>     private:
>         std::atomic<int> value_{0}; // <---- error here
> };
>
> int main()
> {
>     Working working;
>     NotWorking<int> not_working;
> }
>
> test.cpp:19:33: error: copying member subobject of type 'std::atomic<int>'
> invokes deleted constructor
>         std::atomic<int> value_{0}; // <---- error here
>                                 ^
> test.cpp:25:21: note: in instantiation of template class 'NotWorking<int>'
> requested here
>     NotWorking<int> not_working;
>                     ^
> /Users/hhinnant/Development/temp_libcxx/include/atomic:721:7: note: copy
> constructor of 'atomic<int>' is implicitly deleted because base class
>       '__atomic_base<int>' has a deleted copy constructor
>     : public __atomic_base<_Tp>
>       ^
> /Users/hhinnant/Development/temp_libcxx/include/atomic:640:7: note: copy
> constructor of '__atomic_base<int, true>' is implicitly deleted because
> base class
>       '__atomic_base<int, false>' has a deleted copy constructor
>     : public __atomic_base<_Tp, false>
>       ^
> /Users/hhinnant/Development/temp_libcxx/include/atomic:625:5: note:
> function has been explicitly marked deleted here
>     __atomic_base(const __atomic_base&) = delete;
>     ^
> 1 error generated.
>
> Thanks,
> Howard
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120726/aeab64e0/attachment.html>


More information about the cfe-dev mailing list