小编Hin*_*sum的帖子

在C++中,每个"语句"也是一个表达式吗?

我可以说在C++中,每个语句(没有分号)也是一个表达式吗?

另外,所有表达式,添加分号,都可以成为一个声明?谢谢.

c++ expression language-lawyer difference

2
推荐指数
2
解决办法
136
查看次数

我什么时候应该使用subprocess.Popen而不是os.popen?

似乎都执行一个子进程并创建一个进/出的管道,只是它subprocess更新.

我的问题是,有什么功能subprocess.Popen可以做而os.popen不能,所以我们需要新的模块subprocess

为什么Python语言没有选择增强os.popen但是创建了一个新模块?

python subprocess popen

2
推荐指数
1
解决办法
1308
查看次数

vim:如何在所有打开的文件中搜索/替换?

如果我使用 :vimgrep,它会搜索当前目录下的所有文件。但我的要求是在所有打开的文件中搜索/替换。

例如,我使用 vim 同时打开 3 个文件

vim 1.cpp 2.cpp 3.cpp
Run Code Online (Sandbox Code Playgroud)

我希望:

  1. 在所有 3 个文件中搜索所有名为“main”的函数并显示在 quickfix 窗口中。

  2. 将所有 3 个文件中的所有“hello”替换为“world”。

vim search buffer replace list

2
推荐指数
2
解决办法
2809
查看次数

linux C++:libaio.h 不包含 io_context_t 的定义?

我有一个超级简单的 .cpp 文件,如下所示:

\n\n
    $cat test001.cpp\n    #include<libaio.h>\n    int main(){\n        io_context_t ctx={0};\n        struct iocb io,*p=&io;\n        return 0;\n    }\n
Run Code Online (Sandbox Code Playgroud)\n\n

但用gcc 4.1.2编译后,提示错误:

\n\n
    $g++ test001.cpp -laio\n    test001.cpp:1:19: error: libaio.h: No such file or directory\n    test001.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\n    test001.cpp:3: error: \xe2\x80\x98io_context_t\xe2\x80\x99 was not declared in this scope\n    test001.cpp:3: error: expected `;\' before \xe2\x80\x98ctx\xe2\x80\x99\n    test001.cpp:4: error: aggregate \xe2\x80\x98iocb io\xe2\x80\x99 has incomplete type and cannot be defined\n
Run Code Online (Sandbox Code Playgroud)\n\n

好吧,我已经在使用“libaio.h”了。为什么还是失败呢?

\n

c++ linux aio definition include

2
推荐指数
1
解决办法
4316
查看次数

C++ 11:"decltype(1 + 2)"声明xvalue还是prvalue?

decltype(1 + 2)是否声明了xvalue或prvalue?

cppreference说,decltype(表达式)将声明:1.T && if表达式是否是xvalue 2.如果表达式是prvalue 3. T&if表达式是lvalue

但我的问题是:如何生成一个xvalue的表达式?我想返回值和临时对象应该是xvalue,但实际上它们似乎是xvalue,在我的实验中:

struct S{};
S f();
int main()
{
    int i=2;
    decltype(i+1) j=i;
    ++j;
    printf("i=%d\n",i);
    S obj;
    decltype(f()) k=obj;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这个程序编译:我可以判断

decltype(i + 1)将(i + 1)声明为prvalue

因为如果它是一个xvalue,那么decltype得到T &&,它不能绑定到左值变量"i".decltype(f())也给我f()作为prvalue也很奇怪?

所以我的问题是:如何写一个表达式,以便decltype(表达式)给我一个xvalue?谢谢.

c++ decltype xvalue c++11 prvalue

2
推荐指数
1
解决办法
132
查看次数

c ++:为什么模板不能用于推导容器和元素类型?

我有一个非常简单的测试程序,如下所示:

#include<vector>
#include<iostream>
using namespace std;
template<typename C, typename E>
void f(const C<E>& container){
    cout<<container.size()<<endl;
}
int main(){
    vector<int> i;
    f(i);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它无法使用gcc 4.1.2进行编译.错误信息是:

templateContainer.cpp:5: error: ‘C’ is not a template
templateContainer.cpp: In function ‘int main()’:
templateContainer.cpp:10: error: no matching function for call to ‘f(std::vector<int, std::allocator<int> >&)’
Run Code Online (Sandbox Code Playgroud)

c++ containers templates class typename

2
推荐指数
1
解决办法
807
查看次数

C++ 11使用指针导致SIGSEGV修改initializer_list中的元素,为什么?

我使用const_cast来修改initializer_list中的元素,如下所示:

#include <initializer_list>
int main()
{
    auto a1={1,2,3};
    auto a2=a1;//copy or reference?
    for(auto& e:a1)
    {
        int*p=const_cast<int*>(&e);
        ++(*p);
    }
    for(auto& e:a2)
        cout<<e;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,当做++(*p)时,这个g ++ 4.9.2编译的程序会抛出SIGSEGV.这个问题不会发生在VC中.

为什么,我的程序是否有任何不安全的操作?请帮忙,谢谢.

c++ segmentation-fault const-cast initializer-list c++11

1
推荐指数
1
解决办法
167
查看次数

C++ 11:decltype((x))和decltype((x + 1))的类型是不同的?

我以为decltype((x))给出了&reference类型,但是有些实验显示了其他:

#include<stdio.h>
int main(){
    int x = 0;
    decltype((x)) r = x;
    r = 1;
    printf("%d\n",x);

    decltype((x+1)) i = x;
    i = 2;
    printf("%d\n",x);

    decltype((1)) k = x;
    k = 3;
    printf("%d\n",x);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我期待(x)和(x + 1)和(1)woul都给出"int&".然而,运行结果是

1
1
1
Run Code Online (Sandbox Code Playgroud)

我期望运行结果应该是1,2,3,因为每个decltype检索引用,但似乎只有第一个工作,而两个(x + 1)和(1)只给出int,而不是'int&'.为什么,(x)和(x + 1)是不同的id-expression类型?

c++ expression decltype c++11

1
推荐指数
1
解决办法
172
查看次数

C++ 14'auto'能够获得函数返回类型,我们还需要std :: result_of <>吗?

似乎C++ 14 auto关键字可用于出现在函数定义的位置,以指示返回类型.在这种情况下,std::result_of还需要吗?现在不是已经过时了吗?

c++ types return auto c++14

1
推荐指数
1
解决办法
108
查看次数

包含在我的RHEL 5上找不到的文件<sched_yield.h>

我正在使用一个相当旧的开发环境,当我"man sched_yield"时,我可以看到这个手册页,但如果我尝试使用它,

$ cat m.cpp

#include<sched_yield.h>
int main(){
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

g ++会说:

error: sched_yield.h: No such file or directory
Run Code Online (Sandbox Code Playgroud)

那么我应该在这个盒子上安装任何额外的yum包吗?怎么解决?

c++ linux rhel5

1
推荐指数
1
解决办法
51
查看次数