Bug 60867 - std::atomic<std::unique_ptr<T>> should fail to compile since unique_ptr is not trivial to copy
Summary: std::atomic<std::unique_ptr<T>> should fail to compile since unique_ptr is no...
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.8.2
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-04-16 19:52 UTC by Kjell Hedstrom
Modified: 2014-04-17 14:06 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
main.ii compiled with: g++ -v -save-temps -std=c++11 -O2 -Wall -Wextra -pthread main.cpp (106.54 KB, text/plain)
2014-04-16 19:52 UTC, Kjell Hedstrom
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Kjell Hedstrom 2014-04-16 19:52:01 UTC
Created attachment 32621 [details]
main.ii compiled with:  g++ -v -save-temps  -std=c++11  -O2 -Wall -Wextra -pthread  main.cpp

std::atomic<T> is OK for T's that are not trivial to copy.  Example
std::atomic<std::unique_ptr<T>> should fail to g++ compile but is succeeds.

Example: 
std::atomic<std::unique_ptr<int>> a;

Expected output is a compilation error. Maybe something similar to what you would get from clang 3.4   Ref: http://coliru.stacked-crooked.com/view?id=eb48cd3c29b3be15


See attachment main.cpp with code example. It can be build with no problems
g++ -std=c++11  -O2 -Wall -Wextra -pedantic -pthread -pedantic-errors  main.cpp

the main.ii File was created with:
g++ -v -save-temps  -std=c++11  -O2 -Wall -Wextra -pthread  main.cpp


gcc -v gives:
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/probe/libexec/gcc/x86_64-linux-gnu/4.8.2/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../gcc-4.8.2/configure -build=x86_64-linux-gnu --prefix=/usr/local/probe --enable-cxx-flags='-L/usr/local/probe/lib -L/usr/local/probe/lib64 -Wl,-rpath -Wl,/usr/local/probe/lib64 -Wl,-rpath -Wl,/usr/local/probe/lib ' --enable-c-flags='-L/usr/local/probe/lib -L/usr/local/probe/lib64 -Wl,-rpath -Wl,/usr/local/probe/lib64 -Wl,-rpath -Wl,/usr/local/probe/lib ' --enable-shared --enable-threads=posix --enable-libstdcxx-time=rt --enable-checking=release --with-multilib-list=m64 --with-mpc=/usr/local/probe --enable-gather-detailed-mem-stats --enable-vtable-verify --with-mpc=/usr/local/probe/ --enable-languages=c,c++
Thread model: posix
gcc version 4.8.2 (GCC)


// Only one attachment could be added so the main.cpp is pasted in
// --- main.cpp ---
#include <iostream>
#include <atomic>
#include <memory>
#include <string>


int main() {
  std::atomic<std::unique_ptr<int>> a;  // this line should fail to compile.


   a.store(std::unique_ptr<int>(new int{5}));
   for (size_t index = 0; index < 100; ++index){
      a.load();	   
   }
    
   //typedef std::unique_ptr<int> IntPtr;
   // Hint: using gcc's internal 'has_trivial_copy' shows that it is NOT 
   // trivial to copy
   //std::cout << "Is copyable " << __has_trivial_copy(IntPtr)
   return 0;
}
Comment 1 Jonathan Wakely 2014-04-17 14:02:35 UTC
The standard does not require a diagnostic here, the error is in your code not the library.

However, I put the following in std::atomic two days ago:

  // TODO: static_assert(is_trivially_copyable<_Tp>::value, "");

As soon as we have a working implementation of is_trivially_copyable that can be enabled.
Comment 2 Kjell Hedstrom 2014-04-17 14:06:19 UTC
Sounds good. 

>> the error is in your code not the library

Yes. Code like that is obviously faulty. I am happy that you are considering putting in a static_assert. From a coders perspective I find it very helpful if the compiler would immediately find bugs of this nature. 

Cheers
Kjell