constexpr

Constexpr member function on l-value ref object: Clang and gcc disagree

耗尽温柔 提交于 2021-02-07 06:05:53
问题 When a class has a constexpr member function and that member function is being evaluated on an l-value object in a constexpr context, clang and gcc disagree whether the result is a constexpr value. Why? Is there a workaround that needs neither default-constructability nor copy-constructability? When the object is passed by value, both compilers succeed compiling. Clang versions trunk, 8, 7: static_assert expression is not an integral constant expression and Gcc versions trunk, 8.1, 7.4:

How does the C++ compiler evaluate recursive constexpr functions so quickly?

纵然是瞬间 提交于 2021-02-05 20:12:51
问题 I've been learning about C++ constexpr functions, and I implemented a constexpr recursive function to find the nth fibonacci number. #include <iostream> #include <fstream> #include <cmath> #include <algorithm> #include <vector> constexpr long long fibonacci(int num) { if (num <= 2) return 1; return fibonacci(num - 1) + fibonacci(num - 2); } int main() { auto start = clock(); long long num = fibonacci(70); auto duration = (clock() - start) / (CLOCKS_PER_SEC / 1000.); std::cout << num << "\n" <

How does the C++ compiler evaluate recursive constexpr functions so quickly?

[亡魂溺海] 提交于 2021-02-05 20:11:58
问题 I've been learning about C++ constexpr functions, and I implemented a constexpr recursive function to find the nth fibonacci number. #include <iostream> #include <fstream> #include <cmath> #include <algorithm> #include <vector> constexpr long long fibonacci(int num) { if (num <= 2) return 1; return fibonacci(num - 1) + fibonacci(num - 2); } int main() { auto start = clock(); long long num = fibonacci(70); auto duration = (clock() - start) / (CLOCKS_PER_SEC / 1000.); std::cout << num << "\n" <

Clang 3.7.0 complains of class not being literal because it is not an aggregate and has no constexpr constructors

孤街醉人 提交于 2021-02-05 07:23:20
问题 The following code compiles fine in GCC (4.9.3) and VC++ (19.00.23506) but gives these error in Clang (3.7.0). error: constexpr function's return type 'Foo' is not a literal type note: 'Foo' is not literal because it is not an aggregate and has no constexpr constructors other than copy or move constructors Code: #include <iostream> #include <vector> struct Foo { std::vector<int> m_vec; Foo(const int *foo, std::size_t size=0):m_vec(foo, foo+size) {;} //Foo(const std::initializer_list<int>

constexpr begin of a std::array

元气小坏坏 提交于 2021-02-03 23:03:51
问题 I am having trouble understanding why both gcc-8.2.0 and clang-7.0.0 reject the following code (live code here): #include <array> int main() { constexpr std::array<int,3> v{1,2,3}; constexpr auto b = v.begin(); // error: not a constexpr return 0; } with the error error: '(std::array<int, 3>::const_pointer)(& v.std::array<int,3>::_M_elems)' is not a constant expression (constexpr auto b = v.begin();) According to en.cppreference.com, the begin() member function is declared constexpr . Is this

Multiple Member Constexpr Struc Initialization

霸气de小男生 提交于 2021-01-29 19:17:49
问题 I am new to constexpr, however, I believe that problem I am practicing is a good fit compile-time calculations. This is not for homework or anything, just practicing "Modern" C++. Here is what I have so far: Header (prime_app_lib.hpp): // #pragma once for modern header files (.hpp) #pragma once #include <math.h> static constexpr unsigned max_prime = 10000U; // check out this link for the number of primes: https://primes.utm.edu/howmany.html static constexpr unsigned prime_array_size = 1230U;

Converting a function's parameter - signature from using an `std::function<T>` to a template parameter type

会有一股神秘感。 提交于 2021-01-29 16:36:12
问题 In my existing codebase, I have a non-template class and its constructor has the following declaration signature... struct SomeStruct { double a_; double b_; SomeStruct(double a, double b) : a_{a}, b_{b} {} } class SomeClass { private: SomeStruct fields_; size_t n_; std::function<double(double)> func_; public: SomeClass(SomeStruct fields, size_t n, std::function<double(double)> func) : fields_{fields}, n_{n}, func_{func} {} }; I was using it like this: constexpr double funcA(double x) {

Can I make expressions constexpr?

牧云@^-^@ 提交于 2021-01-28 11:16:46
问题 I recently wrote some code which prints a function result to cout . The result could have been evaluated at compile time, but it wasn't: #include <algorithm> #include <iostream> constexpr unsigned int gcd(unsigned int u, unsigned int v) { // ... } int main() { std::cout << gcd(5, 3) << std::endl; } For whatever bizarre reason, this compiles to: ( clang -O3 -std=c++17 ) main: push r14 push rbx push rax mov edi, 5 mov esi, 3 call gcd(unsigned int, unsigned int) mov esi, eax ... See Compiler

Validation of an std::initializer_list in constexpr context

一笑奈何 提交于 2021-01-28 09:40:05
问题 I have some class that I would like to be initialized at compile time by an initializer list that needs some level of validation. I first tried static_assert but that wouldn't compile with the error "non-constant condition for static assertion" What is the best way to causing a build error with this? class foo { public: constexpr foo(std::initializer_list<bar> items) { for(auto &&i: items) { if(i == 12) // example validation logic // fail the build } } } constexpr foo foo_list({0,1,2,3,4,5});

How to define a pointer pointing to a constexpr variable?

会有一股神秘感。 提交于 2021-01-27 19:09:34
问题 In C++ Primer 5th, it says that constexpr imposes a top-level const on the objects it defines. So how I can I declare a pointer with a constexpr specifier imposing a low-level const, i.e. a pointer pointing to a constexpr object? 回答1: A constexpr object is an object just like any other. The fact that its value is computed at compile time does not alter this. Often, the compiler will seek to avoid actually emitting code to create const values and objects if it knows that they will never be