static_assert c ++ 11的可用性 [英] Availability of static_assert c++11

查看:179
本文介绍了static_assert c ++ 11的可用性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我工作的代码库中开始使用 static_assert 。不幸的是,并不是所有的C ++编译器都支持它们。在过去,我们使用了一个编译时断言宏,它适用于所有我试过的编译器(从SO!收集),但是,它提供了一些尴尬的编译错误信息。



我们支持大量的编译器,包括不支持 static_assert 的编译器。此外,因为我们的产品是一个带有源代码的SDK,我们的客户可以使用任何他们想要的编译器重新编译它。因此,虽然我可以在我们使用的所有编译器中引入条件编译,但是对于任何未知编译器来说,它是不可能的。



一些编译时预定义宏或其他所有C ++编译器标准的工具,用于确定 static_assert 的可用性,或者,您只需要知道每个编译器支持?

解决方案

在C ++ 14中有功能测试宏,它允许您推广使用C ++ 11/14 / 17特点。对于 static_assert ,宏是 __ cpp_static_assert



如果你的编译器本身不支持这些(现在),你可以根据你的编译器支持的知识来定义它们,但它们将向前兼容任何标准化的未知编译器。



注意:这个答案是从一个问题获得的,我问这个问题, ( C ++ 11功能的可用性)。我认为这种特定情况的动机有一些混乱,给出的答案试图解决提供一个漂亮的静态断言,比实际问题,因为它被问(这是,他们没有实际做)。


I would like to start using static_assert in the codebase that I work on. Unfortunately, not all C++ compilers support them. In the past, we've used a compile-time assert macro that works reasonably for all the compilers I've tried (gleaned from SO!), but, it gives slightly awkward compile error messages.

We support a large number of compilers, including ones which do not have support for static_assert. Also, because our product is an SDK with source code our customers can recompile it with any compiler that they wish. So, while I could introduce conditional compilation for it in all the compilers we use, it's not really possible for me to do it for any 'unknown' compiler.

Is there some compile-time predefined macro or other facility that is standard across all C++ compilers for determining the availability of static_assert, or, are you just required to 'know' what every compiler supports?

解决方案

In C++14, there are feature test macros, which allow you to generalize the use of C++11/14/17 features. For static_assert, the macro is __cpp_static_assert.

If your compiler does not inherently support these (yet), you can define them, based on knowledge of what your compiler does support, but they will be forward compatible with any standardized 'unknown' compiler.

Note: this answer was obtained from a question I asked generalizing this one, to any C++11 feature (Availability of C++11 features). I think there was some confusion about the motivation of this particular case, and the answers given tried to solve providing a nice static assert, more than than the actual question as it was asked (which, they didn't actually do).

这篇关于static_assert c ++ 11的可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆