constexpr

Are there cases where constexpr should be avoided, even it it could be used?

不羁的心 提交于 2019-12-04 03:34:48
If an object is declared const , its value is guaranteed to be available only at runtime, but if it's declared constexpr , the value is guaranteed to be available both during compilation and at runtime. So if I have an object whose value is available during compilation, are there any situations where I should not declare it constexpr ? const int magicValue = 42; // Does this ever make sense // (using const instead of constexpr)? For functions, if a function can return a value computed during compilation when passed arguments with values available during compilation, would it ever make sense to

Array Initialisation Compile Time - Constexpr Sequence

家住魔仙堡 提交于 2019-12-04 03:32:20
I was reading this question on SO. The question itself is not so interesting, but I was wondering whether it exists and how to implement a compile time solution. Regard to the first sequence: All numbers except the ones which can be divided by 3. The sequence should be something like: [1, 2, 4, 5, 7, 8, 10, 11, 13, 14, ...] By induction, I've found the math formula for that sequence: f(0) = 0; f(x > 0) = floor[(3x - 1) / 2]; So I've implemented a C++ constexpr function which generates the i-th number in the sequence: #include <type_traits> template <typename T = std::size_t> constexpr T

are constexpr functions that load files possible in C++?

一世执手 提交于 2019-12-04 03:27:48
There was a similar question here but it had no valuable information so I want to ask this again - is it possible to load contents of an arbitrary file using a constexpr function? I know that this seems impossible since all the possible functions that allow file I/O ( fopen , open ...) are not constexpr so cannot be called in this scenario. However - since there are many people here who follow the development of c++17 and on - is there any hope that further standards will include some file I/O API that will be constexpr and could be used to load a file at compile time? Just for a comparison -

About ODR-violations and template variables

不打扰是莪最后的温柔 提交于 2019-12-04 03:25:19
I know that template functions don't suffer of multiple definitions when linking, like member functions defined inside a class, which are inline by default. Also, constexpr objects have internal linkage, but template variables have external linkage (I mean at namespace scope and for C++14 in both cases). What about? template<class T> constexpr T i_am_odr_safe{}; Does i_am_odr_safe have external or internal linkage in C++14? and is it safe regarding multiple-definitions like function templates? In other words, is i_am_odr_safe odr-safe? This is core issue 1713 , the direction of which IIRC is

How to use static_assert for constexpr function arguments in C++?

戏子无情 提交于 2019-12-04 03:07:32
问题 I have several brief constexpr functions in my libraries that perform some simple calculations. I use them both in run-time and compile-time contexts. I would like to perform some assertions in the body of these functions, however assert(...) is not valid in a constexpr function and static_assert(...) can not be used to check function parameters. Example: constexpr int getClamped(int mValue, int mMin, int mMax) noexcept { assert(mMin <= mMax); // does not compile! return mValue < mMin ? mMin

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

Possible to instantiate templates using a for loop in a C++14 constexpr function?

笑着哭i 提交于 2019-12-03 23:39:32
问题 I've been messing around with an SVN build of clang to experiment with the relaxed rules for constexpr . One of the things I've been unable to determine as of yet is whether it's possible to loop through the elements inside a tuple at compile-time in a constexpr function. Because I don't have a C++14 compliant standard library to test on, I've prepared the following equivalent test: template<int N> constexpr int foo() { return N; } constexpr int getSum() { auto sum = 0; for (auto i = 0; i <

Why aren't std::algorithms constexpr and which could be?

£可爱£侵袭症+ 提交于 2019-12-03 23:17:45
问题 Why aren't any std::algorithm methods constexpr ? If I understand the new C++14 rules correctly, many of these methods could be constexpr . For example, why can't std::find be constexpr ? static constexpr std::array<char, 4> DnaBases {'A', 'C', 'G', 'T'}; constexpr bool is_dna(char b) { return std::find(std::cbegin(DnaBases), std::cend(DnaBases), b) != std::cend(DnaBases); // why not? } Which other std::algorithm s could be constexpr ? 回答1: It could be constexpr , but cannot be evaluated as a

lambda as a static member

不想你离开。 提交于 2019-12-03 23:15:46
问题 I'm trying to use a lambda as a static member, like this: struct A { static constexpr auto F = [](){}; }; int main() { A::F(); return 0; } Is this even correct C++11 code? On clang, I get this error: error: constexpr variable 'F' must be initialized by a constant expression static constexpr auto F = [](){}; ^~~~~~ It seems in clang, lambdas aren't considered a constant expression. Is this correct? Perhaps they haven't fully implemented lambdas yet in clang because gcc 4.7 seems to allow it as

Why can't decomposition declarations be constexpr?

好久不见. 提交于 2019-12-03 22:03:15
Consider the following snippet to test the upcoming C++17 feature decomposition declarations (formerly known as structured bindings) #include <cassert> #include <utility> constexpr auto divmod(int n, int d) { return std::make_pair(n / d, n % d); // in g++7, also just std::pair{n/d, n%d} } int main() { constexpr auto [q, r] = divmod(10, 3); static_assert(q == 3 && r ==1); } This fails on both g++7-SVN and clang-4.0-SVN with the message: decomposition declaration cannot be declared 'constexpr' Dropping the constexpr definition and changing to a regular assert() works on both compilers. None of