libc ++实例化异常规范的向量和字符串编译简单代码时出错

时间:2013-09-22 22:34:43

标签: c++11 clang libc++

编译这个简单的代码时出现错误,如果我删除了noexcept,我就不会收到错误:

#include<vector>
#include<string>
class Foo
{
public:

protected:
   Foo(Foo&&) noexcept = default;
   Foo& operator=(Foo&&) noexcept = default;

   Foo()
   {
   }

private:

   std::vector<std::string> vectorFoo_;
};

这是我得到的错误,我理解它是在异常规范的实例化中,但是我怀疑std :: string是不是没有移动构造,...第一个错误让我觉得有一个bug。

  

包含来自的文件   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1:   在包含的文件中   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:4:   /usr/local/bin/../lib/c++/v1/vector:575:67:错误:没有成员命名   &#39;值&#39;在   &#39; std :: __ 1 :: is_nothrow_move_constructible,std :: __ 1 :: allocator&gt; &GT; &GT;&#39;            NOEXCEPT (is_nothrow_move_constructible ::值);                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^包括在内   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1:   在包含的文件中   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:4:   在/usr/local/bin/../lib/c++/v1/vector:264中包含的文件中:   /usr/local/bin/../lib/c++/v1/__config:325:34:注意:扩展自   宏&#39; NOEXCEPT &#39;   define NOEXCEPT (x)noexcept(x)                                    ^在/Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1中包含的文件中:   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:29:29:   注意:在实例化&#39; vector&#39;的异常规范中   这里请求std :: vector vectorFoo_;                               ^在/Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1中包含的文件中:   在包含的文件中   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:4:   在/usr/local/bin/../lib/c++/v1/vector:265中包含的文件中:在文件中   包括在/usr/local/bin/../lib/c++/v1/__bit_reference:15:在文件中   包含在/usr/local/bin/../lib/c++/v1/algorithm:624中:   /usr/local/bin/../lib/c++/v1/type_traits:2676:53:注意:在   模板类的实例化   &#39; std :: __ 1 :: add_rvalue_reference,std :: __ 1 :: allocator&gt; &GT; &GT;&#39;   这里要求       :public is_nothrow_constructible&lt; _Tp,typename add_rvalue_reference&lt; Tp&gt; :: type&gt;                                                       ^在包含的文件中   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1:   在包含的文件中   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:4:   /usr/local/bin/../lib/c++/v1/vector:575:20:注意:在实例化中   模板类   &#39; std :: _1 :: is_nothrow_move_constructible,std :: __ 1 :: allocator&gt; &GT; &GT;&#39;   这里要求            NOEXCEPT (is_nothrow_move_constructible ::值);                      ^在/Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1中包含的文件中:   在包含的文件中   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:4:   在/usr/local/bin/../lib/c++/v1/vector:264中包含的文件中:   /usr/local/bin/../lib/c++/v1/__config:325:34:注意:扩展自   宏&#39; NOEXCEPT &#39;   define NOEXCEPT (x)noexcept(x)                                    ^在/Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1中包含的文件中:   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:29:29:   注意:在实例化&#39; vector&#39;的异常规范中   这里请求std :: vector vectorFoo_;                               ^在/Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1中包含的文件中:   在包含的文件中   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:4:   /usr/local/bin/../lib/c++/v1/vector:582:58:错误:没有成员命名   &#39;值&#39;在   &#39; std :: __ 1 :: is_nothrow_move_assignable,std :: __ 1 :: allocator&gt; &GT; &GT;&#39;                is_nothrow_move_assignable ::值);                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1:   在包含的文件中   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:4:   在/usr/local/bin/../lib/c++/v1/vector:264中包含的文件中:   /usr/local/bin/../lib/c++/v1/__config:325:34:注意:扩展自   宏&#39; NOEXCEPT &#39;   define NOEXCEPT (x)noexcept(x)                                    ^在/Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1中包含的文件中:   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:29:29:   注意:在&#39; operator =&#39;的异常规范实例化中   这里请求std :: vector vectorFoo_;                               ^在/Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1中包含的文件中:   在包含的文件中   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:4:   在/usr/local/bin/../lib/c++/v1/vector:265中包含的文件中:在文件中   包括在/usr/local/bin/../lib/c++/v1/__bit_reference:15:在文件中   包含在/usr/local/bin/../lib/c++/v1/algorithm:624中:   /usr/local/bin/../lib/c++/v1/type_traits:2760:45:注意:在   模板类的实例化   &#39; std :: __ 1 :: add_lvalue_reference,std :: __ 1 :: allocator&gt; &GT; &GT;&#39;   这里要求       :public is_nothrow_assignableTp&gt; :: type,                                               ^在/Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1中包含的文件中:   在包含的文件中   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:4:   /usr/local/bin/../lib/c++/v1/vector:582:14:注意:在实例化中   模板类   &#39; std :: _ 1 :: is_nothrow_move_assignable,std :: __ 1 :: allocator&gt; &GT; &GT;&#39;   这里要求                is_nothrow_move_assignable ::值);                ^在/Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1中包含的文件中:   在包含的文件中   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:4:   在/usr/local/bin/../lib/c++/v1/vector:264中包含的文件中:   /usr/local/bin/../lib/c++/v1/__config:325:34:注意:扩展自   宏&#39; NOEXCEPT &#39;   define NOEXCEPT (x)noexcept(x)                                    ^在/Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1中包含的文件中:   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:29:29:   注意:在&#39; operator =&#39;的异常规范实例化中   这里请求std :: vector vectorFoo_;                               ^在/Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1中包含的文件中:   在包含的文件中   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:4:   在/usr/local/bin/../lib/c++/v1/vector:265中包含的文件中:在文件中   包括在/usr/local/bin/../lib/c++/v1/__bit_reference:15:在文件中   包含在/usr/local/bin/../lib/c++/v1/algorithm:624中:   /usr/local/bin/../lib/c++/v1/type_traits:2676:53:注意:在   模板类的实例化   &#39; std :: __ 1 :: add_rvalue_reference,std :: __ 1 :: allocator&gt; &GT; &GT;&#39;   这里要求       :public is_nothrow_constructible&lt; _Tp,typename add_rvalue_reference&lt; Tp&gt; :: type&gt;                                                       ^在包含的文件中   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1:   在包含的文件中   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:4:   /usr/local/bin/../lib/c++/v1/vector:575:20:注意:在实例化中   模板类   &#39; std :: _1 :: is_nothrow_move_constructible,std :: __ 1 :: allocator&gt; &GT; &GT;&#39;   这里要求            NOEXCEPT (is_nothrow_move_constructible ::值);                      ^在/Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1中包含的文件中:   在包含的文件中   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:4:   在/usr/local/bin/../lib/c++/v1/vector:264中包含的文件中:   /usr/local/bin/../lib/c++/v1/__config:325:34:注意:扩展自   宏&#39; NOEXCEPT &#39;   define NOEXCEPT (x)noexcept(x)                                    ^在/Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.cpp:1中包含的文件中:   /Users/Mani/Development/Projects/flowOfLife/Graphics/Graphics/TestCompileError.h:29:29:   注意:在实例化&#39; vector&#39;的异常规范中   这里请求std :: vector vectorFoo _;

我的编译器是从树顶部开始的。 我做错了什么?

由于

更新 The error on Coliru。如果我没有指定libc ++,而是使用stdlibc ++,它就会编译。它开始看起来很像libc ++中的一个bug。

2 个答案:

答案 0 :(得分:5)

代码很好。 (有趣的是,libc ++也是如此)

这是铿锵声中的一个错误;事实上,最近版本的clang在尝试编译此代码时崩溃。

目前正在测试在clang中修复此问题的修补程序,它将成为3.4版本的一部分。 如果您不能等待那么久,那么如果您愿意从源代码构建它,那么您应该能够“很快就能获得”固定版本的clang。

感谢Nuanti的Alp追踪clang bug。

答案 1 :(得分:3)

我的第一个假设是,这可能是因为std::string的移动赋值运算符不是noexcept(需要检查见下文)。

至少,clang和gcc似乎更喜欢这个:

Bar& operator = (Bar&&) 
     noexcept(
         noexcept(std::declval<std::string>() = std::string())
     ) = default;

现在,关于Foo clang和gcc不太同意,但这里的 Clang live on coliru

情节扭曲

更多值得深思:标准似乎明确地将std :: string的赋值运算符(以及移动构造函数)指定为noexcept§21.4.2

http://i.imgur.com/lmjq3dX.png