static-assert

What does static_assert do, and what would you use it for?

末鹿安然 提交于 2019-11-27 16:58:50
Could you give an example where static_assert(...) 'C++0x' would solve the problem in hand elegantly? I am familiar with run-time assert(...) . When should I prefer static_assert(...) over regular assert(...) ? Also, in boost there is something called BOOST_STATIC_ASSERT , is it the same as static_assert(...) ? Off the top of my head... #include "SomeLibrary.h" static_assert(SomeLibrary::Version > 2, "Old versions of SomeLibrary are missing the foo functionality. Cannot proceed!"); class UsingSomeLibrary { // ... }; Assuming that SomeLibrary::Version is declared as a static const, rather than

How to do static_assert with macros?

醉酒当歌 提交于 2019-11-27 15:57:14
I have tried to use this suggestion to do a static assert, but I do not get a compilation error if I use it within a method of a template. The example follows : #include <iostream> #define STATIC_ASSERT(expr, msg) \ { \ char STATIC_ASSERTION__##msg[(expr)?1:-1]; \ (void)STATIC_ASSERTION__##msg[0]; \ } template <typename T > class A { public: int foo(const int k ) { // does not work STATIC_ASSERT( k > 9, error_msg ); return k+5; } }; int bar(const int k ) { // works fine //STATIC_ASSERT( k > 9, error_msg ); return k+5; } int main() { A<int> a; const int v = 2; std::cout<<a.foo(v)<<std::endl;

How to check if two types are same at compiletime(bonus points if it works with Boost strong typedef)

十年热恋 提交于 2019-11-27 11:37:15
问题 I was wondering if it is possible to check if 2 types are same at compile time. What I came up with is(idk if it works because it feels hackish and IDK standard that good so IDK what to look for when testing). #include <boost/strong_typedef.hpp> BOOST_STRONG_TYPEDEF(double, cm); BOOST_STRONG_TYPEDEF(double, inch); template<typename T, typename U> static constexpr void __help() { } template<typename T, typename U> class AreSameType { public: constexpr operator bool() { return &__help<T,U> == &

How do I restrict a template class to certain built-in types?

不想你离开。 提交于 2019-11-27 10:22:23
问题 This issue has been discussed a few times but all the solutions I have found either didn't work or were based on boost's static assert. My problem is simple. I have a class, and I only want to allow real types (double and float). I want a compile-time error if I try to instantiate the class with a type other than float or double. I am using Visual C++ 11. Here is what I have tried: template <typename RealType> class A { // Warning C4346 static_assert(std::is_same<RealType, double>::value ||

constexpr if and static_assert

喜欢而已 提交于 2019-11-27 09:01:02
P0292R1 constexpr if has been included , on track for C++17. It seems useful (and can replace use of SFINAE), but a comment regarding static_assert being ill-formed, no diagnostic required in the false branch scares me: Disarming static_assert declarations in the non-taken branch of a constexpr if is not proposed. void f() { if constexpr (false) static_assert(false); // ill-formed } template<class T> void g() { if constexpr (false) static_assert(false); // ill-formed; no // diagnostic required for template definition } I take it that it's completely forbidden to use static_assert inside

BOOST_STATIC_ASSERT without boost

百般思念 提交于 2019-11-27 03:51:01
Since boost is forbidden in a company I work for I need to implement its functionality in pure C++. I've looked into boost sources but they seem to be too complex to understand, at least for me. I know there is something called static_assert() in the C++0x standart, but I'd like not to use any C++0x features. AProgrammer One other trick (which can be used in C) is to try to build an array with a negative size if the assert fail: #define ASSERT(cond) int foo[(cond) ? 1 : -1] as a bonus, you may use a typedef instead of an object, so that it is usable in more contexts and doesn't takes place

How to write runnable tests of static_assert?

此生再无相见时 提交于 2019-11-27 03:11:12
问题 I am writing a unit-test suite for a source code library that contains static_assert s. I want to provide assurance these static_assert s do no more and no less than they are desired to do, in design terms. So I would like to be able to test them. I could of course add uncompilable unit tests of the interface that cause the static assert s to be violated by a comprehensive variety of means, and comment or #if 0 them all out, with my personal assurance to the user that if any of them are un

Integrate type name in static_assert output?

随声附和 提交于 2019-11-26 19:25:39
问题 I like to give helpful errors / messages, and I also want to do so for my static_assert s. The problem is, that they depend on template parameters. Normally, those parameters will get displayed on way or an other due to the error raised, but they are either obscure or not grouped so they make sense. Example: template<class T> struct fake_dependency{ static bool const value = false; }; template<class T, class Tag> struct Foo{ Foo(){} template<class OtherTag> Foo(Foo<T, OtherTag> const&){

What does static_assert do, and what would you use it for?

天大地大妈咪最大 提交于 2019-11-26 18:48:42
问题 Could you give an example where static_assert(...) ('C++11') would solve the problem in hand elegantly? I am familiar with run-time assert(...) . When should I prefer static_assert(...) over regular assert(...) ? Also, in boost there is something called BOOST_STATIC_ASSERT , is it the same as static_assert(...) ? 回答1: Off the top of my head... #include "SomeLibrary.h" static_assert(SomeLibrary::Version > 2, "Old versions of SomeLibrary are missing the foo functionality. Cannot proceed!");

C++11 - static_assert within constexpr function?

故事扮演 提交于 2019-11-26 18:48:07
How would one properly do a static_assert within a constexpr function? For example: constexpr int do_something(int x) { static_assert(x > 0, "x must be > 0"); return x + 5; } This is not valid C++11 code, because a constexpr function must only contain a return statement. I don't think that the standard has an exception to this, although, the GCC 4.7 does not let me compile this code. This is not valid C++11 code, because a constexpr function must only contain a return statement. This is incorrect. static_assert in a constexpr function are fine. What is not fine is using function parameters in