static-assert

Automatically selecting between static assert and runtime error

女生的网名这么多〃 提交于 2019-12-10 13:48:19
问题 I have macro that performs a division and checks alignment. #define BYTES_TO_WORDS(x) ((CHECK_ALIGNMENT(x,2)) * ((x) / 2)) I would like to implement CHECK_ALIGNMENT as a macro that always returns 1, and triggers an error if x does not divide by 2. The macro BYTES_TO_WORDS is called from different contexts, sometimes with x as a compile-time constant integer expression and other times with x as an integer expression that is resolved on runtime. Is it possible to implement CHECK_ALIGNMENT such

Clang compile error related to static_assert and boost::hana

孤者浪人 提交于 2019-12-10 13:45:28
问题 Consider the following problem which compiles successfully on Clang 3.8 using -std=c++14 . #include <boost/hana.hpp> namespace hana = boost::hana; int main() { constexpr auto indices = hana::range<unsigned, 0, 3>(); hana::for_each(indices, [&](auto i) { hana::for_each(indices, [&](auto j) { constexpr bool test = (i == (j == i ? j : i)); static_assert(test, "error"); }); }); } The test is quite non-sensical but that is not the point. Consider now an alternative version where the test is

C++11 static_assert: Parameterized error messages

不问归期 提交于 2019-12-10 12:48:51
问题 In my previous question I wanted to use static_assert to restrict a template parameter to be a specific subtype. The question was answered, the code for archieving that is as follows: template <typename T> struct X { static_assert(std::is_base_of<Y,T>::value,"T must be derived from Y!"); }; Now, I want to make the error message more concise. I.e., I want to state which type is violating this constraint. E.g., if class A is not derived from Y and someone instanciates X<A> , then the error

How to make static_assert block re-usable in template classes?

一世执手 提交于 2019-12-10 09:55:50
问题 Say I have a template class that makes multiple static_asserts: template <class T> class Foo { static_assert(!std::is_const<T>::value,""); static_assert(!std::is_reference<T>::value,""); static_assert(!std::is_pointer<T>::value,""); //...<snip>... } Now say I have more template classes that need to make the same asserts. Is there a way to make a static_assert block reusable? A "static_assert function" if you will. 回答1: One thing you can do is build a new trait that is a conjunction of the

Work around incomplete type in static assert

只谈情不闲聊 提交于 2019-12-10 09:32:18
问题 Is there a way to static_assert inside a class when the expression depends on the class type itself? Maybe delay the evaluation until the type is complete or after template instantiation? Example code: #include <type_traits> template<typename T> struct Test { T x = 0; // make non-trivial static_assert(std::is_trivial<Test<T>>::value, ""); }; int main() { // would like static assert failure, instead get 'incomplete type' error Test<int> test1; Test<float> test2; return 0; } 回答1: Here's a

Static assertion if possible, dynamic assertion otherwise?

霸气de小男生 提交于 2019-12-10 01:45:49
问题 Let's say I have a template function that takes an integer and a const reference to an instance of type T. Now depending on the integer, only some T's are acceptible, otherwise an exception is thrown at runtime. If all uses of this function would use constant integers, it would be possible to make the int a template parameter and use a static assertion to check if it is acceptable. So instead of func(1,c) one would use func<1>(c) and would gain compile-time type checking. Is there any way to

How to test whether expression is a temporary?

戏子无情 提交于 2019-12-09 17:54:03
问题 With the following macro: #define ASSERT_IF_TEMP(expr) static_assert(?, "Is temporary!"); What should I put for question mark? 回答1: First we should clarify: What do you mean by "temporary"? Many people mean different things when they say temporary. Technically, int() is not a temporary, but most people will include them into their own meaning of that term. Technically, given std::string s; , then move(s) isn't a temporary either, but you may want to treat it as one with your macro. The first

Specializations only for C++ template function with enum non-type template parameter

旧时模样 提交于 2019-12-08 14:33:26
This question is related to this one except that rather than dealing with typename template parameters, I am trying to use an enum non-type template parameter. Is it possible to have a templated (class member function) with only specializations, no general (working) definition in the case of non-type template parameter? I was able to get one version working, by declaration in the class body and providing specializations only, but any misuse calling with a non-defined template parameter doesn't produce an error until linking. What's worse is the missing symbol cryptically refers to the enum's

Availability of static_assert c++11

随声附和 提交于 2019-12-08 11:54:09
问题 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

Error when using static_assert on Netbeans

左心房为你撑大大i 提交于 2019-12-08 03:30:54
问题 I use Netbeans to develop a Java / JNI / C++ application. In my c++ classes, I use static_assert and Netbeans displays an error: static_assert(myvariable == 2, "My test.") ; The error is: Unable to resolve identifier static_assert And yes, I've included "type_traits". But, the code compiles and runs perfectly . Is that a bug from NetBeans? 回答1: I've used C++ on NetBeans for many months now, and it appears to be one of the many bugs of NetBeans with C++. Many times, static_assert or other