static-assert

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

戏子无情 提交于 2019-11-29 14:57:30
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; v.z = 3; return 0; } It compiles fine: http://ideone.com/wHbJYP . I would expect static_assert(0,

static_assert and class templates

Deadly 提交于 2019-11-29 11:57:56
I have a problem with the static_assert feature. When I instantiate a class template directly, everything works as expected. But when I pass it as a parameter for the different class template, static_assert does not work. template <int X> class A{ static_assert(X == 0, "X != 0"); }; template <class T> class B{ }; B<A<1>> b; // Static assert does not work A<1> a; // error: static assertion failed: X != 0 EDIT Thanks all for the answers. Is there a way to explicitly instantiate A without creation of A instances / inheriting from A? I was trying this: template <int X> class A{ static_assert(X ==

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

别说谁变了你拦得住时间么 提交于 2019-11-29 11:04:35
问题 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

Can we use static_assert to detect padding in a struct?

邮差的信 提交于 2019-11-29 08:42:16
This is a follow-up to this other question I was trying to establish at compile time whether a specific implementation had added unnamed padding inside a struct. Specific implementation like gcc allow to use pragmas to control padding and alignment in structs but at the price of compatibility with other implementations. As both static_assert and offset_of are required by the n1570 draft for C11, I wanted to use them to see whether an implementation used padding between members. Here is the relevant part of code (full code in referenced question): #include <stdio.h> #include <stddef.h> #include

Ensure derived class implements static method

百般思念 提交于 2019-11-29 07:06:11
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 : public Base <Derived> { public: static int foo(int i) { return 42; }; }; However, it does not compile

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

好久不见. 提交于 2019-11-28 17:16:18
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 || std::is_same<RealType, float>::value); } template <typename RealType> class A { // Error C2062: type

/boost/lockfree/queue.hpp: error: static assertion failed: (boost::has_trivial_destructor<T>::value)

时光怂恿深爱的人放手 提交于 2019-11-28 08:18:07
问题 I'm trying to substitute boost::lockfree::queue for std::queue in this file https://github.com/zaphoyd/websocketpp/blob/experimental/examples/broadcast_server/broadcast_server.cpp I've added #include <boost/lockfree/queue.hpp> ; changed line 130 , std::queue<action> m_actions; , to boost::lockfree::queue<action> m_actions; ; removed all lines having to do with locking; and changed line 103 , m_actions.pop(); , to m_actions.pop(a); . I get these errors when I scons broadcast_server_lockfree in

static_assert and class templates

房东的猫 提交于 2019-11-28 05:06:44
问题 I have a problem with the static_assert feature. When I instantiate a class template directly, everything works as expected. But when I pass it as a parameter for the different class template, static_assert does not work. template <int X> class A{ static_assert(X == 0, "X != 0"); }; template <class T> class B{ }; B<A<1>> b; // Static assert does not work A<1> a; // error: static assertion failed: X != 0 EDIT Thanks all for the answers. Is there a way to explicitly instantiate A without

Can we use static_assert to detect padding in a struct?

99封情书 提交于 2019-11-28 01:55:59
问题 This is a follow-up to this other question I was trying to establish at compile time whether a specific implementation had added unnamed padding inside a struct. Specific implementation like gcc allow to use pragmas to control padding and alignment in structs but at the price of compatibility with other implementations. As both static_assert and offset_of are required by the n1570 draft for C11, I wanted to use them to see whether an implementation used padding between members. Here is the

Integrate type name in static_assert output?

為{幸葍}努か 提交于 2019-11-27 18:27:31
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&){ static_assert(fake_dependency<T>::value, "Cannot create Foo<T,Tag> from Foo<T,OtherTag>."); } }; int main(){