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
11
votes
3 answers

How to define a const double inside a class's header file?

Inside the header file of my class, I am trying the following and getting compiler complaints: private: static const double some_double= 1.0; How are you supposed to actually do this?
zebra
  • 6,373
  • 20
  • 58
  • 67
11
votes
3 answers

Is there a way to test at compile-time that a constant is a compile-time constant?

Given how difficult it is to know whether an arithmetic final val expression will be compiled to a compile-time constant, and how easy it is to accidentally break compile-time-ness... Can anyone think of an easy way to verify, at compile-time, that…
Ed Staub
  • 15,480
  • 3
  • 61
  • 91
11
votes
2 answers

Compile-time array constants

I seem to be missing something rather fundamental. I'm trying to use const array members at compile-time. const int list[3] = { 2, 5, 7 }; const int a = list[2]; // this doesn't error? template struct tmax { enum { value = ((N1 >…
std''OrgnlDave
  • 3,912
  • 1
  • 25
  • 34
10
votes
2 answers

In C++ 11 Is there any way to branch by if a function parameter is compile-time constant or not?

Is there anyway I can tell function argument is compile time-constant or not in C++ 11?. I would like to branch the functions body based on the results. For example, like below; template size_t get_length(const T &t) { return…
lighthouse
  • 413
  • 2
  • 10
10
votes
1 answer

How to create compile-time constant in Kotlin from enum?

I have an annotation that requires defaultValue to be compile-time constant. I take defaultValue from enum below: enum class RaceType { MARATHON, SPRINT; companion object { fun apply(type: RaceType): RaceDto { return…
10
votes
1 answer

constexpr c string concatenation, parameters used in a constexpr context

I was exploring how far I could take the constexpr char const* concatenation from this answer: constexpr to concatenate two or more char strings I have the following user code that shows exactly what I'm trying to do. It seems that the compiler…
Short
  • 7,767
  • 2
  • 25
  • 33
9
votes
1 answer

How do I determine the size of an array at compile time in Rust?

I have a C library that expects string type that explicitly defines the string length: #[repr(C)] pub struct FFIStr { len: usize, data: *const u8, } Because this type is used as a static, I'd like a way to safely declare it using a const…
dcoles
  • 3,785
  • 2
  • 28
  • 26
9
votes
1 answer

Why can g++ 5.4 not compile this compile-time prime number code?

#include using namespace std; template class Prime { // generate N prime numbers at compile time public: unsigned int arr[N]{}; constexpr Prime() { int k=0; for(unsigned int i=2; k
Zeta
  • 913
  • 10
  • 24
9
votes
1 answer

Top-level expression evaluation at compile time

Is there any way to ensure, that an expression like the following would be evaluated at compile time? myList :: [Int] myList = sort [3,2,0,1]
lanskey
  • 457
  • 3
  • 13
9
votes
3 answers

C++ compile-time constant detection

There're cases when a library source is available, and it has to support variable parameters in general, but in practice these parameters are commonly constants. Then it may be possible to optimize things by special handling of constant parameters…
Shelwien
  • 2,160
  • 15
  • 17
9
votes
1 answer

How can I get a generic parameter type name at compile time?

I'm trying to implement a generic class. It should have a property with an attribute that takes a compile-time constant, which I want to set as the parameter type's name. Something like this: namespace Example { public class MyGeneric { …
string QNA
  • 3,110
  • 3
  • 17
  • 14
9
votes
3 answers

Why are (constant) expressions not evaluated at compile time in Haskell?

I am currently learning Haskell, and there is one thing that baffles me: When I build a complex expression (whose computation will take some time) and this expression is constant (meaning it is build only of known, hard coded values), the expression…
8
votes
1 answer

What's the difference between $?CLASS and ::?CLASS

The Raku docs describe ::?CLASS as a compile-time variable that answers "Which class am I in?". Then, a couple of paragraphs later, it mentions $?CLASS, and says that it answers "Which class am I in? (as variable)". What's the difference between…
codesections
  • 8,900
  • 16
  • 50
8
votes
5 answers

Hexadecimal constant in C is unsigned even though L suffix

I know this is a simple question but I'm confused. I have a fairly typical gcc warning that's usually easy to fix: warning: comparison between signed and unsigned integer expressions Whenever I have a hexadecimal constant with the most significant…
test
  • 411
  • 1
  • 4
  • 10
8
votes
1 answer

Compile time text to number translation (atoi)

I want to implement atoi() function at compile time (in C++ language, by using C++11 or C++14 standard). So it should be able to parse text enclosed in double quotes as number, or repor an error. More specifically, it is a part of bigger system,…
1 2
3
19 20