Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Mac OS X 10.7.4和FreeBSD 9.0上没有右值参考_C++_Macos_Boost_C++11_Rvalue - Fatal编程技术网

C++ Mac OS X 10.7.4和FreeBSD 9.0上没有右值参考

C++ Mac OS X 10.7.4和FreeBSD 9.0上没有右值参考,c++,macos,boost,c++11,rvalue,C++,Macos,Boost,C++11,Rvalue,这是我的密码 #include <iostream> #include <vector> int main() { std::vector<size_t> v1, v2; v1.push_back(3); v1.push_back(4); v2 = static_cast<std::vector<size_t>&&>(v1); std::cout << v1.size() <<

这是我的密码

#include <iostream>
#include <vector>
int main()
{
  std::vector<size_t> v1, v2;
  v1.push_back(3);
  v1.push_back(4);
  v2 = static_cast<std::vector<size_t>&&>(v1);
  std::cout << v1.size() << '\n';
  std::cout << v2.size() << '\n';  
}
clang++--std=c++11--stdlib=libc++rval.cpp

In file included from rval.cpp:1:
In file included from /usr/include/c++/v1/iostream:38:
In file included from /usr/include/c++/v1/ios:216:
In file included from /usr/include/c++/v1/__locale:15:
/usr/include/c++/v1/string:1952:10: error: overload resolution selected
      implicitly-deleted copy assignment operator
    __r_ = _STD::move(__str.__r_);
         ^
/usr/include/c++/v1/string:1942:9: note: in instantiation of member function
      'std::__1::basic_string<char, std::__1::char_traits<char>,
      std::__1::allocator<char> >::__move_assign' requested here
        __move_assign(__str, true_type());
        ^
/usr/include/c++/v1/string:1961:5: note: in instantiation of member function
      'std::__1::basic_string<char, std::__1::char_traits<char>,
      std::__1::allocator<char> >::__move_assign' requested here
    __move_assign(__str, integral_constant<bool,
    ^
/usr/include/c++/v1/algorithm:1595:19: note: in instantiation of member function
      'std::__1::basic_string<char, std::__1::char_traits<char>,
      std::__1::allocator<char> >::operator=' requested here
        *__result = _STD::move(*__first);
                  ^
/usr/include/c++/v1/algorithm:1619:12: note: in instantiation of function
      template specialization 'std::__1::__move<std::__1::basic_string<char> *,
      std::__1::basic_string<char> *>' requested here
    return _STD::__move(__unwrap_iter(__first), __unwrap_iter(__last),...
           ^
/usr/include/c++/v1/__config:153:14: note: expanded from macro '_STD'
#define _STD std::_LIBCPP_NAMESPACE
             ^
/usr/include/c++/v1/__split_buffer:557:22: note: in instantiation of function
      template specialization 'std::__1::move<std::__1::basic_string<char> *,
      std::__1::basic_string<char> *>' requested here
            __end_ = _STD::move(__begin_, __end_, __begin_ - __d);
                     ^
/usr/include/c++/v1/__config:153:14: note: expanded from macro '_STD'
#define _STD std::_LIBCPP_NAMESPACE
             ^
/usr/include/c++/v1/vector:1289:13: note: in instantiation of member function
      'std::__1::__split_buffer<std::__1::basic_string<char>,
      std::__1::allocator<std::__1::basic_string<char> > &>::push_back'
      requested here
        __v.push_back(_STD::move(__x));
            ^
rval.cpp:10:5: note: in instantiation of member function
      'std::__1::vector<std::__1::basic_string<char>,
      std::__1::allocator<std::__1::basic_string<char> > >::push_back' requested
      here
        v1.push_back("nice");
           ^
/usr/include/c++/v1/memory:1941:5: note: copy assignment operator is implicitly
      deleted because '__compressed_pair<std::__1::basic_string<char,
      std::__1::char_traits<char>, std::__1::allocator<char> >::__rep,
      std::__1::allocator<char> >' has a user-declared move constructor
    __compressed_pair(__compressed_pair&& __p)
    ^
1 error generated.
RVA.cpp中包含的文件中的
:1:
在/usr/include/c++/v1/iostream:38中包含的文件中:
在/usr/include/c++/v1/ios:216中包含的文件中:
在/usr/include/c++/v1/\u语言环境15中包含的文件中:
/usr/include/c++/v1/string:1952:10:错误:选择了重载分辨率
隐式删除的复制分配运算符
__r_u=u STD::move(u str.u r_u);
^
/usr/include/c++/v1/string:1942:9:注意:在成员函数的实例化中
此处请求了“std::_1::基本字符串::_移动\u分配”
__移动赋值(uuu str,true_type());
^
/usr/include/c++/v1/string:1961:5:注意:在成员函数的实例化中
此处请求了“std::_1::基本字符串::_移动\u分配”
__移动赋值(uuu str,整型常量)
此函数提供了在具有右值引用的编译器中将引用转换为右值引用的方法。对于其他编译器,将
T&
转换为
::boost::rv&
,以便激活移动模拟


如果std::vector::operator=(boost::rv&)没有定义,可能是回到了常规的副本构造。我认为您必须做一些特殊的事情来让类处理boost::rvs

GCC4.2是古老的,所以它的
std::vector
不支持右值引用。使用
boost::move
不会改变这一点,它无法神奇地将右值引用支持添加到不支持它的编译器+库。这意味着它执行的是复制而不是移动

我怀疑当你在Mac上使用Clang时,它使用的是GCC4.2中的旧标准库,即使编译器支持右值引用,它仍然不支持右值引用。你能改用应该支持C++11的libc++库吗

在Linux上使用Clang时,它使用GCC4.7中的标准库,该库支持C++11


注意:Clang 3.1和GCC 4.7都支持
-std=c++11
而不是
-std=c++0x
,我认为Clang 3.0也支持。如果您的所有编译器都为选项使用较新的名称,您也可以使用它。

您应该使用std::move来激活移动分配操作符。即
v2=move(v1)问题是为什么boost::move(v1)不将v1移到v2。我认为与不完整/错误的C++11标准实现作斗争是不值得的。要么安装gcc 4.7.0(或更好)或者等到你的操作系统有了它们。@walter gcc 4.7不完整,我确信它有一些错误。但是gcc支持右值引用已经有一段时间了。你试过clang++--std=c++11--stdlib=libc吗++
#include <iostream>
#include <string>
#include <vector>
int main()
{
  std::vector<std::string> v1, v2;
  v1.push_back("hello");
  /*
  v2 = static_cast<std::vector<std::string>&&>(v1);
  std::cout << v1.size() << '\n';
  std::cout << v2.size() << '\n';  
  */
}
/uac/gds/hjli/clang+llvm-3.1-x86_64-apple-darwin11/bin/clang
In file included from rval.cpp:1:
In file included from /usr/include/c++/v1/iostream:38:
In file included from /usr/include/c++/v1/ios:216:
In file included from /usr/include/c++/v1/__locale:15:
/usr/include/c++/v1/string:1952:10: error: overload resolution selected
      implicitly-deleted copy assignment operator
    __r_ = _STD::move(__str.__r_);
         ^
/usr/include/c++/v1/string:1942:9: note: in instantiation of member function
      'std::__1::basic_string<char, std::__1::char_traits<char>,
      std::__1::allocator<char> >::__move_assign' requested here
        __move_assign(__str, true_type());
        ^
/usr/include/c++/v1/string:1961:5: note: in instantiation of member function
      'std::__1::basic_string<char, std::__1::char_traits<char>,
      std::__1::allocator<char> >::__move_assign' requested here
    __move_assign(__str, integral_constant<bool,
    ^
/usr/include/c++/v1/algorithm:1595:19: note: in instantiation of member function
      'std::__1::basic_string<char, std::__1::char_traits<char>,
      std::__1::allocator<char> >::operator=' requested here
        *__result = _STD::move(*__first);
                  ^
/usr/include/c++/v1/algorithm:1619:12: note: in instantiation of function
      template specialization 'std::__1::__move<std::__1::basic_string<char> *,
      std::__1::basic_string<char> *>' requested here
    return _STD::__move(__unwrap_iter(__first), __unwrap_iter(__last),...
           ^
/usr/include/c++/v1/__config:153:14: note: expanded from macro '_STD'
#define _STD std::_LIBCPP_NAMESPACE
             ^
/usr/include/c++/v1/__split_buffer:557:22: note: in instantiation of function
      template specialization 'std::__1::move<std::__1::basic_string<char> *,
      std::__1::basic_string<char> *>' requested here
            __end_ = _STD::move(__begin_, __end_, __begin_ - __d);
                     ^
/usr/include/c++/v1/__config:153:14: note: expanded from macro '_STD'
#define _STD std::_LIBCPP_NAMESPACE
             ^
/usr/include/c++/v1/vector:1289:13: note: in instantiation of member function
      'std::__1::__split_buffer<std::__1::basic_string<char>,
      std::__1::allocator<std::__1::basic_string<char> > &>::push_back'
      requested here
        __v.push_back(_STD::move(__x));
            ^
rval.cpp:10:5: note: in instantiation of member function
      'std::__1::vector<std::__1::basic_string<char>,
      std::__1::allocator<std::__1::basic_string<char> > >::push_back' requested
      here
        v1.push_back("nice");
           ^
/usr/include/c++/v1/memory:1941:5: note: copy assignment operator is implicitly
      deleted because '__compressed_pair<std::__1::basic_string<char,
      std::__1::char_traits<char>, std::__1::allocator<char> >::__rep,
      std::__1::allocator<char> >' has a user-declared move constructor
    __compressed_pair(__compressed_pair&& __p)
    ^
1 error generated.
rvalue_reference boost::move(input_reference)