小编眠りネ*_*ネロク的帖子

gcc可能不太可能使用宏

我正在用大致以下逻辑编写一段关键代码

if(expression is true){
   //do something with extremely low latency before the nuke blows up. This branch is entered rarely, but it is the most important case
}else{
   //do unimportant thing that doesnt really matter
}
Run Code Online (Sandbox Code Playgroud)

我想在表达式周围使用possible()宏,所以当它到达重要分支时,我得到最小延迟.我的问题是,使用方法与宏名称的建议完全相反,因为我选择"不太可能"的分支进行预取.在性能方面这样做有明显的缺点吗?

memory performance gcc likely-unlikely

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

STL堆栈和priority_queue的插入器

std::vector,std::list并且std::dequestd::back_inserterstd::setstd::inserter.

对于std::stackstd::priority_queue我会假设等效的插入器将是一个,push()但我似乎无法找到正确的函数来调用.

我的意图是能够使用以下函数和正确的插入迭代器:

#include <string>
#include <queue>
#include <iterator>

template<typename outiter>
void foo(outiter oitr)
{
   static const std::string s1 ("abcdefghji");
   static const std::string s2 ("1234567890");
   *oitr++ = s1;
   *oitr++ = s2;
}

int main()
{
   std::priority_queue<std::string> spq;
   std::stack<std::string> stk;

   foo(std::inserter(spq));
   foo(std::inserter(stk));

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

c++ containers stl priority-queue inserter

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

使用auto &&完美转发返回值

请考虑C++模板中的引用:完整指南(第2版):

decltype(auto) ret{std::invoke(std::forward<Callable>(op),
                               std::forward<Args>(args)...)};
...
return ret;
Run Code Online (Sandbox Code Playgroud)

需要注意的是,宣布retauto&&不正确.作为引用,auto&&将返回值的生存期延长到其作用域的末尾,但不超出return对函数调用者的语句.

作者说这auto&&不适合完美转发回报值.但是,是否也没有decltype(auto)形成对xvalue/lvalue的引用?国际海事组织,decltype(auto)然后遭受同样的问题.那么,作者有什么意义呢?

编辑:

上面的代码片段应该放在这个函数模板中.

template<typename Callable, typename... Args>
decltype(auto) call(Callable&& op, Args&&... args) {
    // here
}
Run Code Online (Sandbox Code Playgroud)

c++ templates language-lawyer perfect-forwarding

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

最烦恼的解析防止在类中初始化std :: vector <int>

C++ 11允许类内初始化:

struct Foo{
    std::vector<std::string> v{3}; // vector of 3 empty strings
};
Run Code Online (Sandbox Code Playgroud)

如果我们想要在类中初始化一个int的向量,我们会得到其他东西:

struct Foo{
    std::vector<int> v{3}; // vector of one element with value 3
};
Run Code Online (Sandbox Code Playgroud)

这个问题似乎是语言的限制,正如之前的问题所讨论的那样.但是,如果这不是类内初始化,我们将能够使用括号而不是大括号,并获得所需的结果:

std::vector<int> v(3); // vector of three zeros
Run Code Online (Sandbox Code Playgroud)

但是,由于最令人烦恼的解析,我们不能在课堂上这样做:

struct Foo{
    std::vector<int> v(3); // most vexing parse; doesn't compile
};
Run Code Online (Sandbox Code Playgroud)

当然,上面的代码是否是良好的设计实践是有争议的,因为我们可以轻松地将我们想要做的事情移动到构造函数中.但暂时把它放在一边,有没有办法执行所需的初始化,尽可能接近第一个std::string例子,这没有问题?

c++ constructor initialization most-vexing-parse c++11

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

在Python中检查set中的项目成员资格

您好我已经编写了几个月了,并且知道了基础知识,但是我遇到了一个集合成员问题,我无法找到解决方案.

我有一个整数对列表的列表,我想删除其中包含"a"整数的列表.我认为使用套装是最简单的方法.贝娄是代码:

## This is the item to test against. 
a = set([3]) 
## This is the list to test.      
groups = [[3, 2], [3, 4], [1, 2], [5, 4], [4, 3]]     

## This is a list that will contain the lists present
## in groups which do not contain "a"
groups_no_a = []        

for group in groups:
    group = set(group)
    if a in group:
        groups_no_a.append(group)
    ## I thought the problem had something to do with
    ## clearing the variable …
Run Code Online (Sandbox Code Playgroud)

python set

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

纯虚拟类中的构造函数应该是"受保护的"还是"公共的"?

以下示例来自"Inside C++对象模型"一书

class Abstract_base {
public:
    virtual ~Abstract_base () = 0;
    virtual void interface () const = 0;
    virtual const char* mumble () const 
    {
        return _mumble;
    }
protected:
    char *_mumble;
};
Run Code Online (Sandbox Code Playgroud)

作者说如果我想初始化_mumble,那么应该实现纯虚基类的数据成员,一个"受保护的构造函数".

但为什么要保护?为什么"公共建设者"不适合这个班级?

谢谢你的回答,如果有一个例子,这将是完美的~~ :)

c++ oop inheritance constructor pure-virtual

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

从微体系结构抽象的x86程序计数器?

我正在阅读《 RISC-V读者:开放式体系结构图集》一书。为了解释ISA(指令集体系结构)与特定实现(即微体系结构)的隔离,作者写道:

架构师的诱惑是将说明包含在ISA中,以帮助在特定时间实现一种实现的性能或成本,但会给不同的或将来的实现带来负担。

据我了解,它指出,在设计ISA时,ISA应该理想地避免公开实现它的特定微体系结构的细节。


请牢记上面的引号:当涉及程序计数器时,在RISC-V ISA上,程序计数器(pc)指向当前正在执行的指令。另一方面,在x86 ISA上,程序计数器(eip)不包含当前正在执行的指令的地址,而是包含当前指令之后的指令的地址。

x86程序计数器是否从微体系结构中抽象出来了?

x86 instruction-set cpu-architecture program-counter riscv

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

为什么std :: accumulate生成705032704作为输出而不是向量中元素的总和?

输出是705032704而不是5000000000。为什么?我以为std::accumulate可以计算向量中元素的总和。

#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <string>
#include <iterator>
#include <queue>
#include <stack>
#include <numeric>

typedef long long ll;

int main()
{

    std::vector<ll> nums = {1000000000, 1000000000,1000000000,1000000000,1000000000};
    std::cout << std::accumulate(nums.begin(), nums.end(), 0);
    std::cin.ignore();

}
Run Code Online (Sandbox Code Playgroud)

c++ algorithm stl overflow accumulate

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

如何在GCC中将堆栈对齐到32字节边界?

我正在使用基于GCC 4.6.1的MinGW64构建Windows 64bit目标.我正在玩新的英特尔AVX指令.我的命令行参数是-march=corei7-avx -mtune=corei7-avx -mavx.

但是当我在堆栈上分配局部变量时,我开始遇到分段错误错误.GCC使用对准移动VMOVAPSVMOVAPD移动__m256__m256d周围,并且这些指令需要32字节对齐.但是,Windows 64bit的堆栈只有16字节对齐.

如何将GCC的堆栈对齐更改为32个字节?

我尝试使用-mstackrealign但无济于事,因为它只对齐16个字节.我也无法__attribute__((force_align_arg_pointer))工作,无论如何它都会对齐16个字节.我无法找到任何其他可以解决此问题的编译器选项.任何帮助是极大的赞赏.

编辑: 我尝试使用-mpreferred-stack-boundary=5,但GCC说这个目标不支持5.我没有想法.

stack gcc sse avx

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

如何将两个32位寄存器移入一个64位?

假设我想将两个32位寄存器EAX作为低32位字和EDX高32位字放入RAX.我找到了一种方法:

shl   rdx, 32
or    rax, rdx
Run Code Online (Sandbox Code Playgroud)

只有当我们确定从32到61的位RAX为0时,此方法才有效.如果我们不确定,那么我们必须首先清除高32位字,如:

mov   eax, eax      //This instruction should clear the high 32 bit word of RAX
Run Code Online (Sandbox Code Playgroud)

这是最短路吗?

是否有一个asm x86-64指令执行此操作?

x86 assembly x86-64 move

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