Questions tagged [compile-time-constant]

Use this tag for questions related to the compile time constant, a constant value that is known at compile time.

A expression is an expression denoting a value of primitive type or a String that is composed using only the following:

is uses in its general meaning, but you should provide the relevant tag of your programming environment, if any.

300 questions
21
votes
1 answer

Why is std::make_tuple(7 + N...) legal in C++11?

The following code is legal in C++11. template std::tuple f() { return std::make_tuple(7 + N...); } What does it mean?
xmllmx
  • 39,765
  • 26
  • 162
  • 323
21
votes
1 answer

Do C/C++ compilers such as GCC generally optimize modulo by a constant power of 2?

Let's say I have something like: #define SIZE 32 /* ... */ unsigned x; /* ... */ x %= SIZE; Would the x % 32 generally be reduced to x & 31 by most C/C++ compilers such as GCC?
Matt
  • 21,026
  • 18
  • 63
  • 115
20
votes
2 answers

Can I get a class's name as a compile-time constant without hardcoding it in a string literal?

I am working on an annotation processor. This code compiles: package sand; import java.util.Set; import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.RoundEnvironment; import…
Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62
18
votes
6 answers

Defining colors as constants in C#

I've set up some default colors in a C# winforms application like so: readonly Color ERROR = Color.Red; readonly Color WARNING = Color.Orange; readonly Color OK = Color.Green; As far as I am aware, readonly is essentially a constant for my…
JYelton
  • 35,664
  • 27
  • 132
  • 191
17
votes
5 answers

Is there no built-in way to compute a power at compile time in C++?

I have the following very simple template. As I learned, ^ is not the exponential operator. Now I'm looking for a way to compute this power. There are many examples with a recursive template on the internet. This is not too difficult. But I wonder:…
Michael
  • 7,407
  • 8
  • 41
  • 84
15
votes
2 answers

Legitimate to initialize an array in a constexpr constructor?

Is the following code legitimate? template class foo { public: constexpr foo() { for (int i = 0; i < N; ++i) { v_[i] = i; } } private: int v_[N]; }; constexpr foo<5> bar; Clang accepts it, but…
15
votes
5 answers

How to have a const variable in a for loop for the generation of template classes?

I have a code like template class A { template someFunctions() {}; }; Now I want to create instances of the class and call the functions in it in a for loop for a set of many values like // in main() int main() { …
NSK
  • 180
  • 2
  • 12
15
votes
3 answers

Meta programming: Declare a new struct on the fly

Is it possible to declare a new type (an empty struct , or a struct without an implementation) on the fly? E.g. constexpr auto make_new_type() -> ???; using A = decltype(make_new_type()); using B = decltype(make_new_type()); using C =…
Kijewski
  • 25,517
  • 12
  • 101
  • 143
15
votes
1 answer

"Constant expressions" prior to C++11

The constexpr keyword was introduced in C++11, as (I think) was the corresponding idea of "constant expressions." However, this concept was implicitly present in C++98/c++03, since array declarations require a constant expression: // valid: int…
Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
15
votes
1 answer

Declare and initialize constant in header file

I'm well versed in the typical paradigm of: //.h extern const int myInt; //.c, .m, .cpp, what have you const int myInt = 55; But there's got to be a way to put that into .h files for use with libraries or other instances where you cannot access…
Patrick Perini
  • 22,555
  • 12
  • 59
  • 88
14
votes
1 answer

Does "int size = 10;" yield a constant expression?

The following code compiles under gcc 4.8 and Clang 3.2: int main() { int size = 10; int arr[size]; } 8.3.4/1 of the C++ Standard says that the size of an array must be an integral constant expression, which size does not seem to be. Is this a…
14
votes
2 answers

What does it mean to say that int enum patterns are compile-time constants?

This is from Effective Java Programs that use the int enum pattern are brittle. Because int enums are compile-time constants, they are compiled into the clients that use them. Can some one explain why the int enum pattern is called compiled…
Geek
  • 26,489
  • 43
  • 149
  • 227
14
votes
2 answers

c++ template specialization based on compile time value

I'm feeling my way into template meta-programming, slowly and I'm not sure how to implement the following: // hpp file enum MyEnum { Alive = 0, Dead }; class A { public: template static int…
Short
  • 7,767
  • 2
  • 25
  • 33
12
votes
2 answers

Why doesn't a Java constant divided by zero produce compile time error?

Possible Duplicate: Is 1/0 a legal Java expression? Why does this code compile? class Compiles { public final static int A = 7/0; public final static int B = 10*3; public static void main(String[] args) {} } If I take a look in the…
12
votes
10 answers

Can I compute pow(10,x) at compile-time in c?

Is it possible to compute pow(10,x) at compile time? I've got a processor without floating point support and slow integer division. I'm trying to perform as many calculations as possible at compile time. I can dramatically speed up one…
AShelly
  • 34,686
  • 15
  • 91
  • 152
1
2
3
19 20