static-assert

How do I check if a template parameter is a power of two?

杀马特。学长 韩版系。学妹 提交于 2019-12-20 11:53:19
问题 I want to create a structure that allocates statically an array of 2^N bytes , but I don't want the users of this structure to specify this size as the exponent. Example: my_stupid_array<char, 32> a1; // I want this! my_stupid_array<char, 5> a2; // And not this... How do I check if this template parameter is a power of two and warn the user with a nice message about this? I've been able to check for this with a simple template: template<int N> struct is_power_of_two { enum {val = (N >= 1) & !

When to use `static_assert` instead of SFINAE?

社会主义新天地 提交于 2019-12-20 08:49:25
问题 I have been using (and seen used) static_assert to flag undesired values of template parameter values. However, for all cases I came across it seems better and more elegant to disable those undesired values via SFINAE. For example template<typename T, class = std::enable_if<std::is_floating_point<T>::value>::type> struct Foo { ... }; instead of template<typename T> struct Foo { static_assert(std::is_floating_point<T>::value, "Foo<T>: T must be floating point :-("); ... }; So my question: when

Comparing constexpr function parameter in constexpr-if condition causes error

余生长醉 提交于 2019-12-20 05:13:50
问题 I'm trying to compare a function parameter inside a constexpr-if statement. Here is a simple example: constexpr bool test_int(const int i) { if constexpr(i == 5) { return true; } else { return false; } } However, when I compile this with GCC 7 with the following flags: g++-7 -std=c++1z test.cpp -o test I get the following error message: test.cpp: In function 'constexpr bool test_int(int)': test.cpp:3:21: error: 'i' is not a constant expression if constexpr(i == 5) { return true; } However, if

Is there a way to prevent a class from being derived from twice using a static assert and type trait?

流过昼夜 提交于 2019-12-20 01:06:58
问题 I realize this is a contrived example, but I want a compile check to prevent this... class A {}; class B : public A {}; class C : public A {}; class D : public B, public C { BOOST_STATIC_ASSERT((is_base_of_once<A,D>::value)) }; 回答1: The following should work: BOOST_STATIC_ASSERT(((A*)(D*)0 == 0)) If A exists twice, this should rise an ambiguity error, while otherwise the test will always succeed (because it compares two null pointers). 回答2: When I try to derive a class twice as you have here

Force deriving from a class virtually

六月ゝ 毕业季﹏ 提交于 2019-12-19 05:56:41
问题 We have a special framework for interfaces in our project, and part of the requirements is that classes which represent an interface may only be used as virtual base classes, not as non-virtual ones. Is there a way to enforce this in code? That is, produce a compilation error if the class is derived from non-virtually. I have access to C++11 as implemented by VS 2010: this means static_assert , enable_if and <type_traits> are available. 回答1: IMO, there is no clean and platform independent

static_assert - a way to dynamically customize error message

吃可爱长大的小学妹 提交于 2019-12-19 05:18:49
问题 Is there a way to make static_assert's string being dynamically customized and then displayed? What I mean is something like: //pseudo code static_assert(Check_Range<T>::value, "Value of " + typeof(T) + " type is not so good ;)"); 回答1: No, there is not. However this does not matter so much, because static_assert are evaluated at compile-time, and in case of error the compiler will not only print out the message itself, but it will also print the instanciation stack (in case of templates).

How to combine static_assert with sizeof and stringify?

不问归期 提交于 2019-12-18 14:47:48
问题 Memory usage is quite critical in my application. Therefore I have specific asserts that check for the memory size at compile time and give a static_assert if the size is different from what we considered correct before. I have defined a macro like this: #define CHECKMEM(mytype, size) static_assert((sizeof(objectType) == size)), "Size incorrect for " #mytype "!"); This macro makes it very easy to write this: CHECKMEM(Book,144); CHECKMEM(Library,80); The problem is that when this static_assert

Does GCC have a built-in compile time assert?

拟墨画扇 提交于 2019-12-18 14:16:20
问题 Our existing compile-time assert implementation is based on negative array index, and it provides poor diagnostic output on GCC. C++0x's static_assert is a very nice feature, and the diagnostic output it provides is much better. I know GCC has already implemented some C++0x features. Does anyone know if static_assert is among them and if it is then since what GCC version? 回答1: According to this page, gcc has had static_assert since 4.3. 回答2: If you need to use a gcc version which does not

Why static_assert in template gives me different result with equivalent expressions?

六月ゝ 毕业季﹏ 提交于 2019-12-18 09:04:22
问题 I've noticed strange behavior of static_assert : #include <iostream> template <typename T, unsigned int D> struct Vec { static_assert(D && 0, "Invalid dimension for vector!"); }; template <typename T> struct Vec<T, 1> {union {T x, r;};}; template <typename T> struct Vec<T, 2> : Vec<T, 1> {union {T y, g;};}; template <typename T> struct Vec<T, 3> : Vec<T, 2> {union {T z, b;};}; template <typename T> struct Vec<T, 4> : Vec<T, 3> {union {T w, a;};}; int main() { Vec<float, 3> v; v.x = 1; v.y = 2

Ensure derived class implements static method

青春壹個敷衍的年華 提交于 2019-12-18 04:52:08
问题 I want to ensure, that a derived class implements a specific static method. I think doing so should be possible using static_assert, std::is_same, decltype, CRTP and maybe making use of SFINAE. However, similar code I found so far is quite complex and it seems I do not yet fully understand it making me unable to adopt it to my needs. What I tried so far is this template <class T> class Base { static_assert(std::is_same<decltype(T::foo(1)), int>::value, "ERROR STRING"); }; class Derived :