c++17

why declare constrexpr constructors for classes with non-trivial destructors (e.g. unique_ptr, std::variant)

末鹿安然 提交于 2019-12-03 16:36:49
问题 As far as I understand (at least for c++14 ), a destructor cannot be constexpr if it is not trivial (implicit generated or =default ). What is the point of declaring constexpr constructors for structures with non-trivial destructors? struct X { int a_; constexpr X(int a) : a_{a} {} // constexpr ~X(){}; // Error dtor cannot be marked constexpr // ~X(){}; // causes error at y declaration: temporary of non-literal type ‘X’ // in a constant expression . }; template <int N> struct Y {}; int main()

Determine whether a constructor of an abstract base class is noexcept?

此生再无相见时 提交于 2019-12-03 16:27:06
问题 In C++11 and later, how to determine whether a constructor of an abstract base class is noexcept ? The following methods don't work: #include <new> #include <type_traits> #include <utility> struct Base { Base() noexcept; virtual int f() = 0; }; // static assertion fails, because !std::is_constructible<Base>::value: static_assert(std::is_nothrow_constructible<Base>::value, ""); // static assertion fails, because !std::is_constructible<Base>::value: static_assert(std::is_nothrow_default

Nested template argument deduction for class templates not working

家住魔仙堡 提交于 2019-12-03 16:20:51
问题 In this Q&A I wrote a little wrapper class that provides reverse iterator access to a range, relying on the c++1z language feature template argument deduction for class templates (p0091r3, p0512r0) #include <iostream> #include <iterator> #include <vector> template<class Rng> class Reverse { Rng const& rng; public: Reverse(Rng const& r) noexcept : rng(r) {} auto begin() const noexcept { using std::end; return std::make_reverse_iterator(end(rng)); } auto end() const noexcept { using std::begin;

Modern C++: initialize constexpr tables

折月煮酒 提交于 2019-12-03 16:17:11
问题 Suppose I have a class X , which functionality requires a lot of constant table values, say an array A[1024] . I have a recurrent function f that computes its values, smth like A[x] = f(A[x - 1]); Suppose that A[0] is a known constant, therefore the rest of the array is constant too. What is the best way to calculate these values beforehand, using features of modern C++, and without storaging file with hardcoded values of this array? My workaround was a const static dummy variable: const bool

How to check if a pointer points to a properly aligned memory location?

喜欢而已 提交于 2019-12-03 15:44:05
问题 Given a void * to some storage, how to check whether it points to properly aligned storage without any implementation defined behavior? Of course we have std::align, but is there a more effective way to do this? template <std::size_t alignment> inline bool is_aligned(void * ptr) noexcept { std::size_t max = 1u; return std::align(alignment, 1u, ptr, max); } PS: I need to do this in a C++ standards-compatible fashion, without relying on any platform-specific (implementation defined) hacks. PPS:

Why is `std::byte` an enum class instead of a class?

孤者浪人 提交于 2019-12-03 15:19:56
问题 std::byte is an abstraction that is supposed to provide a type safe(r) access to regions of memory in C++, starting with the new standard 17. However, it's declared this way according to http://en.cppreference.com/w/cpp/types/byte: enum class byte : unsigned char {} ; That is, it is an enum class without any enumerations. Since usually the purpose of enums is to provide a restricted set of enumerations, this seems a bit strange. A class with a private unsigned char member seems like the more

Is “enum class” a class type in C++?

折月煮酒 提交于 2019-12-03 14:23:11
问题 I read about enumeration declaration in C++ using cppreference. Then I have made Enum class and check whether it is a class type or not using std::is_class . #include <iostream> enum class Enum { red = 1, blue, green }; int main() { std::cout << std::boolalpha; std::cout << std::is_class<Enum>::value << '\n'; } Then I compiled and ran in G++ compiler on Linux platform, it prints false value. So Is enum class type or not? If enum is a class type, then why I'm getting false value? 回答1: enum

C++17: still using enums as constants? [duplicate]

一个人想着一个人 提交于 2019-12-03 12:39:29
This question already has answers here : Replacing constants: when to use static constexpr and inline constexpr? (2 answers) I am used to using enum as constants -- they're quick to write, can be placed in .h files, and work fine. enum {BOX_LEFT=10, BOX_TOP=50, BOX_WIDTH=100, BOX_HEIGHT=50}; enum {REASONS_I_LIKE_ENUM_AS_CONSTANTS = 3}; Is this no longer a good idea? I see good reasons to prefer enum class (conventional enums implicitly convert to int; conventional enums export their enumerators to the surrounding scope), but those are reasons to prefer old enum in this case. I see in a thread

[[maybe_unused]] on enumerator

喜欢而已 提交于 2019-12-03 11:56:54
Looking at the specification of the [[maybe_unused]] , it states: Appears in the declaration of a class, a typedef­, a variable, a non­static data member, a function, an enumeration, or an enumerator. If the compiler issues warnings on unused entities, that warning is suppressed for any entity declared maybe_unused. As this mentions enumerator, I kinda expect it to have a use-case. As the only thing I could come up with is the -Wswitch warning, I tried it with Clang, GCC and MSVC. enum A { B, C [[maybe_unused]] }; void f(A a) { switch (a) { case B: break; } } All 3 compilers give me a

Infinity not constexpr

会有一股神秘感。 提交于 2019-12-03 11:47:29
I wanted to test the behavior of floats near infinity. For that I naively wrote the following code: #include <limits> #include <iostream> int main() { constexpr float foo = std::numeric_limits<float>::infinity() - std::numeric_limits<float>::epsilon(); std::cout << foo << std::endl; return foo; } The interesting part to me was that this compiles fine in GCC 7.2 but fails on Clang 5 (complaining about non-constexpr assign of foo ). AFAIK, since C++11, std::numeric_limits<float>::infinity() and infinity() are constexpr , so I am wondering where the problem lies for Clang. EDIT 1: Removed