我想编写一个模板函数,该模板函数可同时用于double和float,如下所示:
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)
对于较大/更复杂的功能,我发现它的可读性较差,难以维护。
我知道如何在 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)