我一直在使用一些demangling代码,以帮助进行一些调试,而无需使用动态强制转换编写数千行,或者必须实现返回类名的虚函数.
template <class CLASS>
std::string getClassName(CLASS &theObject)
{
int status = 0;
// Convert real name to readable string
char *realName = abi::__cxa_demangle(typeid(theObject).name(), nullptr,
nullptr, &status);
ASSERT(status == 0); // Assert for success
VERIFY(realName, return std::string());
// Return as string + prevent memory leaks!
const std::string result(realName);
free(realName);
return result;
}
Run Code Online (Sandbox Code Playgroud)
这段代码背后的想法很简单,输出我们实际使用的类.虽然在切换到Ubuntu 14.04之后我无法再使用clang和c ++ - 11/c ++ - 14标准进行编译,所以我转而使用libc ++而不是libstdc ++.
在切换到libc ++之后,我注意到当我对'std :: string'进行demangle时它不再输出'std :: string',而是输出:
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >
Run Code Online (Sandbox Code Playgroud)
当然这是正确的,因为std :: string是std :: basic_string的typedef.虽然我在libc ++中都可以看到libstdc ++,但是使用typedef以相同的方式定义.所以我真的不明白为什么这个demangling通过切换到libc ++而改变了.
有人知道为什么这是不同的如何获得'std …
我正在阅读Bjarne的《使用C ++的编程和原理》。
我遇到了有关switch的以下详细信息:
您可以为一个案例使用多个案例标签。
两个案例标签不能使用相同的值。
我想我很清楚2.一个。它应该意味着:
switch (a) {
case 'c':{//some code}
case 'c':{//some (different) code}
}
Run Code Online (Sandbox Code Playgroud)
是不合法的。
但是,我不确定第一个是否意味着,如果案例1和案例2等不同,那么我可以有任意数量的案例(当然,它们是常量表达式)还是意味着我可以有任意多个案例,但是其中一些案例是相同的。
我发现了与此类似的问题: C开关情况下的多个标签值
我的解释正确吗?如果不是,我想念或弄错了什么?
我对c ++完全不熟悉,但我试图从我正在阅读的关于这个主题的书中运行代码.对于初学者来说,这是一本非常基础的书,但它包含了我试图写入c ++的som代码,以了解它是如何工作的.我尝试了以下代码,但我收到两个错误:
在函数'int main()'中:
[错误]预期';' 'emp'之前
#include <iostream>
#include <ctime>
class Employee
{
private:
int m_id;
public:
Employee(){}
Employee(int id)
{
m_id=id;
}
int id;
std::string firstname;
std::string lastname;
int birthyear;
void ClockTime()
{
//clocktime code goes here
}
~Employee()
{
m_id=0;
}
};
int main()
{
Employee emp(2);
std::cout<<"The employee time clocked is"
emp.ClockTime()<<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为了使这段代码有效,我还有很多需要改变的地方吗?
基于额外的括号,下面两个 return 语句之间的表达式求值有什么区别吗?
return a++ *(-b+123.456)/999.12344;
Run Code Online (Sandbox Code Playgroud)
与
return (a++ *(-b+123.456)/999.12344);
Run Code Online (Sandbox Code Playgroud)
编程语言 C++ 标准 CPP 98'ish(C++11 之前)
希望问题清楚。期望是对表达式进行完整评估。
为了简化我的问题,我将使用它std::unique_lock作为解释工具。std :: unique_lock有一个模板参数,互斥体。但是,它的构造函数也是模板函数unique_lock(TMutex &, const chrono::duration<_Rep, _Period>&)。
当一个人使用它时,可以写:
auto lock = std::unique_lock(my_mutex, 5s);
Run Code Online (Sandbox Code Playgroud)
那么,问题是:如何为此写出演绎指南(不改变行为),怎么做?
到目前为止,我最大的尝试是:
template<typename _Mutex>
template<typename _Rep, typename _Period>
unique_lock(_Mutex &, const chrono::duration<_Rep, _Period>&) -> unique_lock<_Mutex>;
Run Code Online (Sandbox Code Playgroud)
不幸的是,clang不接受这一点:
错误:模板专业化或离线模板定义中的无关模板参数列表
这是我的代码和我的操作,我想一步一步调试我的代码(下面的代码只是一个例子) main.cpp
#include <iostream>
#include <string>
extern int addd(int ,int);
int main()
{
std::string str = "Hello";
std::cout << str << std::endl;
int a = 10,b = 20;
std::cout << a + b << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
操作文件
int add(int a,int b)
{
return a + b;
}
Run Code Online (Sandbox Code Playgroud)
我使用模板 makefile 表单makefiletemplate只是修改一些不常见的东西,当我发出 g++ 命令时:
[root@centos-linux-10 52coder]# make
g++ -std=c++11 -g -O3 -Wall -Wextra -c opr.cpp -o opr.o
g++ -std=c++11 -g -O3 -Wall -Wextra -c main.cpp -o main.o
g++ …Run Code Online (Sandbox Code Playgroud) 此代码在Visual Studio 2017中可 100%工作,但我不知道为什么在VS 6.0中不工作
#include<iostream>
#include<assert.h>
using namespace std;
class stack
{
struct Node
{
int info;
Node *link;
};
Node *Top;
int count;
int *list1, *list2;
int i, x;
public:
stack()
{
Top = NULL;
count = 0;
list1 = new int[count];
list2 = new int[count];
}
bool isEmpty() { return Top == NULL; }
int TopStack()
{
assert(Top != NULL);
return Top->info;
}
void Push(int num)
{
Node …Run Code Online (Sandbox Code Playgroud)