[llvm-bugs] [Bug 37852] New: A tricky code sample

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Jun 19 04:25:58 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=37852

            Bug ID: 37852
           Summary: A tricky code sample
           Product: clang
           Version: unspecified
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: zhonghao at pku.org.cn
                CC: dgregor at apple.com, llvm-bugs at lists.llvm.org

The code is as follow:

 #define _GLIBCXX_DEBUG
 #include <vector>

 struct T { int i; };

 int swap_calls;

 namespace std
 {
 template<>
 void
 vector<T, allocator<T> >::swap(vector<T, allocator<T> >&)
 { ++swap_calls; }
 }

 // Should use vector specialization for swap.
 int main()
 {
 std::vector<T> A;
 std::vector<T> B;
 swap_calls = 0;
 std::swap(A, B);
 return swap_calls;
 }

It comes from a gcc bug report:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=16021

The programmers of gcc have a long discussion on whether the above code is
legal. 

I tried the latest g++, and it accepts the above code, but clang++ rejects it.
The error message is as follow:

code0.c.cpp:12:28: error: 'swap' is missing exception specification
'noexcept(noexcept(declval<std::__debug::vector<T, std::allocator<T> >::_Base
&>().swap(__x)))'
     vector<T, allocator<T> >::swap(vector<T, allocator<T> >&)
                               ^
                                                              
noexcept(noexcept(declval<std::__debug::vector<T, std::allocator<T> >::_Base
&>().swap(__x)))
   
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/8.0.1/../../../../include/c++/8.0.1/debug/vector:688:7:
note: previous declaration is here
          swap(vector& __x)
          ^
    1 error generated.

You may have interests in reading the gcc bug report, and I am curious how you
consider the code sample. Is it buggy or not?

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180619/4f732b63/attachment.html>


More information about the llvm-bugs mailing list