小编no *_*ial的帖子

如何调用模板类型的正确构造函数?

在下面的代码中,如何使注释行的工作方式与其正上方的行相同?

我想让它成为一个通用代码,调用模板的合适构造函数Type

#include <string>
#include <iostream>

template <typename Type>
struct Class
{
    Type data;
    Class(Type data) : data(data) { }
};

int main()
{
    Class<std::string> a = std::string("abc");
    // Class<std::string> b = "abc";
    std::cout << a.data << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates initialization class c++11

21
推荐指数
3
解决办法
633
查看次数

ndk和clang ++中的ARM编译器错误?

请考虑以下代码:

float test(int len, int* tab)
{
    for(int i = 0; i<len; i++)
        tab[i] = i;
}
Run Code Online (Sandbox Code Playgroud)

显然回报缺失。对于用于ARM处理器的clang和ndk编译器的这种情况,都会生成一个无限循环。拆卸之后,很明显编译器生成常规分支指令,而不是条件分支。

    mov     r0, #0
.LBB0_1:
    str     r0, [r1, r0, lsl #2]
    add     r0, r0, #1
    b       .LBB0_1
Run Code Online (Sandbox Code Playgroud)

可以在此处找到带有错误的示例:https : //godbolt.org/z/YDSFw-

请注意,c ++规范指出缺少返回值被视为未定义行为,但仅引用返回值。它不会影响前面的说明。

我在这里想念什么吗?有什么想法吗?

c++ arm compiler-errors android-ndk clang++

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

lambda调用另一个外部lambda

我可以轻松地做到这一点:

auto f = []()->int { return 4; };
auto g = [f]()->int { return f(); });
int i = g();
Run Code Online (Sandbox Code Playgroud)

不过,我不能这样做:

int (*f)() = []()->int { return 4; };
int (*g)() = [f]()->int { return f(); });
int i = g();
Run Code Online (Sandbox Code Playgroud)

为什么我在MSVC中收到这样的消息?

error C2440: 'initializing' : cannot convert from 'ClassName::functionName::< lambda_b2eebcdf2b88a20d8b40b0a03c412089>' to 'int (__cdecl *)(void)'
Run Code Online (Sandbox Code Playgroud)

这发生在线:

int (*g)() = [f]()->int { return f(); });
Run Code Online (Sandbox Code Playgroud)

怎么做得好?

c++ lambda c++11

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

C++:将多态lambda传递给函数

我想传递一个多态lambda函数将执行某些操作(即==<=<在不同的数据类型等).所以我想做这样的操作:

bool func(bool (* f)(auto a, auto b))
{
    int a = 1, b = 2;
    float c = 3, d = 4;
    return f(a, b) || f(c, d);
}
Run Code Online (Sandbox Code Playgroud)

以后执行它:

func([](auto a, auto b) -> bool { return a == b; });
func([](auto a, auto b) -> bool { return a <= b; });
func([](auto a, auto b) -> bool { return a < b; });
Run Code Online (Sandbox Code Playgroud)

但遗憾的auto是,不允许以这种方式声明函数指针(带参数类型).我该怎么做呢?

c++ lambda c++14

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

C ++重复N次迭代

我可以清楚地做这样的事情:

for(int i = 0; i < 10000; i++)
    testIteration();
Run Code Online (Sandbox Code Playgroud)

但是是否有任何一行可以做类似事情的std函数?像这样:

std::repeat(10000, testIteration);
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

Reserve() - data() 空向量技巧 - 正确吗?

正如我们所知, std::vector 在初始化时像std::vector vect(n)orempty_vect.resize(n)不仅分配所需的内存量,而且还用默认值初始化它(即调用默认构造函数)。这会导致不必要的初始化,特别是如果我有一个整数数组并且我想用一些无法通过任何向量构造函数提供的特定值填充它。

另一方面,Capacity 在 call like 中分配内存empty_vect.reserve(n),但在这种情况下 vector 仍然是空的。所以size()返回0empty()返回trueoperator[]产生异常。

现在,请查看代码:

{ // My scope starts here...

    std::vector<int> vect;
    vect.reserve(n);
    int *data = vect.data();

    // Here I know the size 'n' and I also have data pointer so I can use it as a regular array.
    // ...

} // Here ends my scope, so vector is destroyed, memory is released.
Run Code Online (Sandbox Code Playgroud)

问题是“所以我可以将它用作数组”是否是一个安全的假设?

无论争论,我只是对上述问题感到好奇。无论如何,至于论据: …

c++ stl c++11

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

MSVC 中的 C++ 自定义 STL 分配器错误?

我认为在 MSVC++ 中发现了一个错误。或者,这可能是我缺乏知识,我错过了代码中的某些内容。我创建了一个自定义分配器:

#include <forward_list>
#include <iostream>

template <class T>
class Allocator
{
    public:

        typedef std::size_t size_type;
        typedef std::ptrdiff_t difference_type;
        typedef T *pointer;
        typedef const T *const_pointer;
        typedef T &reference;
        typedef const T &const_reference;
        typedef T value_type;

        template <class U>
        struct rebind
        {
            typedef Allocator<U> other;
        };

        Allocator()
        {
            std::cout << (ptrdiff_t) this << " Allocator()" << std::endl;
        }

        Allocator(const Allocator &allocator)
        {
            std::cout << (ptrdiff_t) this << " Allocator(const Allocator &allocator)" << std::endl;
        }

        template <class U>
        Allocator(const Allocator<U> …
Run Code Online (Sandbox Code Playgroud)

c++ stl allocator c++11

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

了解FMA表现

我想了解如何计算FMA性能。如果我们在这里查看说明:

https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_mm256_fmadd_ps&expand=2520,2520&techs=FMA

对于Skylake架构,该指令具有Latency=4Throughput(CPI)=0.5,因此该指令的整体性能为4*0.5 = 2每条指令的时钟数。

据我了解,如果最大(涡轮)时钟频率为3GHz,那么对于一秒钟的单个内核,我可以执行1 500 000 000条指令。

这样对吗?如果是这样,我观察到性能稍高的原因可能是什么?

c++ x86 fma

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

使用正则表达式和“find”命令查找 *.cpp、*.hpp、*.c 和 *h 文件

我有以下内容:

find . -type f | grep -E '\.(cpp|hpp|c|h)$'
Run Code Online (Sandbox Code Playgroud)

如何仅使用 find 命令来做到这一点?

bash

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