小编Mat*_*che的帖子

朋友课程的部分模板专业化?

我有一个简单的类X,还有一组模板化类Y <T,U>。我希望所有第一个模板化参数恰好是X的类Y都成为X本身的朋友。以下希望传达出我想要的内容,但是Friendly语句给出了编译错误。

template<typename T, typename U>
class Y {
};

class X {
    public:
        X(int value) : i(value) {}
        const int& getI()    const { return i; }
    private:
        int    i;
        template<class U> friend class Y<X,U>;
};
Run Code Online (Sandbox Code Playgroud)

我不确定朋友语句的模板化是否被允许(更不用说朋友语句的部分模板化了)。有没有办法做到这一点?还是我坚持一一列出所有朋友?

谢谢,马特

c++ templates friend partial

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

为什么__builtin_popcount比我自己的位计数功能慢?

在我编写了自己的位计数例程后,我偶然发现了__builtin_popcount for gcc.但当我切换到__builtin_popcount时,我的软件实际上运行速度较慢.我在Unbutu上使用英特尔酷睿i3-4130T CPU @ 2.90GHz.我建立了一个性能测试,看看是什么给出的.它看起来像这样:

#include <iostream>
#include <sys/time.h>
#include <stdint.h>

using namespace std;

const int bitCount[256] = {
    0,1,1,2,1,2,2,3,  1,2,2,3,2,3,3,4,  1,2,2,3,2,3,3,4,  2,3,3,4,3,4,4,5,
    1,2,2,3,2,3,3,4,  2,3,3,4,3,4,4,5,  2,3,3,4,3,4,4,5,  3,4,4,5,4,5,5,6,
    1,2,2,3,2,3,3,4,  2,3,3,4,3,4,4,5,  2,3,3,4,3,4,4,5,  3,4,4,5,4,5,5,6,
    2,3,3,4,3,4,4,5,  3,4,4,5,4,5,5,6,  3,4,4,5,4,5,5,6,  4,5,5,6,5,6,6,7,
    1,2,2,3,2,3,3,4,  2,3,3,4,3,4,4,5,  2,3,3,4,3,4,4,5,  3,4,4,5,4,5,5,6,
    2,3,3,4,3,4,4,5,  3,4,4,5,4,5,5,6,  3,4,4,5,4,5,5,6,  4,5,5,6,5,6,6,7,
    2,3,3,4,3,4,4,5,  3,4,4,5,4,5,5,6,  3,4,4,5,4,5,5,6,  4,5,5,6,5,6,6,7,
    3,4,4,5,4,5,5,6,  4,5,5,6,5,6,6,7,  4,5,5,6,5,6,6,7,  5,6,6,7,6,7,7,8
};

const uint32_t m32_0001 = 0x000000ffu;
const uint32_t m32_0010 = 0x0000ff00u;
const uint32_t m32_0100 = 0x00ff0000u;
const uint32_t m32_1000 = 0xff000000u;

inline int countBits(uint32_t bitField)
{
    return
        bitCount[(bitField …
Run Code Online (Sandbox Code Playgroud)

c++ gcc

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

标签 统计

c++ ×2

friend ×1

gcc ×1

partial ×1

templates ×1