在c++lldb中使用重载运算符计算表达式
Evaluating an expression with overloaded operators in c++ lldb
我正在使用lldb调试Xcode 5中的C++程序,我想在调试器中评估任意表达式,特别是那些使用重载运算符的表达式。
例如,我创建了一个非常简单的Xcode 5 C++项目,其中包含以下main.cpp和所有编译器/链接器等选项设置为默认值:
#include <iostream>
#include <vector>
int main(int argc, const char * argv[])
{
std::vector<int> vec;
vec.push_back(42);
std::cout << "vec[0] = " << vec[0] << std::endl;
return 0;
}
我在return 0;
行设置了一个断点并运行了程序。
然后,在lldb提示下,打印矢量作为一个整体可以很好地工作:
(lldb) expr vec
(std::__1::vector<int, std::__1::allocator<int> >) $0 = size=1 {
[0] = 42
}
但是,我无法使用重载的operator[]
:访问其成员
(lldb) expr vec[0]
error: call to a function 'std::__1::vector<int, std::__1::allocator<int> >::operator[](unsigned long)' ('_ZNSt3__16vectorIiNS_9allocatorIiEEEixEm') that is not present in the target
error: The expression could not be prepared to run in the target
同样,我无法获得迭代器(尽管我在这里的经验较少,所以我的语法可能是错误的):
(lldb) expr vector<int>::iterator it = vec.begin()
error: use of undeclared identifier 'vector'
error: expected '(' for function-style cast or type construction
error: expected '(' for function-style cast or type construction
error: 3 errors parsing expression
和
(lldb) expr (vector<int>::iterator) vec.begin()
error: use of undeclared identifier 'vector'
error: expected '(' for function-style cast or type construction
error: expected '(' for function-style cast or type construction
error: 3 errors parsing expression
类似地,打印一个简单的字符串效果很好:
(lldb) expr string("a")
(std::__1::string) $0 = "a"
然而,一个简单的字符串串联失败了:
(lldb) expr string("a") + string("b")
error: invalid operands to binary expression ('string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') and 'string')
error: 1 errors parsing expression
我做错了什么?lldb是否支持重载运算符的求值?
提前谢谢!
我刚刚遇到了同样的问题,显然找到了一个简单的解决方案。您可以访问向量vec
的第i个元素,如下所示:
(lldb) p vec.__begin_[i]
(int) $1 = 100
请注意,C++标准库的设置使它们能够合理地内联所有模板化函数,并且不存在真正的函数副本。例如,当您调用std::vector<int>::begin()
时,没有这样的函数。它的所有用途都已内联。
这就是为什么您会收到关于"调用函数…目标中不存在"的错误。可能有函数的内联副本,但我们实际上无法调用。举个例子,如果我构建一个小C++程序,它生成一个std::vector,并将一些元素推到上面,然后对它们进行迭代,然后执行:
(lldb) image lookup -r -n begin
2 matches found in /private/tmp/vector:
Address: vector[0x0000000100000eaf] (vector.__TEXT.__text + 1071)
Summary: vector`main + 1071 [inlined] std::__1::vector<int, std::__1::allocator<int> >::begin() at vector.cpp:12
vector`main + 1071 at vector.cpp:12 Address: vector[0x0000000100000eaf] (vector.__TEXT.__text + 1071)
Summary: vector`main + 1071 [inlined] std::__1::vector<int, std::__1::allocator<int> >::begin() at vector.cpp:12
vector`main + 1071 at vector.cpp:12
所以begin&CCD_ 5的末端访问器是内联的。在来自stdc库本身的部分中:
12 matches found in /usr/lib/libc++.1.dylib:
Address: libc++.1.dylib[0x000000000003e4ec] (libc++.1.dylib.__TEXT.__text + 252188)
Summary: libc++.1.dylib`std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::begin() Address: libc++.1.dylib[0x000000000003e51c] (libc++.1.dylib.__TEXT.__text + 252236)
Summary: libc++.1.dylib`std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::begin() const Address: libc++.1.dylib[0x000000000003e574] (libc++.1.dylib.__TEXT.__text + 252324)
还有一些关于basic_string的内容,仅此而已。因此,我们无法调用任何真正的实现。一旦我们对这些std对象的真实世界只有一点点了解,当你开始推动它时,这个世界就会以其他奇怪的方式分崩离析
lldb目前还不够聪明,无法从C++标准库的头文件中重新构造模板化的函数/方法。我们没有足够的环境来完成该任务,您的代码最初是在其中编译的。
请注意,这并不是重载运算符的问题,而是编译器使用std库的方式的问题。您自己的类应该工作得更好,在-O0中没有那么多内联。
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 如何在不使用乘除运算符的情况下计算 A 的 B 次幂?
- 是否允许三元运算符在C++中计算两个操作数?
- 计算校验和的逐位运算符
- c++ 如何计算赋值运算符(if 语句)
- 为什么按位运算符在使用与整数中提供的位数相同的位数计算可能的最大范围时会产生错误
- 我想知道我将如何实现 + 运算符重载.我已经从我上一个问题中计算出 += 运算符重载
- 运算符"|"如何计算两个整数?
- 为什么这个条件运算符的计算结果为 int?
- 为什么 if 语句对于运算符重载函数计算 false
- 我重载了一个运算符*()函数来计算两个矩阵的总和,但编译器提示此错误
- C++运算符和带枚举的计算结果为 false
- 使用条件运算符递归计算模板化值或函数时出现错误 C1202(堆栈溢出)
- C++:计算顺序运算符时忽略括号
- 编译器如何在c中计算sizeof运算符
- 如何重载加法运算符以进行交换计算
- 在c++lldb中使用重载运算符计算表达式
- C++嵌套的条件运算符计算顺序
- 用许多算术运算符c++计算许多值
- 如何使用模运算符计算小数