static-assert

How to make static_assert play nice with SFINAE

会有一股神秘感。 提交于 2019-12-05 08:23:17
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<typename T> struct fake_alloc { using value_type = T; }; template<typename T, typename Alloc = fake

Enable template only for specific templated class

自作多情 提交于 2019-12-05 06:38:28
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; makeTypeErasedCallbackHandle(h); //should compile fine makeTypeErasedCallbackHandle(s); //should

Can static_assert check if a type is a vector?

早过忘川 提交于 2019-12-04 22:41:58
Can static_assert check if a type is a vector? IE, an int would raise the assertion, whereas a vector<int> would not. I'm thinking of something along the lines of: static_assert(decltype(T) == std::vector, "Some error") Yes. Consider the following meta function: #include <stdio.h> #include <vector> template <class N> struct is_vector { static const int value = 0; }; template <class N, class A> struct is_vector<std::vector<N, A> > { static const int value = 1; }; int main() { printf("is_vector<int>: %d\n", is_vector<int>::value); printf("is_vector<vector<int> >: %d\n", is_vector<std::vector<int

How do you static_assert the values in a parameter pack of a variadic template?

血红的双手。 提交于 2019-12-04 17:37:45
问题 I'm creating a variadic template. Let's say I have something like this: template<typename T, T ... Numbers> class Sequence final { // Unpack parameter pack into a constexpr array constexpr static T count = sizeof...(Numbers); constexpr static T numbers[count] = { Numbers... }; // ... } Instances of this class can be instantiated like: Sequence<uint32_t, 1, 2, 3, 42, 25> seq; I'd like to make sure at compile time using a static_assert that the numbers parameter pack only contains specific

Enforce template type through static_assert

橙三吉。 提交于 2019-12-04 10:07:44
问题 I'm trying to understand the usefulness of static_assert , and I want to know if it can help me in enforcing a design, and if so, how. I have a general template class that hides its own implementation inside another template class which is partially specialized based on the size of the template type. Here's a brief outline of this design: template <class T, size_t S = sizeof(T)> struct Helper; template <class T> struct Helper<T, sizeof(long)> { static T bar(); }; // ... other specializations

How to test whether expression is a temporary?

独自空忆成欢 提交于 2019-12-04 06:20:15
With the following macro: #define ASSERT_IF_TEMP(expr) static_assert(?, "Is temporary!"); What should I put for question mark? First we should clarify: What do you mean by "temporary"? Many people mean different things when they say temporary. Technically, int() is not a temporary, but most people will include them into their own meaning of that term. Technically, given std::string s; , then move(s) isn't a temporary either, but you may want to treat it as one with your macro. The first kind of "temporaries" I mentioned above are really "prvalue expressions". Those are the std::string("foo")

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

☆樱花仙子☆ 提交于 2019-12-04 03:17:39
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 second assert fails. Who's right? Is there any trick to make MSVC treat the class as pod? EDIT For

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

只谈情不闲聊 提交于 2019-12-04 00:42:32
问题 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? 回答1: Here is some code that exploits gcc's diagnostic messages to print values of interest after an

How do I check if a template parameter is a power of two?

旧巷老猫 提交于 2019-12-03 01:54:07
I want to create a structure that allocates statically an array of 2^N bytes , but I don't want the users of this structure to specify this size as the exponent. Example: my_stupid_array<char, 32> a1; // I want this! my_stupid_array<char, 5> a2; // And not this... How do I check if this template parameter is a power of two and warn the user with a nice message about this? I've been able to check for this with a simple template: template<int N> struct is_power_of_two { enum {val = (N >= 1) & !(N & (N - 1))}; }; However, I'm unable to warn the user about this with a sane message. Any ideas? EDIT

Conditional compilation of templates

大城市里の小女人 提交于 2019-12-02 21:28:06
问题 I am trying to get static_assert to help me avoid null pointers in C++11. The problem seems to be that C++11 require the compiler to compile templates even if they are not instantiated. I have the following code: #include <type_traits> template<typename T, typename... Us> std::enable_if_t< std::is_constructible<T, Us...>::value == true, T * > create_if_constructible(Us... args) { return new T(args...); } template<typename T, typename... Us> std::enable_if_t< std::is_constructible<T, Us...>: