零运行时成本功能标志
Zero runtime costs feature flags
目标是拥有一个没有运行时成本的功能标志系统。一个简单的 C99 解决方案是:
C99:
#include <stdio.h>
#define flag_1 1
int main()
{
#if flag_1
printf("Activen");
#else
printf("InActiven");
#endif
return 0;
}
并不是说这里的 C++ 17 解决方案看起来很优雅:
#include <iostream>
constexpr bool tag_flag_1 = true;
constexpr bool tag_flag_2 = true;
int main()
{
if constexpr(tag_flag_1)
{
std::cout << "Active" << std::endl;
}
else
{
std::cout << "InActive" << std::endl;
}
return 0;
}
但是不起作用,因为"if constexpr"结构仅在"if"结构有效的情况下有效。例如,此代码无效:
if constexpr(tag_flag_1)
{
class foo
{
};
}
而这个是:
#if tag_flag_1
class foo
{
};
#endif
C99 解决方案的问题:
打字:
if constexpr(flag_not_exists)
将导致编译错误,同时:
#if flag_not_exists
不会。
当然,人们总是可以用 C99 编写这个替代繁琐的解决方案:
#include "stdio.h"
#define flag_1 0
int main()
{
#if flag_1
printf("Activen");
#elif defined(flag_1)
printf("InActiven");
#else
#error undefined_flag
#endif
return 0;
}
问题:
有没有一种优雅的方法来确保使用不存在的(例如拼写错误(的功能标志会导致编译错误?
对于解决方案很重要:
- 不需要开发人员对额外的"其他 #error"进行永久纪律处分。我们都很懒...
- 运行时成本为 0
- 支持布尔运算"#if (feature_1 ||feature_2(&&!feature_3">
- "特征标志系统"的精度:当你开发一个新特征时,你可以修改函数签名,向类添加成员,改变变量的类型,添加新的类......从字面上看,它相当于在同一个文件中有两个分支(主分支和功能分支(。通过打开和关闭标志从一个切换到另一个。任何代码修改都可以进行功能标记!
我对可能的模板和/或面向宏的解决方案非常好奇。
从评论问题编辑:
使用C99的简单解决方案会很好。目前,我们的软件使用 Cpp11 编译器进行编译。但即使是 Cpp17 解决方案也适合以后......任何解决方案都是好的,向后兼容越多越好(因为更多的人可以使用它!
我希望我完全理解要求。如果没有,请告诉我,我会编辑或撤回这个答案。
下面的代码 (C++11( 通过以下方式符合要求:
- "不需要开发商对额外的"其他 #error"进行永久纪律处分。我们都很懒...":实际上只需要一次(定义允许的特征组合的
static_assert()
(。 - "运行时成本为 0":是的,感谢编译器的优化器(如果打开(。
- "支持布尔运算"#if (feature_1 || feature_2( && !feature_3":是的,当然,但不使用预处理指令
- ">特征标志系统"的精度[...见OP的问题和评论]":不完全。这里没有条件编译,因此始终编译整个代码,并且无论功能组合如何,都必须定义运行时代码中使用的所有类型(即使它们是不同的方式(。但是,未使用的代码会被编译器的优化器(如果打开(剥离。
话虽如此,这种解决方案使代码复杂化。下面的内容在软件的某些精确部分很有用,但我不会使用它来处理代码的整个条件激活。我通常将这些东西与普通分支和预处理器指令结合使用。因此,请将下面的代码作为"极端的小例子"。
#include <iostream>
// Having all your flags encapsulated in a namespace or in a class allows you to avoid errors tied to typos:
// - "#if feaature_1" (notice the typo in 'feaature') would just exclude some code silentely
// - but "if (FeatureFlags::feaature_1)" (same typo) produces a compile error, which is better
class FeatureFlags
{
public:
static constexpr bool feature_1 = false; // This would also work with 'const' instead of 'constexpr' actually.
static constexpr bool feature_2 = true;
static constexpr bool feature_3 = true;
};
// We want to define a conditional class Foo. But we can't just use FeatureFlags to do conditional compile, and
// we can't test FeatureFlags with preprocessor #directives either. So we split it as follow:
// - There's one version of it just for FeatureFlags::feature_1
// - There's another for FeatureFlags::feature_3 provided FeatureFlags::feature_1 is not defined
// - And there's a default one that deliberately cause a compile time error as we want
// either FeatureFlags::feature_1 or FeatureFlags::feature_3 to be activated, in this example.
// This pure virtual class is just there to cause compile-time errors should we forget to
// implement a part of the class's behaviour in our Foo variants.
// This is not mandatory: if we don't use such an interface we'll just have compile-time errors later
// in the run-time code instead of having them at class definition level.
// This doesn't cause performances issues as the compiler's optimizer will handle that for us, we'll see later.
class Foo_Interface
{
public:
virtual ~Foo_Interface()
{}
virtual void doSomething() = 0;
};
// Will be stripped out by modern compilers' optimizers if FeatureFlags::feature_1 is false
// Side note: Methods are implemented inline just to have a compact example to copy/paste.
// It would be best to have them in a separate .cpp file of course, as we usually do.
class Foo_Feature1 : public Foo_Interface
{
public:
Foo_Feature1()
: i(5)
{}
virtual ~Foo_Feature1()
{}
virtual void doSomething()
{
std::cout << "Foo_Feature1::doSomething() with " << i << std::endl;
}
private:
int i;
};
// Will be stripped out by modern compilers' optimizers if FeatureFlags::feature_1 is true or FeatureFlags::feature_3 is false
class Foo_NotFeature1But3 : public Foo_Interface
{
public:
Foo_NotFeature1But3()
: d(1e-5)
{}
virtual ~Foo_NotFeature1But3()
{}
virtual void doSomething()
{
std::cout << "Foo_NotFeature1But3::doSomething() with " << d << std::endl;
}
private:
double d;
};
// Will be stripped out by modern compilers' optimizers if FeatureFlags::feature_1 is true or FeatureFlags::feature_3 is true
class Foo_Default : public Foo_Interface
{
public:
Foo_Default()
{
// This produces an error at compile time should the activated features be unconsistant.
// static_assert(cdt,msg) can be used everywhere, not only in blocks. It could have been right under
// the definition of FeatureFlags for example. It really depends on where you would like the error to appear.
static_assert(FeatureFlags::feature_1 || FeatureFlags::feature_3, "We shouldn't be using Foo_Default, please enable at least feature 1 or 3");
}
virtual ~Foo_Default()
{}
virtual void doSomething()
{}
};
// Now we can conditionally define Foo:
// - Foo is Foo_Feature1 if FeatureFlags::feature_1 is true.
// - Otherwise, it is either Foo_NotFeature1But3 or Foo_Default depending on FeatureFlags::feature_3
typedef std::conditional
<
FeatureFlags::feature_1,
Foo_Feature1,
std::conditional<FeatureFlags::feature_3, Foo_NotFeature1But3, Foo_Default>::type
>::type Foo;
void main()
{
// What follows is automatically inlined in release mode, no virtual table. Not even an object.
// If Foo becomes bigger or more complicated, this might change. But in that case this means the
// cost of the vtable becomes neglictible. All of this can perfectly be done with no inheritance at
// all though (see comments at Foo_Interface's definition)
Foo f;
f.doSomething();
if (FeatureFlags::feature_1)
{
// Do something or not depending on feature_1.
}
if (FeatureFlags::feature_2)
{
// Do something or not depending on feature_2.
}
if ((FeatureFlags::feature_1 || FeatureFlags::feature_2) && !FeatureFlags::feature_3)
{
// Why not, after all, but that sounds odd...
}
}
如果缺少对不存在的标志的诊断是预处理器方法的唯一问题,您也可以改用类似函数的宏:
#define feature_flag() 0
int main()
{
#if feature_flag()
printf("A");
#else
printf("B");
#endif
}
如果该标志不存在,这将触发诊断,但行为类似于普通宏。
并不是说这里的C++ 17解决方案看起来很优雅:
#include <iostream> constexpr bool tag_flag_1 = true; constexpr bool tag_flag_2 = true; int main() { if constexpr(tag_flag_1) { std::cout << "Active" << std::endl; } else { std::cout << "InActive" << std::endl; } return 0; }
但是不起作用,因为"if constexpr"结构仅在"if"结构>有效。例如,此代码无效:
if constexpr(tag_flag_1) { class foo { }; }
模板来救援。模板专业化是IMO不错的解决方案:
template<bool selectFeature>
class FooImpl;
template<>
class FooImpl<true>
{
};
using Foo = FooImpl<tag_flag_1>;
或者更好:
template<bool selectFeature = tag_flag_1>
class Foo;
template<>
class Foo<true>
{
};
您可以使用 SFINAE 禁用某些功能(如果功能可用(。
对于复杂的功能标志,这也很好用:
constexpr bool tag_flag_1 = true;
constexpr bool tag_flag_2 = true;
constexpr bool tag_flag_3 = true;
template<bool future_1 = tag_flag_1, bool future_2 = tag_flag_2, bool future_2 = tag_flag_2>
class Foo;
template<>
class Foo<true, true, true>
{
};
// when feature 1 is disabled we do not care
// about state of feature 2
template<bool anyFeature2>
class Foo<false, anyFeature2, true>
{
};
....
template<>
class Foo<false, false, false>
{
};
....
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 文件系统:复制功能的速度秘诀是什么
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 如何在Directwrite中获得给定字体的可用OpenType功能
- 零运行时成本功能标志
- 自定义汇编标志以启用特定功能
- 人们为什么使用#IFDEF进行功能标志测试
- 如何在gcc中保留命令行选项,但为某些功能添加更多优化标志