C++11 C2664无法转换为&&;价值
编译器希望我的左值是右值引用,我不明白为什么 我的问题是:C++11 C2664无法转换为&&;价值,c++11,lambda,compiler-errors,lvalue-to-rvalue,C++11,Lambda,Compiler Errors,Lvalue To Rvalue,编译器希望我的左值是右值引用,我不明白为什么 我的问题是: 为什么“dataLen”是常量,即使它被声明为非常量并且lambda被告知通过引用捕获作为默认值 为什么编译器会尝试转换为右值引用“unsigned\uu int64&&”,即使它被声明为tupleByteVector\u内容的“unsigned long long”(无右值引用) 我认为这是因为lambda捕获,但请参见下面的简化工作流程: void read_socket() { std::vector<std::tupl
void read_socket()
{
std::vector<std::tuple<unsigned long long, std::vector<unsigned char>>> tupleByteVector_content;
read_socket_readSome(tupleByteVector_content, [this, &tupleByteVector_content]() {
//use tuple vector
});
}
//catch the tuple vector by reference
void read_socket_readSome(std::vector<std::tuple<unsigned long long, const std::shared_ptr<Session>& session, std::vector<unsigned char>>> & tupleByteVector_content, std::function<void()> && continueReadFunction)
{
//Read data length from a asio socket
std::shared_ptr<asio::streambuf> len_buffer = std::make_shared<asio::streambuf>();
asio::async_read(session->connection->socket->next_layer(), *len_buffer, asio::transfer_exactly(1), [&,
this, session, len_buffer, tupleByteVector_content, continueReadFunction](const error_code& ec, std::size_t bytes_transferred) {
//the first value I want to save
unsigned long long dataLen = BytesToLength(len_buffer);
//Read data from a asio socket
std::shared_ptr<asio::streambuf> data_buffer = std::make_shared<asio::streambuf>();
asio::async_read(session->connection->socket->next_layer(), *data_buffer, asio::transfer_exactly(dataLen), [&, this, dataLen, data_buffer, tupleByteVector_content, session, continueReadFunction](const error_code& ec, std::size_t bytes_transferred) {
//ERROR HERE: ----------->
std::tuple<unsigned long long, std::vector<unsigned char>> t =
std::make_tuple<unsigned long long, std::vector<unsigned char>>(
dataLen, // ERROR C2664, cant convert argument 1 from "const unsigned __int64" to "unsigned __int64 &&"
{ asio::buffers_begin(data_buffer->data()), asio::buffers_end(data_buffer->data()) });
//ERROR HERE: <-----------
tupleByteVector_content.push_back(t);
continueReadFunction();
});
});
}
void read_socket()
{
std::向量元组向量_内容;
read\u socket\u readSome(tupleByteVector\u content,[此,&tupleByteVector\u content](){
//使用元组向量
});
}
//通过引用捕获元组向量
void read\u socket\u readSome(std::vector和tupleByteVector\u内容、std::function和continueradfunction)
{
//从asio插槽读取数据长度
std::shared_ptr len_buffer=std::make_shared();
asio::async_read(会话->连接->套接字->下一层(),*len_缓冲区,asio::transfer_正好(1),[&,
此,会话,len\u缓冲区,tupleByteVector\u内容,continueReadFunction](const error\u code&ec,std::size\u t bytes\u传输){
//我要保存的第一个值
无符号长数据长度=ByTestToLength(长度缓冲区);
//从asio插槽读取数据
std::shared_ptr data_buffer=std::make_shared();
asio::异步读取(会话->连接->套接字->下一层(),*数据缓冲区,asio::精确传输(数据长度),[&,this,dataLen,数据缓冲区,tupleByteVector\u内容,会话,continueReadFunction](常量错误代码&ec,std::大小字节传输){
//此处错误:------------>
std::tuple t=
std::make_tuple(
dataLen,//错误C2664,无法将参数1从“const unsigned\uu int64”转换为“unsigned\uu int64&”
{asio::buffers\u begin(data\u buffer->data()),asio::buffers\u end(data\u buffer->data())};
//这里的错误:data()),asio::buffers\u end(data\u buffer->data())};
但将_推回向量会产生错误:
错误C2663:[…]::push_back”:对于2个重载,此指针没有转换(从我自己自由翻译成英语)
dataLen
被视为常量,因为您通过值捕获它:
[&, this, dataLen,
^^^
mutable
添加到lambda的定义中
make_tuple
时,您应该依赖模板参数推断,而不是像您那样以显式方式放置类型。您问题的简短版本:
int i;
std::tuple<int> t = std::make_tuple<int>(i);
inti;
std::tuple t=std::make_tuple(i);
i
是命名对象,所以它是左值。通过make\u tuple
您可以使make\u tuple
签名看起来像:make\u tuple(int&)
。这是编译器抱怨的地方,因为作为左值的i
不能绑定到右值引用。通过参数推断,make\u tuple
的参数被推断为:int&
,在这种情况下,i
可以绑定
push_back
onvector
不起作用,因为您再次通过值捕获向量。push_back
修改对象,这在调用const对象时是不允许的。您应该通过引用捕获它
dataLen
被视为常量,因为您通过值捕获它:
[&, this, dataLen,
^^^
mutable
添加到lambda的定义中
make_tuple
时,您应该依赖模板参数推断,而不是像您那样以显式方式放置类型。您问题的简短版本:
int i;
std::tuple<int> t = std::make_tuple<int>(i);
inti;
std::tuple t=std::make_tuple(i);
i
是命名对象,所以它是左值。通过make\u tuple
您可以使make\u tuple
签名看起来像:make\u tuple(int&)
。这是编译器抱怨的地方,因为作为左值的i
不能绑定到右值引用。通过参数推断,make\u tuple
的参数被推断为:int&
,在这种情况下,i
可以绑定
push_-back
onvector
不起作用,因为您再次通过值捕获了vector。push_-back
修改对象,这在调用const对象时是不允许的。您应该通过引用捕获它。您能指出获取错误的代码行吗E和//ERROR C2664你能指出你得到的代码行吗?在代码中查找注释://ERROR HERE和//ERROR C2664谢谢你,在你的帮助下,我可以编译代码。但我不理解第2点。我是命名的和左值的,但在每一个其他函数中,我都可以像我一样提供左值。例如,这确实可以编译:std::unique_ptr l=std::make_unique(数据长度)这是第一次,这可能是第一次,我必须不太明确地写C++代码来让它工作。我希望这是脚本语言,但不是C++。我希望至少有一种明确的方式来写这个,比如:STD::tuple t= STD::make_tuple
使用转发引用作为其参数,make_unique
也使用,但是通过编写make_unique
您只指定T
,而不是Args
,因此Args
是通过转发引用规则推导出来的,这就是为什么您的示例可以工作的原因。当传递左值时,Args
是Args&
,当传递右值参数时
将是参数&&
。谢谢,在您的帮助下,我可以编译代码。但我不理解第2点。我