Questions tagged [constexpr]

constexpr is a modifier introduced in C++11, which informs the compiler that the value of a function or variable is known or can be calculated at compile time. As such, it can be used as a constant in places where otherwise it couldn't be.

2435 questions
2
votes
1 answer

Behavior of a constexpr variable inside a function

I'm wondering what happen internally when I define a constexpr variable inside a function. Is the program storing each version of the called function's constexpr variables ? Example: template template…
Cevik
  • 313
  • 1
  • 4
  • 17
2
votes
0 answers

constexpr int * vs constexpr const int * in C++ 11

I have two variables outside of any functions (having the fixed address): constexpr int j = 1; const int k = 2; Then I get their address in main: int main(){ constexpr int *p1 = &j; // ok constexpr int *p2 = &k; //error: incompatible pointer…
chenzhongpu
  • 6,193
  • 8
  • 41
  • 79
2
votes
1 answer

Which IDE in order to install and use a compiler with C++14 constexpr relaxation?

I apologize if this question is out of topic, but it's a matter of accessibility for C++14 programmers. Today i've updated Qt v5.4 on my PC (Windows 8.1/64bit/x86) and it support now the gcc compiler v4.9.2. But i've seen that constexpr relaxation…
Cevik
  • 313
  • 1
  • 4
  • 17
2
votes
1 answer

Completely enumerate indices of D-dimensional array at compile time

To test some multidimensional structures there is a need to generate compile time multidimensional indices to fully cover all the possible cases. I search for compile-time inexpensive way to achieve above purpose. What I do currently: #include…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
2
votes
2 answers

There is a way in gcc to get a warning when a constexpr can't be evaluated at compile time?

I'm using gcc 5.1.0 (c++14) and I was trying with constexpr. Is very annoying to verify if the constexpr I've implemented are evaluated at compile time. I couldn't find any flag for get a warning about that situation. Here is an example: example.cpp…
Andres Tiraboschi
  • 543
  • 1
  • 7
  • 17
2
votes
4 answers

C++ creating a 2D array using the size of a given vector, in a memory-safe manner

How do I achieve the following: std::vector vec = { 1, 2, 3 }; const int N = vec.size(); // Now create NxN 2D array. First, I know I could do it with new but I'd have to remember to delete it later, and I'd rather not have to handle…
Ray
  • 7,833
  • 13
  • 57
  • 91
2
votes
1 answer

Workaround for GCC 4.9 constexpr bug

I have the following piece of code which represents an actual bigger piece of code: #include using namespace std; template class A { public: static constexpr size_t getN() {return N;} }; template class B…
Javi
  • 3,440
  • 5
  • 29
  • 43
2
votes
2 answers

How do I turn this runtime-efficient function into a constexpr?

I'm using C++ constexpr to evaluate, at compile time, the largest term of the calculated Fibonacci sequence that this code can correctly evaluate. To do this, I use this code: constexpr unsigned long long fibo(unsigned n) { return (n < 2) ? 1 :…
Edward
  • 6,964
  • 2
  • 29
  • 55
2
votes
2 answers

Why do templates allow constexpr function members with non-constexpr constructors?

Using C++14. Why will this compile: template constexpr bool foo() { std::array arr; return true; } but not this? constexpr bool foo() { std::array arr; // Non-constexpr constructor 'array' cannot be used…
Daniel
  • 8,179
  • 6
  • 31
  • 56
2
votes
1 answer

Constexpr wrapper over C array with std::vector-like constructors

I need a constexpr wrapper over a C array or a std::array with some extra constructors (similar to std::vector constructors): template struct wrapper { T data[N]; constexpr wrapper(int s); // a constexpr wrapper(int j,…
gnzlbg
  • 7,135
  • 5
  • 53
  • 106
2
votes
2 answers

constexpr array not defined

I'm with c++11. I'm trying to initialice a multidimensional array. The first try was const static int COORDINATES[4][4][2]={{{-1,-1},{0,0},{1,1},{2,0}}, {{-1,1},{0,0},{1,-1},{0,-2}}, …
2
votes
1 answer

static constexpr function different than global?

I cannot understand why static constexpr behaves differently then global constexpr. What am I doing wrong? Compiler error is not particularly helpful: prog.cpp:20:17: error: ‘static constexpr int Bar::foo(const char*)’ called in a constant…
kpx1894
  • 381
  • 2
  • 8
2
votes
1 answer

Template varargs and explicit instantiation

I am trying to use a couple of new C++11 features together. #include #include // Trying out template varargs. template struct Test { // Using constexpr // I had assumed that with this I did not…
Martin York
  • 257,169
  • 86
  • 333
  • 562
2
votes
0 answers

Why it was decided to decorate functions with constexpr?

There is a feature in c++ for forcing compile-time executions of functions on demand, if that is possible. The feature is constexpr functions. Now at the committee they are trying (I think this is Niccolas Josuttis effort) to give guidelines on when…
Germán Diago
  • 7,473
  • 1
  • 36
  • 59
2
votes
3 answers

C++ use parameter of a constexpr function as constant for template

Since C++ does not allow template class with a value parameter template of "auto" type (you can template, or template , but you cannot really match both), I wanted to write a wrapper for a type+value. The real problem is, no one…
1 2 3
99
100