gpt4 book ai didi

c++ - 将一个函数返回的 vector 传递给另一个函数

转载 作者:搜寻专家 更新时间:2023-10-31 01:08:56 26 4
gpt4 key购买 nike

我有一个返回字符串 vector 的函数

std::vector<std::string> getNames()
{
std::vector<std::string> names;
names.push_back("one");
return names;
}

我有另一个类,其构造函数具有以下签名(这实际上来自 TCLAP):

ValuesConstraint(std::vector<T>& allowed);

如果我尝试执行以下操作,我会收到错误消息

ValuesConstraint<std::string> A( getNames() );

但不是当我执行以下操作时

std::vector<std::string> v = getNames();
ValuesConstraint<std::string> A( v );

这发生在 GCC 4.4.7 中,但不会出现在 VS2010 中。错误如下:

error: no matching function for call to ‘TCLAP::ValuesConstraint<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::ValuesConstraint(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)’

note: candidates are: TCLAP::ValuesConstraint<T>::ValuesConstraint(std::vector<T, std::allocator<_CharT> >&) [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]

note: TCLAP::ValuesConstraint<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::ValuesConstraint(const TCLAP::ValuesConstraint<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >&)

为什么会出现这个错误,如何将返回值直接传递给构造函数?

最佳答案

Why is this error occurring, and what can I do to pass the return value directly to the constructor?

ValuesConstraint<> 的构造函数接受左值(对非 const 的左值引用只能绑定(bind)到左值),而不返回引用类型的函数调用表达式是右值

左值引用无法绑定(bind)到右值,因此您不能将临时值传递给 ValuesConstraint<> 的构造函数(临时值是右值)。

如果您发现术语左值右值 令人困惑,您可以(直观地)将左值视为:

  • 命名变量(实际上是标识符);和/或
  • 你可以取地址的对象(实际上是表达式)

它们通常表示可以在程序中重复引用的对象,具有稳定的标识,从中隐式移动是安全的(因为它们可以稍后在程序中引用)。

相反,您可以将右值视为:

  • 未命名的对象,如临时对象;和/或
  • 文字(如 42false3.14 );和/或
  • 表达式表示具有“不稳定”身份的对象(如 std::move() 的结果),即将从中移出

它们通常表示不能在程序中重复引用的对象(或者 promise 在通过显式调用 std::move() 重新分配它们之前不再引用的对象)并且是,因此,可以安全地离开。

尽管对上述内容持保留态度 - 它只是一个指南,您可以使用它来建立一些关于什么是值类别以及如何判断某个表达式的值类别是什么的直觉。在形式上,事情有点复杂,上述概括并不总是正确的。

所以回到你的例子,在这种情况下:

std::vector<std::string> v = getNames();
// ^
// Named variable! Can take its address! lvalue!
ValuesConstraint<std::string> A( v );
// ^
// OK! The constructor accepts an lvalue
// reference to non-const, and I am giving
// it an lvalue

您将左值传递给构造函数,一切正常。另一方面,在这种情况下:

ValuesConstraint<std::string> A( getNames() );
// ^^^^^^^^^^
// getNames() returns a vector by value, this
// means a temporary will be constructed as the
// return value of the function, and temporaries
// are unnamed... so, that's an rvalue!
// And the constructor accepts an lvalue reference
// to non-const! So this is an ERROR!

您正在尝试将右值传递给接受非 const 左值引用的函数(换句话说,它需要一个可修改的左值)。这是非法的,编译器会提示。

This occurs in GCC 4.4.7 but not in VS2010

这是因为 VC10 实现了一个不一致的编译器扩展,允许将右值绑定(bind)到左值引用到非 const .不过请注意,这不是标准行为,如果您想编写可移植代码,则不应依赖它(实际上,我建议您无论如何都不要依赖它)。

As suggested by Jesse Good in the comments ,将警告级别提高到 4,以便从编译器获得警告并检测您正在使用此非标准扩展的情况。

关于c++ - 将一个函数返回的 vector 传递给另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17535712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com