在C++ 11中,我们可以编写以下代码:
struct Cat {
Cat(){}
};
const Cat cat;
std::move(cat); //this is valid in C++11
Run Code Online (Sandbox Code Playgroud)
当我打电话时std::move,这意味着我想移动对象,即我将改变对象.移动const对象是不合理的,为什么不std::move限制这种行为呢?这将是未来的陷阱,对吧?
陷阱意味着布兰登在评论中提到:
"我认为他的意思是"偷偷摸摸"他鬼鬼祟祟偷偷摸摸,因为如果他没有意识到,他最终得到的副本并不是他想要的."
在Scott Meyers的"Effective Modern C++"一书中,他给出了一个例子:
class Annotation {
public:
explicit Annotation(const std::string text)
: value(std::move(text)) //here we want to call string(string&&),
//but because text is const,
//the return type of std::move(text) is const std::string&&
//so we actually called string(const string&)
//it is a bug which is very hard to find out
private:
std::string value;
};
Run Code Online (Sandbox Code Playgroud)
如果 …
假设我们有一个名为libtest.so的共享库,其中有一个函数"foo"
使用条带丢弃libtest.so中的所有符号
$strip libtest.so
Run Code Online (Sandbox Code Playgroud)
所以,现在如果我们使用:
$nm libtest.so
Run Code Online (Sandbox Code Playgroud)
它会打印出来:
nm:libtest.so:没有符号
但如果我们使用:
$readelf -s libtest.so
Run Code Online (Sandbox Code Playgroud)
foo函数仍然可以从其结果中看出:
...
10:000005dc 5 FUNC GLOBAL DEFAULT 12 _Z3foov
...
我们也可以使用命令字符串来检查它:
$strings libtest.so
Run Code Online (Sandbox Code Playgroud)
...
_Z3foov
...
这是我的问题,为什么nm没有给条纹libtest.so带来任何结果?
谢谢
例如,评估任何自定义数学表达式的最佳方法是什么
3+sqrt(5)+pow(3)+log(5)
Run Code Online (Sandbox Code Playgroud)
我知道将Python嵌入到C++中可以做到这一点; 有没有更好的方法?
谢谢!
在我的shell脚本中,我正在运行一个命令,它要求我输入.
如何自动为命令提供所需的输入?
例如:
$cat test.sh
ssh-copy-id tester@10.1.2.3
Run Code Online (Sandbox Code Playgroud)
运行test.sh时:
首先,它会问:
Are you sure you want to continue connecting (yes/no)?
然后,它会要求我输入密码:
tester@10.1.2.3's password:
有没有办法自动输入?
Objective C引入了一项名为ARC的技术,使开发人员免于内存管理的负担.这听起来不错,我认为如果g ++也有这个功能,C++开发人员会非常高兴.
ARC允许您将内存管理的负担放在(Apple LLVM 3.0)编译器上,从不再考虑保留,释放和自动释放
所以,如果LLVM3.0可以做到这一点,我认为g ++也可以让C++开发人员摆脱内存管理的艰巨任务,对吧?
将ARC引入C++有什么困难吗?
我的意思是:如果我们不使用智能指针,我们只使用new/ new[],编译器是否可以为我们做些什么来防止内存泄漏?例如,自动将新指针更改为智能指针?
.pro如果我想执行chmod命令,执行输出二进制文件或执行其他操作,我必须对文件进行哪些更改.

CAT *p;
...
p->speak();
...
Run Code Online (Sandbox Code Playgroud)
有些书说编译器会将p-> speak()翻译为:
(*p->vptr[i])(p); //i is the idx of speak in the vtbl
Run Code Online (Sandbox Code Playgroud)
我的问题是:因为在编译时,不可能知道p的实际类型,这意味着不可能知道要使用哪个vptr或vtbl.那么,编译器如何生成正确的代码呢?
[改性]
例如:
void foo(CAT* c)
{
c->speak();
//if c point to SmallCat
// should translate to (*c->vptr[i])(p); //use vtbl at 0x1234
//if c point to CAT
// should translate to (*c->vptr[i])(p); //use vtbl at 0x5678
//since ps,pc all are CAT*, why does compiler can generate different code for them
//in compiler time?
}
...
CAT *ps,*pc;
ps = new SmallCat; //suppose SmallCat's …Run Code Online (Sandbox Code Playgroud) 我对/ proc/pid/smaps中的pss列感到困惑,所以我写了一个程序来测试它:
void sa();
int main(int argc,char *argv[])
{
int fd;
sa();
sleep(1000);
}
void sa()
{
char *pi=new char[1024*1024*10];
for(int i=0;i<4;++i) {
for(int j=0;j<1024*1024;++j){
*pi='o';
pi++;
}
}
int cnt;
for(int i=0;i<6;++i) {
for(int j=0;j<1024*1024;++j){
cnt+=*pi;
pi++;
}
}
printf("%d",cnt);
}
Run Code Online (Sandbox Code Playgroud)
$cat /proc/`pidof testprogram`/smaps
08838000-0885b000 rw-p 00000000 00:00 0 [heap]
Size: 140 kB
Rss: 12 kB
Pss: 12 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 12 kB
Referenced: 12 kB
Swap: 0 kB
KernelPageSize: 4 …Run Code Online (Sandbox Code Playgroud) 在阅读"超越C++标准库:Boost简介"时,我得到了一个非常有趣的例子:
class A
{
public:
virtual void sing()=0;
protected:
virtual ~A() {};
};
class B : public A
{
public:
virtual void sing( )
{
std::cout << "Do re mi fa so la"<<std::endl;;
}
};
Run Code Online (Sandbox Code Playgroud)
我做了一些测试:
int main()
{
//1
std::auto_ptr<A> a(new B); //will not compile ,error: ‘virtual A::~A()’ is protected
//2
A *pa = new B;
delete pa; //will not compile ,error: ‘virtual A::~A()’ is protected
delete (dynamic_cast<B*>(pa)); //ok
//3
boost::shared_ptr<A> a(new B);//ok
}
Run Code Online (Sandbox Code Playgroud)
我在这里很好奇的是~hared_ptr是如何工作的?它如何推导派生类B?
谢谢你的帮助!
谢谢大家,我写了一个关于~share_ptr如何工作的简单示例
class sp_counted_base …Run Code Online (Sandbox Code Playgroud) 我发现它binary_function已从C++ 11中删除.我想知道为什么.
C++ 98:
template <class T> struct less : binary_function <T,T,bool> {
bool operator() (const T& x, const T& y) const {return x<y;}
};
Run Code Online (Sandbox Code Playgroud)
C++ 11:
template <class T> struct less {
bool operator() (const T& x, const T& y) const {return x<y;}
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
};
Run Code Online (Sandbox Code Playgroud)
MODIFIED ------------------------------------------------- ---------------------------
template<class arg,class result>
struct unary_function
{
typedef arg argument_type;
typedef result result_type;
};
Run Code Online (Sandbox Code Playgroud)
例如,如果我们想在C++ 98中为函数编写适配器,
template <class T> struct even : …Run Code Online (Sandbox Code Playgroud)