static-assert

Ways to ASSERT expressions at build time in C

谁说我不能喝 提交于 2019-12-17 03:03:10
问题 I'm tidying up some older code that uses 'magic numbers' all over the place to set hardware registers, and I would like to use constants instead of these numbers to make the code somewhat more expressive (in fact they will map to the names/values used to document the registers). However, I'm concerned that with the volume of changes I might break the magic numbers. Here is a simplified example (the register set is more complex): const short mode0 = 0; const short mode1 = 1; const short mode2

Ways to ASSERT expressions at build time in C

☆樱花仙子☆ 提交于 2019-12-17 03:02:27
问题 I'm tidying up some older code that uses 'magic numbers' all over the place to set hardware registers, and I would like to use constants instead of these numbers to make the code somewhat more expressive (in fact they will map to the names/values used to document the registers). However, I'm concerned that with the volume of changes I might break the magic numbers. Here is a simplified example (the register set is more complex): const short mode0 = 0; const short mode1 = 1; const short mode2

C++ - Static_assert and ability of constexpr functions to evaluate at runtime

拟墨画扇 提交于 2019-12-13 08:11:10
问题 I'm reading about constexpr and static_assert features in C++ and one thing seems confusing to me - I've read that constexpr functions are not necessarily always evaluated during compilation and they can sometimes evaluate at runtime. One thing that bothers me is that static_assert is always checked during compilation. So what happens, if we pass constexpr to static_assert , but compiler chooses to evaluate that constexpr during runtime? Is that even an issue? 回答1: constexpr functions are not

_Static_assert replacement to show value in C

落花浮王杯 提交于 2019-12-11 17:08:36
问题 Is it possible to have the compiler error/warning diagnostic output a compile-time computed numeric value in C11 or C17 (i.e. not using templates)? The below link does this in C++ using template magic. The intention is to use this as a _Static_assert replacement which prints the values of the non-equal failed expression. Ideally, it should be able to evaluate the expression as true or false, and print only when it fails evaluation. This is obviously compiler-dependent, so assume GCC. Display

static_assert with dependent expression that is actually independent

*爱你&永不变心* 提交于 2019-12-11 07:42:44
问题 Consider the following template: template <typename T> void foo() { static_assert(sizeof(T) == 0, "Now what?"); } The standard (§7.4) says: [If the condition to static_assert is false] the program is ill-formed, and the resulting diagnostic message (1.4) shall include the text of the string-literal, […] (Reference taken from https://stackoverflow.com/a/11048906/560450) In practice, the static_assert will not fail until we instantiate the function template foo , because the use othe the

Boost Fusion: validate adapted struct member ordering at compile time

喜夏-厌秋 提交于 2019-12-11 02:16:48
问题 I'm using BOOST_FUSION_ADAPT_STRUCT() , and I need to check that all the members are declared and in the correct order. So first I did this: template <typename Sequence> struct checker { static void check() { typedef typename mpl::accumulate<Sequence, mpl::size_t<0>, mpl::plus<mpl::_1, mpl::sizeof_<mpl::_2>>>::type total_size; static_assert(sizeof(Sequence) == total_size::value, "omitted field?"); } }; And this works: struct foo { int x; float y; double z; }; BOOST_FUSION_ADAPT_STRUCT(foo, x,

Forbids functions with `static_assert`

做~自己de王妃 提交于 2019-12-11 01:06:28
问题 I want to prevent certain functions from being called. Let's ignore the case of calling the function via a function pointer or something, and just concentrate on the case of direct function call. I can do this with = delete . However, the diagnostic issued is not quite informative. I considered using static_assert , with which you can supply a custom diagnostic message. I placed a static_assert(false, ...) statement within the function body, hoping that it fires when the function is called.

Compile-time check to make sure that there is no padding anywhere in a struct

喜欢而已 提交于 2019-12-10 18:56:34
问题 Is there a way to write a compile-time assertion that checks if some type has any padding in it? For example: struct This_Should_Succeed { int a; int b; int c; }; struct This_Should_Fail { int a; char b; // because there are 3 bytes of padding here int c; }; 回答1: Since C++17 you might be able to use std::has_unique_object_representations. #include <type_traits> static_assert(std::has_unique_object_representations_v<This_Should_Succeed>); // succeeds static_assert(std::has_unique_object

Should static_assert be triggered with a typedef?

孤者浪人 提交于 2019-12-10 14:33:10
问题 I noticed that static assertions in class templates are not triggered when instantiations are typedef 'ed. #include <type_traits> template <typename T> struct test_assert { static_assert( std::is_same< T, int >::value, "should fail" ); }; typedef test_assert< float > t; This code compiles without error. If I try to create an instance, then the assertion fails: t obj; // error: static assertion failed: "should fail" Finally, if I replace the condition with false , the assertion fails even if I

Custom compile error message when undefined subtype is accessed

时间秒杀一切 提交于 2019-12-10 13:57:41
问题 I have some types which have sub-types with the same name each: struct TypeA { typedef int subtype; }; struct TypeB { typedef float subtype; }; and also types which don't have this sub-type but which are used in the same context: struct TypeC { // (no subtype defined) }; How can I add a dummy sub-type which gives a custom compile error message? My (so far unsuccessful) attempt is: struct TypeC { struct subtype { static_assert(false, "Attempt to access the non-existent subtype of TypeC."); };