小编Jon*_*let的帖子

C ++模板函数需要float或double

我想编写一个模板函数,该模板函数可同时用于doublefloat,如下所示:

template <typename T>
constexpr auto someCalc(const T x) {
    return x * 4.0 + 3.0;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,并且4.0 3.0都是双精度字面量,因此在clang上出现以下错误:

error: implicit conversion increases floating-point precision: 'const float' to 'double' [-Werror,-Wdouble-promotion]
Run Code Online (Sandbox Code Playgroud)

是否有一种优雅的方式编写此代码而无需将上转换倍增?我能想到的最好的是

template <typename T>
constexpr auto someCalc(const T x) {
    constexpr T four = 4.0;
    constexpr T three = 3.0;
    return x * four + three;
}
Run Code Online (Sandbox Code Playgroud)

对于较大/更复杂的功能,我发现它的可读性较差,难以维护。

c++ c++11

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

如何在SystemVerilog中创建随机动态二维数组?

我知道如何在 SystemVerilog 中创建随机动态数组:

class packet;
    rand int unsigned len;
    rand byte data[];

    constraint size_con {
        len < 2000;
        data.size = len;
    }
endclass: packet
Run Code Online (Sandbox Code Playgroud)

但我不知道如何使用随机二维动态数组?

class video_frame;
    rand int unsigned width;
    rand int unsigned height;
    rand int unsigned data[][];

    constraint size_con {
        width >= 8;
        width <= 4096;
        height >= 8;
        height >= 2048;
        // How to constraint data.size to be [height, width]
    }
endclass: video_frame;
Run Code Online (Sandbox Code Playgroud)

system-verilog

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

标签 统计

c++ ×1

c++11 ×1

system-verilog ×1