小编The*_*erd的帖子

在B类中声明为朋友的A类成员模板函数无法访问A类的私有成员(仅限Clang)

请查看此代码段.我知道它没有多大意义,它只是为了说明我遇到的问题:

#include <iostream>
using namespace std;

struct tBar
{
    template <typename T>
    void PrintDataAndAddress(const T& thing)
    {
        cout << thing.mData;
        PrintAddress<T>(thing);
    }

private:
    // friend struct tFoo; // fixes the compilation error

    template <typename T>
    void PrintAddress(const T& thing)
    {
        cout << " - " << &thing << endl;
    }
};

struct tFoo
{
    friend void tBar::PrintDataAndAddress<tFoo>(const tFoo&);
    private:

    int mData = 42;
};

struct tWidget
{
    int mData = 666;
};

int main() 
{
    tBar bar;
    bar.PrintDataAndAddress(tWidget()); // Fine …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11 clang++

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

为什么 x86-64 C/C++ 编译器没有为此代码生成更高效的汇编?

考虑以下局部变量的声明:

bool a{false};
bool b{false};
bool c{false};
bool d{false};
bool e{false};
bool f{false};
bool g{false};
bool h{false};
Run Code Online (Sandbox Code Playgroud)

在 x86-64 架构中,我希望优化器将这些变量的初始化减少到类似mov qword ptr [rsp], 0. 但相反,我使用所有编译器(无论优化级别如何)得到的结果是某种形式的:

mov     byte ptr [rsp + 7], 0
mov     byte ptr [rsp + 6], 0
mov     byte ptr [rsp + 5], 0
mov     byte ptr [rsp + 4], 0
mov     byte ptr [rsp + 3], 0
mov     byte ptr [rsp + 2], 0
mov     byte ptr [rsp + 1], 0
mov     byte …
Run Code Online (Sandbox Code Playgroud)

c++ performance initialization x86-64

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

标签 统计

c++ ×2

c++11 ×1

clang++ ×1

initialization ×1

performance ×1

templates ×1

x86-64 ×1