- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个返回字符串 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<>
的构造函数(临时值是右值)。
如果您发现术语左值 和右值 令人困惑,您可以(直观地)将左值视为:
它们通常表示可以在程序中重复引用的对象,具有稳定的标识,从中隐式移动是不安全的(因为它们可以稍后在程序中引用)。
相反,您可以将右值视为:
42
、 false
和 3.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/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!