是否有某种编译时 switch
语句可以用来将参数传递给成员变量的构造函数?现在,我有一个 Controller (在控制系统意义上,而不是 MVC 意义上),我需要能够在编译时配置其工作频率,以及一个其参数取决于所选频率的滤波器。以下是我如何实现它的框架:
#include <cstdint>
class Filter {
public:
Filter(float p1, float p2) : p1(p1), p2{p2} {}
private:
float const p1;
float const p2;
};
class Controller {
public:
Controller(void) {}
private:
static constexpr uint32_t frequency = 200U;
Filter filter{frequency == 400U ? 3.0f : // p1
frequency == 200U ? 1.0f :
frequency == 50U ? 0.55f : 0f,
frequency == 400U ? 2.0f : // p2
frequency == 200U ? 9.0f :
frequency == 50U ? 37.1f : 0f,
};
static_assert(frequency == 400U || frequency == 200U || frequency == 50U, "Invalid frequency");
};
对于大量频率,即使只有两个滤波器参数(真正的软件有更多),这显然很难维持。每次我需要添加对新频率的支持时,我都需要在代码中的 n
点添加代码,其中 n
是滤波器的参数数量。我想要的是这样的:
Filter filter = frequency == 400U ? {3.0f, 2.0f} :
frequency == 200U ? {1.0f, 9.0f} :
frequency == 50U ? {0.55f, 37.1f} :
{0.0f, 0.0f};
或者,在我更狂野的梦想中:
Filter filter = static_switch_map(frequency) {
400U: {3.0f, 2.0f},
200U: {1.0f, 9.0f},
50U: {0.55f, 37.1f},
};
过滤器的参数不是通过公式确定的,因此不能写为表达式的一部分。一些附加说明:
- 我在
clang
和 GNU C++ 中使用 c++14 扩展。 - 我愿意使用更高的 C++ 扩展和特定于 GNU C++ 的编译器扩展,但首选
clang
和 GNU C++ 中的 c++14。仅clang
的解决方案对我来说没有好处。 - 这适用于嵌入式环境;使用
switch
加new
加指针的运行时解决方案是 Not Acceptable ,因为间接性能下降、二进制文件膨胀以及嵌入式环境中内存分配的不安全性. Filter
类可以实例化多次。- 涉及模板的解决方案是可以的;我现在只使用
float
,因为我正在移植某人的 Matlab 代码,但我最终会切换到定点数学。
我考虑过的其他解决方案包括:
- 使用宏和
define
进行条件编译(我在实际代码中使用的Frequency
变量是自定义数据类型,因此我需要使用define
和具有相似作用的 C++ 变量;我不喜欢在两个位置定义频率的想法——这会导致以后出现维护问题。 - 在构建过程中使用自定义预处理器重写变量。太神奇了,将来可能会成为某人的陷阱。
- 枚举。我没有排除这些可能性,但我想不出如何在没有 Java 枚举和/或类似 Python 的
*args
扩展能力的情况下使用它们来改进代码。 。诚然,我编写 C++ 的时间大约只有四个月(非连续),并且在此之前只有一年的 C 经验,所以很有可能我在语法方面遗漏了一些东西。 - 单独的包含文件来包含魔法;在我的项目中,所有自动生成的文件都有一个单独的扩展名,所以这是可行的。不过,我更喜欢更简单的构建脚本并在 C++ 代码中保留尽可能多的逻辑。
最佳答案
将您的开关放入工厂方法中并将构造函数设为私有(private),以便您被迫使用该方法。
这样您将来只需在代码中更新一点:
struct Filter {
static Filter create(int freq) {
switch(freq) {
case 0: return { 0, 1 };
case 2: return { 3, 7 };
default: return { 0, 0 };
}
}
private:
Filter(int, int) {}
};
int main() {
auto filter = Filter::create(2);
(void)filter;
}
如果您也想在编译时使用它,您可以稍微更改它,如下所示(这需要 C++14):
class Filter {
constexpr Filter(int i, int j)
: i{i}, j{j}
{}
public:
static constexpr Filter create(int freq) {
switch(freq) {
case 0: return { 0, 1 };
case 2: return { 3, 7 };
default: return { 0, 0 };
}
}
constexpr int get_i() const { return i; }
constexpr int get_j() const { return j; }
private:
int i;
int j;
};
int main() {
constexpr auto filter = Filter::create(2);
static_assert(filter.get_i() == 3, "!");
}
当然,您可以轻松地将复制构造函数或其他内容添加到您的 Filter
类中。这是一个展示该模式如何工作的最小示例,仅此而已。
另一种单独定义它们并通过调用工厂方法使用每个构造函数的方法是基于委托(delegate)构造函数:
template<int>
struct freq_tag {};
class Filter {
constexpr Filter(int i, int j)
: i{i}, j{j}
{}
constexpr Filter(freq_tag<0>): Filter{0, 1} {}
constexpr Filter(freq_tag<2>): Filter{3, 7} {}
template<int N>
constexpr Filter(freq_tag<N>): Filter{0, 0} {}
public:
template<int N>
constexpr static Filter create() {
return Filter{freq_tag<N>{}};
}
constexpr int get_i() const { return i; }
constexpr int get_j() const { return j; }
private:
int i;
int j;
};
int main() {
constexpr auto filter = Filter::create<2>();
static_assert(filter.get_i() == 3, "!");
}
与基于开关的解决方案相比,这主要是一个品味问题,但事实上,这个解决方案也应该在 C++11 中工作。
关于c++ - C++ 中的编译时构造函数切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46101569/