static-assert

static assert that template typename T is NOT complete?

試著忘記壹切 提交于 2019-12-30 03:14:05
问题 Is there a way to static_assert that a type T is Not complete at that point in a header? The idea is to have a compile error if someone adds #includes down the road in places they should not be. related: How to write `is_complete` template? Using that link's answer, namespace { template<class T, int discriminator> struct is_complete { static T & getT(); static char (& pass(T))[2]; static char pass(...); static const bool value = sizeof(pass(getT()))==2; }; } #define IS_COMPLETE(X) is_complete

Static assertions and SFINAE

为君一笑 提交于 2019-12-30 02:11:51
问题 Consider this: template <typename T> struct hash { static_assert(false,"Not implemented."); }; struct unhashable {}; template <typename T> auto test(const T &t) -> decltype((*(hash<T> const *)nullptr)(t),int); void test(...); int main() { std::cout << std::is_same<decltype(test(std::declval<unhashable>())),void>::value; } Apart from obviously missing headers, should this compile? In other words, I am asking if the static assertion failure triggering inside a trailing decltype while deducing

BOOST_STATIC_ASSERT without boost

梦想的初衷 提交于 2019-12-28 01:55:10
问题 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. 回答1: 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

In VS2010, is it possible to use static_assert to verify an assumption about the offset of a variable from the start of a class?

有些话、适合烂在心里 提交于 2019-12-24 09:26:41
问题 Here is a simplified example: class A { enum {OFFSET = 4}; //Due to packing bool m_bool; }; template<class T> class B : public A { MyClass<T> m_class; }; Now supposing that class A can make use of a subset of MyClass's functionality via a base-class of MyClass, what I wish to do is verify an assumption about the location of 'm_class' with respect to an instance of class A. I have tried the following code from within a member-function of class B, but it gives an error ("expected constant

Securing CRTP: is private destructor the only solution?

丶灬走出姿态 提交于 2019-12-23 13:12:04
问题 How to avoid template <typename Derived> struct base { int foo() { return static_cast<Derived*>(this)->bar(); } }; struct derived : base<derived> { int bar(); }; struct another_derived : base<derived> { int bar(); }; // error: wrong base without additional code in derived classes ? This has been asked twice before (though without the extra condition of avoiding additional code in derived classes), with the recommended answer template <typename Derived> struct base { int foo() { return static

Securing CRTP: is private destructor the only solution?

拟墨画扇 提交于 2019-12-23 13:09:24
问题 How to avoid template <typename Derived> struct base { int foo() { return static_cast<Derived*>(this)->bar(); } }; struct derived : base<derived> { int bar(); }; struct another_derived : base<derived> { int bar(); }; // error: wrong base without additional code in derived classes ? This has been asked twice before (though without the extra condition of avoiding additional code in derived classes), with the recommended answer template <typename Derived> struct base { int foo() { return static

Is std::is_same<t,t>::value always true?

假装没事ソ 提交于 2019-12-23 08:47:10
问题 I've inherited some code that looks like this: /// /// A specializable function for converting a user-defined object to a string value /// template <typename value_type> std::string to_string(const value_type &value) { static_assert(!std::is_same<value_type, value_type>::value, "Unspecialized usage of to_string not supported"); return ""; } /// /// A specializable function for converting a user-defined object from a string to a value /// template <typename return_type> return_type from_string

Macro for use in expression while enforcing its arguments to be compile time constants

試著忘記壹切 提交于 2019-12-23 01:44:11
问题 I am looking for a way to #define a macro that enforces its arguments to be compile time constants, and at the same time can be used in an expression. The method should be working under C90 and be upward compatible - if possible also portable for the different C++ variants. Also a 0-footprint to memory is preferable. Consider a compile-time minimum macro as an example. The behavior should be: #define CT_MIN(CTC_A, CTC B) <<<MAGIC>>> int a = 1; int b = a + CT_MIN(1,4); /* OK */ int c = a + CT

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

橙三吉。 提交于 2019-12-23 00:52:31
问题 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

C++11 static assert for equality comparable type?

强颜欢笑 提交于 2019-12-21 05:24:08
问题 How to static_assert a template type is EqualityComparable concept in C++11? 回答1: You could use the following type trait: #include <type_traits> template<typename T, typename = void> struct is_equality_comparable : std::false_type { }; template<typename T> struct is_equality_comparable<T, typename std::enable_if< true, decltype(std::declval<T&>() == std::declval<T&>(), (void)0) >::type > : std::true_type { }; Which you would test this way: struct X { }; struct Y { }; bool operator == (X const