constexpr

is bit_cast without compiler support for constexpr memcpy possible?

為{幸葍}努か 提交于 2019-12-08 04:32:51
问题 I had heard that std::bit_cast will be in C++20, and I am slightly puzzled about the conclusion that implementing it necessarily requires special compiler support. To be fair, the argument I have heard is that the implementation performs a memcpy operation, and memcpy is not typically constexpr , while std::bit_cast is supposed to be, so making std::bit_cast constexpr supposedly requires compiler support for a constexpr -compliant memcpy operation. I was wondering, however, if it was possible

Initializing `constexpr` Array with Pattern

此生再无相见时 提交于 2019-12-08 02:27:22
问题 I would like to initialize a constexpr array with a pattern that is generated using variadic template parameters. For simplicity, consider the problem of initializing a constexpr unsigned static array with the sizes of a list of types, say, unsigned, short, char, int, long . How can I do this so that all of the computation is done during compile time? I need the solution to play nice with the C++ type system, so I cannot use macros. The best I could come up with is shown below, but

Returning a class from a constexpr function requires virtual keyword with g++

浪尽此生 提交于 2019-12-08 02:14:58
问题 Hi the following program works with g++ 4.9.2 (Ubuntu 4.9.2-10ubuntu13) but the virtual keyword is required for the function get : //g++ -std=c++14 test.cpp //test.cpp #include <iostream> using namespace std; template<typename T> constexpr auto create() { class test { public: int i; virtual int get(){ return 123; } } r; return r; } auto v = create<int>(); int main(void){ cout<<v.get()<<endl; } If I omit the virtual keyword, I get the following error : test.cpp: In instantiation of ‘constexpr

Constexpr find implementation

这一生的挚爱 提交于 2019-12-07 06:22:56
问题 After answering this question and reading this talk and looking at this code, I want to implement constexpr find with just simple array class. Consider following example: #include <cstddef> template <class It, class T> constexpr auto constexpr_find(const It& b, const It& e, T value) { auto begin = b; while (begin != e) { if (*begin == value) break; ++begin; } return *begin; } template<typename T, size_t N> class array { public: typedef T* iterator; typedef const T* const_iterator; constexpr

Constexpr decltype

喜欢而已 提交于 2019-12-07 05:09:35
问题 I recently asked a question here (Detecting instance method constexpr with SFINAE) where I tried to do some constexpr detection at compile time. Eventually, I figured out that one can exploit noexcept to do this: any constant expression is also noexcept . So I put together the following machinery: template <class T> constexpr int maybe_noexcept(T && t) { return 0; } ... constexpr bool b = noexcept(maybe_noexcept(int{})); This works and b is true as you'd expect, as zero-initializing an int is

How can I have a temporary variable in a constexpr function?

℡╲_俬逩灬. 提交于 2019-12-07 05:04:11
问题 This is a simplified version what I would like to do. constexpr float f(float a, float b){ constexpr float temp = a+b; return temp*temp*temp; } In my version, a+b is something much more complicated, so I don't want to cut and paste it three times. Using 3*(a+b) is also not a working solution for the real function. I'm trying to keep the question related to syntax, and not algebra. I can get it to work by moving a+b to it's own constexpr function, but I'd prefer to not pollute the namespace

constexpr and ODR

被刻印的时光 ゝ 提交于 2019-12-07 04:29:38
问题 If we have a header file widget.hpp with the contents below: constexpr int foo = 10; struct widget { int bars[foo]; }; ...and we have two translation units generated from two source files which both only include widget.hpp , does this violate the one definition rule (more specifically, does the use of foo violate the one definition rule)? foo has internal linkage but it is also a constant expression. From my reading of 3.2.6 in the C++11 standard, which I will quote below, this is well-formed

Practical limitations on amount of constexpr computation

假装没事ソ 提交于 2019-12-07 04:05:41
问题 As an experiment, I just put together some code to generate a std::array<uint32_t, 256> at compile time. The table contents themselves are a fairly typical CRC lookup table - about the only new thing is the use of constexpr functions to calculate the entries as opposed to putting an autogenerated magic table directly in the source code. Anyway, this exercise got me curious: would there be any practical limitations on the amount of computation a compiler would be willing to do to evaluate a

Force Clang to “perform math early” on constant values

蹲街弑〆低调 提交于 2019-12-07 02:28:53
问题 This is related to How to force const propagation through an inline function? Clang has an integrated assembler; and it does not use the system's assembler (which is often GNU AS (GAS)). Non-Clang performed the math early, and everything "just worked". I say "early" because @n.m. objected to describing it as "math performed by the preprocessor." But the idea is the value is known at compile time, and it should be evaluated early, like when the preprocessor evaluates a #if (X % 32 == 0) .

Simple constexpr LookUpTable in C++14

情到浓时终转凉″ 提交于 2019-12-06 23:01:21
问题 I am trying to make a simple LookUpTable based on an array of integers, where the idea is to have it calculated at compile time . Trying to make it possible to use it for any other future tables of various integer types I might have, I need it as a template . So I have a LookUpTable.h #ifndef LOOKUPTABLE_H #define LOOKUPTABLE_H #include <stdexcept> // out_of_range template <typename T, std::size_t NUMBER_OF_ELEMENTS> class LookUpTableIndexed { private: //constexpr static std::size_t NUMBER_OF