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
6
votes
0 answers

When is declaring a local variable as non-static constexpr beneficial/required?

Despite reading through some StackOverflow posts (this and this) and cppreference pages, I cannot figure out how a non-static constexpr local variable would be beneficial compared to a static constexpr one. The only difference I can see is that each…
Reizo
  • 1,374
  • 12
  • 17
6
votes
1 answer

How do I output a compile-time numeric constant during compilation in Visual C++?

Visual C++ has #pragma message that outputs a string into compiler output. Now I have a factory: template CComPtr CreateComObject() { CComPtr newObject( new CComObject ); //do some tuning to the object return…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
6
votes
1 answer

Why does std::is_copy_constructible not behave as expected?

#include int main() { std::is_constructible_v; // false, as expected. std::is_copy_constructible_v; // true, NOT as expected! } According to cppref: If T is an object or reference type and the…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
6
votes
3 answers

How to define CUDA device constant like a C++ const/constexpr?

In a .cu file I've tried the following in the global scope (i.e. not in a function): __device__ static const double cdInf = HUGE_VAL / 4; And got nvcc error: error : dynamic initialization is not supported for __device__, __constant__ and…
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
6
votes
3 answers

Different behavior of compilers with array allocation

I recently found a interesting behaviour of g++ when compared with MSVC++ 2008. Consider this tiny program: #include const int ARR_LENGTH = 512; void doSomething( int iLen ); int main( int argc, char** argv ) { doSomething(…
PeterK
  • 6,287
  • 5
  • 50
  • 86
6
votes
1 answer

A function that accepts only compile time known expressions?

Compile time expressions are good because you can use them to specialize templates. So for example, tuples can be accessed by using a compile time expression with the std::get method. std::cout << std::get<0>(my_tuple) << std::endl; Now, the above…
Matteo Monti
  • 8,362
  • 19
  • 68
  • 114
6
votes
2 answers

Confusion about pointer values being compile-time constatns

In C++, it is possible for pointer values to be compile-time constants. This is true, otherwise, non-type template parameters and constexpr won't work with pointers. However, as far as I know, addresses of functions and objects of static storage are…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
6
votes
1 answer

Basic compile time format string checking using constexpr

In our project we use a printf compatible function to add messages to an external log file. e.g. We can write __LOG_INFO( "number of files = %d\n", number_of_files ); __LOG_INFO( "Just for information\n" ); The function declarations of __LOG_INFO…
aerofly
  • 61
  • 1
  • 2
6
votes
3 answers

Using compiler constants in build events

Is there anyway to use compiler constants in Build Events in Visual Studio - VB.NET? (especially in Post-Build events) Scenario If TEST_EDITION=TRUE is defined I want to run an executable during the Post-Build event so if it's FALSE then I'll run…
dr. evil
  • 26,944
  • 33
  • 131
  • 201
6
votes
2 answers

Scala: Compile time constants

How do you declare compile time constants in Scala? In C# if you declare const int myConst = 5 * 5; myConst is in-lined as the literal 25. Is: final val myConst = 5 * 5 equivalent or is there some other mechanism/ syntax?
Rich Oliver
  • 6,001
  • 4
  • 34
  • 57
5
votes
2 answers

Can the conditional operator ( ? : ) in C++ be compile time?

Can the ternary (conditional) operator be used as an analogous to constexpr if(), introduced in C++17? I would like to add some conditionality to member variables initialization in a template. Would the following expression resolve at compile time…
5
votes
4 answers

Why can't I resolve a constant expression after increasing -fconstexpr-steps?

Take the following constexpr example: #include constexpr int fib(const int i) { if (i == 0) return 0; if (i == 1) return 1; return fib(i-1) + fib(i-2); } int main(){ std::cout << fib(45) << '\n'; } Despite being constexpr, it…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
5
votes
1 answer

Will const and constexpr eventually be the same thing?

I just read the answer to const vs constexpr on variables and am watching this Google Tech Talk about C++11/14 features , in which it is said that, well, constexpr might not be necessary in the future when it comes to functions, since compilers…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
5
votes
2 answers

Compile time checking of constness

If I have a function int calcStuff_dynamic(const int a, const int b) and some template meta code template struct calcStuff_static { static const int value = //some more code }; Is there a way to write a wrapper int…
5
votes
1 answer

How to extract a value from a variadic template parameter pack by index?

I want to write a function magic_get, which can extract a value from a parameter pack by index, for example: int n = 0; n = magic_get<0>(1, 3, 5, 7); assert(1 == n); n = magic_get<1>(1, 3, 5, 7); assert(3 == n); n = magic_get<2>(1, 3, 5,…
xmllmx
  • 39,765
  • 26
  • 162
  • 323