C ++ 11填充胶 [英] C++11 Polyfills
问题描述
我正在启动一个新项目,该项目的目标是MSVC
,GCC (latest)
,GCC 4.3 ARM
等.我们构建的waf
构建系统具有对编译器的C++11
功能检测.
I am starting a new project that will be targeting MSVC
, GCC (latest)
, GCC 4.3 ARM
and more. The waf
build system we have built has C++11
feature detection of the compiler.
我现在拥有针对C++11
的编译器中所有功能的预处理器宏,例如#ifdef WAF_FEATURE_CXX_STRONGLY_TYPED_ENUMS
.因此,我可以为编译器支持的内容编译不同的代码.由于GCC
几乎支持它,因此MSVC
甚至都没有关闭(即使使用MSVC
11)
I now have preprocessor macros for all features in compiler that I am targeting for C++11
, for example #ifdef WAF_FEATURE_CXX_STRONGLY_TYPED_ENUMS
. I can therefore compile different code for what the compiler supports. As GCC
nearly supports it all be MSVC
isn't even close (even with MSVC
11)
这使我想到了Web开发polyfills-如果该功能不可用,请使用可用的功能集将其实现.
This got me thinking about web development polyfills - if the feature isn't available implement it with the feature set available.
这没有像C++11
那样简单的Web开发polyfill,但是如果编译器不支持的话,有什么我可以简单地用C ++ 03实现的东西吗?
This is no way near as simple as web development polyfills as for C++11
but is there anything that I can simply implement with C++03 if the compiler doesn't support it?
这归结为以下事实:我想在我的公共API中使用强类型的枚举器,但作用域MyClass::MyEnumerator::EnumValue
看起来更像C++03
中的MyClass::EnumValue
.无论如何,我可以很容易地在C++03
中发生同样的事情:
This boils down to the fact that I want to use strongly typed enumerators in my public API but the scoping MyClass::MyEnumerator::EnumValue
will look more like MyClass::EnumValue
in C++03
. Is there anyway I can get the same to occur in C++03
easily:
class MyClass {
public:
#ifdef WAF_FEATURE_CXX_STRONGLY_TYPED_ENUMS
enum class MyEnumerator : unsigned int {
#else
enum MyEnumerator {
#endif
EnumValue = 0
}
void method(MyEnumerator e);
}
MyClass mc = new MyClass();
mc.method(MyClass::MyEnumerator::EnumValue) // C++11
mc.method(MyClass::EnumValue) // C++03 :(
推荐答案
这是您需要做的(我对指针和非指针访问进行了其他琐碎的修复). 基本上,这就是@demi所说的.我必须做一个虚拟的枚举名称.这适用于g ++-4.7.
This is what you'll need to do (I made other trivial fixes about pointers and non-pointer access). Bsically, it's what @demi said. I had to make a dummy enum name. This works in g++-4.7.
class MyClass {
public:
#if __cplusplus > 201000
enum class MyEnumerator : unsigned int {
EnumValue = 0
};
void method(MyEnumerator e) {}
#else
class MyEnumerator {
public:
enum Dummy {
EnumValue = 0
};
};
void method(MyEnumerator::Dummy e) {}
#endif
};
int main() {
MyClass mc;
mc.method(MyClass::MyEnumerator::EnumValue); // C++11 or C++03
}
这篇关于C ++ 11填充胶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!