static-assert

How to do a static assert that a pointer cast is trivial?

拜拜、爱过 提交于 2020-02-03 04:32:26
问题 Let's say I have these types: struct A { int a; }; struct B { int b; }; struct C : public A, public B { int c; }; A C* pointer can be cast to A* pointer without adjusting the actual address at all. But when C* is cast to B* , the value must change. I'd like to ensure that two related types I have can be cast to each other without a change in address (i.e. that there is no multiple inheritance, or that the base class is the first base of the derived class). This could be checked at run-time, e

Why do C11 global and local static asserts behave differently?

三世轮回 提交于 2020-01-24 00:47:06
问题 Consider the following code: const int g_foo = 1; // (1): _Static_assert(g_foo > 0, "g_foo > 0"); // error: expression in static assertion is not constant. _Static_assert(g_foo > 2, "g_foo > 2"); // error: expression in static assertion is not constant. void Foo(void) { const int foo = 1; // (2): _Static_assert(foo > 0, "foo > 0"); // No issue. _Static_assert(foo > 2, "foo > 2"); // error: static assertion failed: "foo > 2" // (3): _Static_assert(g_foo > 0, "g_foo > 0"); // No issue. _Static

Will consteval allow using static_assert on function arguments?

社会主义新天地 提交于 2020-01-22 18:26:23
问题 Currently you cannot use static_assert to verify parameters of a constexpr function, even if all calls to it are indeed constexpr . That makes sense because the compiler still has to create a non-constexpr instantiation of this function in case some other module will try to call it. Sadly, this is the case even if the function is static or in an anonymous namespace. C++20 however, will introduce a new keyword consteval which is like constexpr but it doesn't allow calling a function in a non

Will consteval allow using static_assert on function arguments?

旧街凉风 提交于 2020-01-22 18:24:06
问题 Currently you cannot use static_assert to verify parameters of a constexpr function, even if all calls to it are indeed constexpr . That makes sense because the compiler still has to create a non-constexpr instantiation of this function in case some other module will try to call it. Sadly, this is the case even if the function is static or in an anonymous namespace. C++20 however, will introduce a new keyword consteval which is like constexpr but it doesn't allow calling a function in a non

How to restrict template parameter to pointer or random access iterator only?

天涯浪子 提交于 2020-01-11 09:26:10
问题 Is there a way to restrict the parameter type of a template function to only pointers or random-access iterators? Say I am developing a sorting function which works only with random accessible containers. I am looking for a way to throw a compile-time error if the user passes a non-random-access iterator. #include <type_traits> #include <iterator> template <class Iterator> void mySort(Iterator begin, Iterator end){ /*The below condition must be true if the 'Iterator' type is a pointer or if

Display integer at compile time in static_assert()

假如想象 提交于 2020-01-10 18:21:27
问题 Here is a simplified version of what I'm trying to do enum First { a, b, c, nbElementFirstEnum, }; enum Second { a, b, c, nbElementSecondEnum, }; static_assert( First::nbElementFirstEnum == Second::nbElementSecondEnum, "Not the same number of element in the enums."); /*static_assert( First::nbElementFirstEnum == Second::nbElementSecondEnum, "Not the same number of element in the enums." + First::nbElementFirstEnum + " " + Second::nbElementSecondEnum);*/ But I would like to be able to print

Is it possible to ASSERT_DOES_NOT_COMPILE with GTest?

孤街浪徒 提交于 2020-01-05 17:37:47
问题 Assume a template class where we assert at compile time that the integer template argument must be greater zero: template<int N> class A { public: A() { static_assert(N > 0, "N needs to be greater 0."); } }; Is it possible to create a googletest unit test that compiles , but reports the error at runtime ? For example: TEST(TestA, ConstructionNotAllowedWithZero) { ASSERT_DOES_NOT_COMPILE( { A< 0 > a; } ); } 回答1: There is a way, but sadly it's probably not the way you want. My first thought was

Can static_assert check if a type is a vector?

给你一囗甜甜゛ 提交于 2020-01-02 00:12:17
问题 Can static_assert check if a type is a vector? IE, an int would raise the assertion, whereas a vector<int> would not. I'm thinking of something along the lines of: static_assert(decltype(T) == std::vector, "Some error") 回答1: Yes. Consider the following meta function: #include <stdio.h> #include <vector> template <class N> struct is_vector { static const int value = 0; }; template <class N, class A> struct is_vector<std::vector<N, A> > { static const int value = 1; }; int main() { printf("is

C++11 static_assert (and functions to be used therein)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-31 04:06:31
问题 static_assert seems to be a very nice feature together with templates. However, I have trouble finding functions in the standard library for doing various tests at compile time. For example, I am looking for a function to check whether a type is a subtype of another one. boost::is_base_of does the job, however, is a comparable function in std, so I do not need to rely on boost. Basically, is there a good source for a list of functions which can be used in static_assert and are contained in

Are compilers allowed to evaluate tautologies in static assert [duplicate]

ぐ巨炮叔叔 提交于 2019-12-30 06:17:11
问题 This question already has answers here : static_assert dependent on non-type template parameter (different behavior on gcc and clang) (2 answers) Closed 2 years ago . Providing a static_assert in templates are often helpful. In the case where a template shouldn't be instantiated in a certain way at all, I often do this template<typename T, typename = void> struct S { static_assert(false, "Unconditional error"); static_assert(sizeof(T) != sizeof(T), "Error on instantiation"); }; template