static-assert

static_assert of const Variable

我与影子孤独终老i 提交于 2019-12-08 02:55:49
问题 I have this code: const float foo = 5.0F; static_assert(foo > 0.0F, "foo must be greater than 0."); But in visual-studio-2010 I get the error: error C2057: expected constant expression I'm actually doing this correctly and visual-studio-2010 just hasn't properly implemented static_assert , right? In visual-studio-2017 it works as intended. There has been some commentary of the differences between const and constexpr . I understand this difference, however many compilers support this use of

Is const int[2] trivially copyable?

别来无恙 提交于 2019-12-08 01:01:55
问题 I have a templated member function which looks somewhat like the following: template <typename T> int SendData( const T& tDataBuffer ) const { static_assert( std::is_trivially_copyable<T>::value, "The object type must be trivially copyable" ); // Send the data bitwise ... } I then call this function in a manner like the following: const int iArray[2] = {1, 2}; int iResult = pSocket->SendData( iArray ); When I compile this with Visual Studio 2012, I get no error message and the functionality

How to check, if the class is abstract, at compile time?

浪子不回头ぞ 提交于 2019-12-07 08:27:00
问题 By an abstract class I mean the class with at least one pure virtual method. I want the compilation to fail, if the check shows that the class is not abstract. Is it even possible? 回答1: Use std::is_abstract. #include <type_traits> static_assert(std::is_abstract<T>(), "T ought to be abstract."); See it in action. 来源: https://stackoverflow.com/questions/21369036/how-to-check-if-the-class-is-abstract-at-compile-time

How to make static_assert play nice with SFINAE

偶尔善良 提交于 2019-12-07 05:00:33
问题 Update I posted a working rough draft of rebind as an answer to the question. Though I didn't have much luck finding a generic way to keep static_assert s from breaking metafunctions. Basically I want to check if a templated type T<U, Args...> can be constructed from some other type T<V, Args...> . Where T and Args... is the same in both types. The problem is, T<> might have a static_assert in it that totally breaks my metafunction. Below is a rough summary of what I'm trying to do. template

Getting std::complex<double> to pass std::is_floating_point test

半世苍凉 提交于 2019-12-07 04:27:41
问题 I want the types double , float , complex<double> and complex<float> to pass a static_assert condition. I figured static_assert(std::is_floating<T>::value, "some message") would do the trick, but the complex types do not pass this test (at least under gcc-4.10). What predicate would I add to make sure these four types (and perhaps long double s as well) are allowed as template instantiations, but nothing else? 回答1: It is generally illegal to add specializations for standard library type trait

Enable template only for specific templated class

北城余情 提交于 2019-12-07 02:05:56
问题 This question is inspired from my previous question No template parameter deduction of parameter pack. Consider following code example: #include <memory> #include <string> template<typename... FArgs> class Callback { public: class Handle{}; }; class BaseCallbackHandle { }; using TypeErasedCallbackHandle = std::unique_ptr<BaseCallbackHandle>; template<typename H> TypeErasedCallbackHandle makeTypeErasedCallbackHandle( H handle) { return {}; } int main() { Callback<int>::Handle h; std::string s;

Is const int[2] trivially copyable?

与世无争的帅哥 提交于 2019-12-06 14:12:51
I have a templated member function which looks somewhat like the following: template <typename T> int SendData( const T& tDataBuffer ) const { static_assert( std::is_trivially_copyable<T>::value, "The object type must be trivially copyable" ); // Send the data bitwise ... } I then call this function in a manner like the following: const int iArray[2] = {1, 2}; int iResult = pSocket->SendData( iArray ); When I compile this with Visual Studio 2012, I get no error message and the functionality of the program is how I would expect (i.e. the data is sent bitwise), however, when compiling with the

MSVC12 thinks aggregate derived from std::array is not pod

◇◆丶佛笑我妖孽 提交于 2019-12-05 23:29:41
问题 Given the following #include <array> struct litmus final : std::array<unsigned char, 16> { }; static_assert(std::is_pod<std::array<unsigned char, 16> >::value, "not pod"); // this fails on MSVC: static_assert(std::is_pod<litmus>::value, "not pod"); The following compilers agree that litmus is pod: clang++ version 3.5 (trunk 198621) http://coliru.stacked-crooked.com/a/7add7a2fe58a7e38 g++ 4.8.1 http://coliru.stacked-crooked.com/a/74cfe97f06c8c128 However, MSVC12 (VS2013 RTM) maintains that the

How to check, if the class is abstract, at compile time?

空扰寡人 提交于 2019-12-05 16:14:42
By an abstract class I mean the class with at least one pure virtual method. I want the compilation to fail, if the check shows that the class is not abstract. Is it even possible? Use std::is_abstract . #include <type_traits> static_assert(std::is_abstract<T>(), "T ought to be abstract."); See it in action . 来源: https://stackoverflow.com/questions/21369036/how-to-check-if-the-class-is-abstract-at-compile-time

Getting std::complex<double> to pass std::is_floating_point test

微笑、不失礼 提交于 2019-12-05 09:34:00
I want the types double , float , complex<double> and complex<float> to pass a static_assert condition. I figured static_assert(std::is_floating<T>::value, "some message") would do the trick, but the complex types do not pass this test (at least under gcc-4.10). What predicate would I add to make sure these four types (and perhaps long double s as well) are allowed as template instantiations, but nothing else? It is generally illegal to add specializations for standard library type trait classes, even for user-defined types. §20.10.2 [meta.type.synop]/p1: The behavior of a program that adds