static-assert

Force deriving from a class virtually

☆樱花仙子☆ 提交于 2019-12-01 03:51:18
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. IMO, there is no clean and platform independent solution available to this problem. The best way is to manually go and change each and every inheritance to

How to print result of a compile-time calculation in C++?

有些话、适合烂在心里 提交于 2019-12-01 03:48:07
I've wrote several constexpr functions and use them in static_asserts to control some resource limits. But I'd like to not only enforce compile-time predicate but also to see the actual values calculated during normal compilation process or at least when assertion fails. There are ways to print string messages during compilation, but what's about printing results of constexpr computations? Here is some code that exploits gcc's diagnostic messages to print values of interest after an assert message. To find the values of interest, you just need to search the error string for T x = : #include

template metafunction for detecting template specialisations

人走茶凉 提交于 2019-12-01 03:46:56
Inspired by this question , i'm wondering if there is some compile-time check one can introduce to detect if two given template instantiations: template <typename T> class Templ... typedef Templ<std::string> stringInstance; typedef Templ<double> doubleInstance; are constructed from the same definition, or if they are built from different specializations of the Templ template so basically the hypothetical template function will behave like this: template <typename T> class Templ {} template <> class Templ<std::string> {} template <> class Templ<double> {} template <typename T1,typename T2>

Using std::extent on std::array

六月ゝ 毕业季﹏ 提交于 2019-12-01 03:21:49
问题 I have a templatized function and I want to static_assert that it's type has a size of three. This code illustrates what I'm trying to do, but doesn't work: template < typename T > void foo( T& param ) { // This line is the one that I need to figure out how to write static_assert( 3 == std::extent< T >::value, "param must have a size of 3" ); } int main( void ) { int cArray[3]; std::array< int, 3 > stdArray; foo( cArray ); foo( stdArray ); } 回答1: std::extent is defined for built-in arrays.

Optimization, asserts and release mode

和自甴很熟 提交于 2019-12-01 02:25:51
问题 Consider a function void f() { assert(condition); ... } In debug mode, where asserts are enabled, the compiler is free to assume condition holds, since the remaining code will not be executed if it does not. However, in release mode, I believe the compiler will only see void f() { ... } and can no longer assume condition . Are there any compiler directives or static assert tricks to let compiler know about certain invariants? 回答1: This can't be done in portable C or C++. Some compilers

template metafunction for detecting template specialisations

放肆的年华 提交于 2019-12-01 01:46:04
问题 Inspired by this question, i'm wondering if there is some compile-time check one can introduce to detect if two given template instantiations: template <typename T> class Templ... typedef Templ<std::string> stringInstance; typedef Templ<double> doubleInstance; are constructed from the same definition, or if they are built from different specializations of the Templ template so basically the hypothetical template function will behave like this: template <typename T> class Templ {} template <>

Display integer at compile time in static_assert()

。_饼干妹妹 提交于 2019-11-30 18:43:21
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 the value of First::nbElementFirstEnum and Second::nbElementSecondEnum in the assert message (like in the

Does GCC have a built-in compile time assert?

被刻印的时光 ゝ 提交于 2019-11-30 11:16:19
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? According to this page , gcc has had static_assert since 4.3. If you need to use a gcc version which does not support it you can use #include <boost/static_assert.hpp> BOOST_STATIC_ASSERT( /* assertion */ ) Basically, what

Is there a compile-time func/macro to determine if a C++0x struct is POD?

倖福魔咒の 提交于 2019-11-30 08:05:34
I'd like to have a C++0x static_assert that tests whether a given struct type is POD (to prevent other programmers from inadvertently breaking it with new members). ie, struct A // is a POD type { int x,y,z; } struct B // is not a POD type (has a nondefault ctor) { int x,y,z; B( int _x, int _y, int _z ) : x(_x), y(_y), z(_z) {} } void CompileTimeAsserts() { static_assert( is_pod_type( A ) , "This assert should not fire." ); static_assert( is_pod_type( B ) , "This assert will fire and scold whoever added a ctor to the POD type." ); } Is there some kind of is_pod_type() macro or intrinsic that I

constexpr std::array with static_assert

我只是一个虾纸丫 提交于 2019-11-30 02:40:27
问题 #include <iostream> #include <array> int main(int argc, char **argv) { constexpr const std::array<int, 2> arr {{ 0, 1 }}; constexpr const int arr2[] = { 0, 1}; static_assert(arr[0] == arr2[0], "asdf"); static_assert(arr[1] == arr2[1], "asdfasdf"); return 0; } When compiled with gcc 4.8.2 and 4.9.1 using g++ test.cpp --std=c++11 , the compilation succeeds. When compiled with clang 3.4 and 3.5 using clang++ test.cpp --std=c++11 however, the compilation fails: test.cpp:8:16: error: static_assert