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

Help with type traits

Suppose we have the following template class template class Wrap { /* ... */ }; We can not change Wrap. It is important. Let there are classes derived from Wrap. For example, class NewInt : public Wrap { /* ... */ }; class…
5
votes
1 answer

What are the differences between 0, int() and int{}?

As int() and int{} are constant expressions of value equal to 0, I thought they are equivalent and interchangeable, thus compilers must treat them equally. For example, int a[0]; //error: zero-sized array not allowed in ISO C++ int b[int()]; …
Nawaz
  • 353,942
  • 115
  • 666
  • 851
4
votes
1 answer

How does the C# visual studio compiler treat struct/NULL comparisons?

We just ran across some bad code like this in our c#.net 4 codebase DateTime myDate = someValue; If (myDate==Null) Do Something It occurred to us that this condition will never occur. How does the compiler handle these non-nullable struct…
Matthew
  • 10,244
  • 5
  • 49
  • 104
4
votes
2 answers

constexpr general confusion

I have made a thread yesterday but I think that it was unclear and the replies I got didn't solve my confusion at all. So, I'll try to make the example simpler. Why is this allowed: constexpr int incr(int k1) { return k1 + 5; } constexpr int…
4
votes
2 answers

Common constants for an AVR / Linux GCC C++ project

I'm creating software for an Linux + AVR Arduino project. Obviously the whole work is split in several projects in Eclipse (I'm not using Arduino IDE). I'd like to use common, mostly string, constants for all those projects. I also have to spare…
mmm
  • 1,277
  • 5
  • 18
  • 34
4
votes
2 answers

Why can't C (nor C++) optimize this recursive experiment?

I was thinking about functional programming techniques, recursions, and constness, and linked lists, so I made two experiments to test my compiler. In theory the compiler should be capable of optimizing away tail recursion (I now realize the…
Dmytro
  • 5,068
  • 4
  • 39
  • 50
4
votes
1 answer

Interpolate string in const function

Im trying to make a function similar to this pub const fn insert(num1:i32, num2:i32) -> &'static str { formatcp!("n1:{}, n2:{}" , num1, num2) } But num1/num2 are not const. I think this would be possible as a macro but im not experienced in…
4
votes
1 answer

Why is `CMSG_SPACE` not a constant function in Rust?

According to the documentation: pub const unsafe extern "C" fn CMSG_SPACE(length: c_uint) -> c_uint However, if I compile fn main() { let _ = [0u8; libc::CMSG_SPACE(1) as usize]; } I get the following error: error[E0015]: calls in constants…
beroal
  • 369
  • 4
  • 14
4
votes
2 answers

Something like std::integral_constant but with auto template argument in std C++20 library?

Starting from C++20 one can use auto template argument to implement integral constant: Try it online! template struct integral_constant2 : std::integral_constant {}; which can be used instead of more verbose…
Arty
  • 14,883
  • 6
  • 36
  • 69
4
votes
0 answers

Numba - compile time constants optimizations

I have artificial example code JIT-ed using Numba: import numba @numba.njit def f(x, c): for i in range(3): if c == 0: x += 1 elif c == 1: x *= 2 else: assert False return…
Arty
  • 14,883
  • 6
  • 36
  • 69
4
votes
2 answers

C++ Template specialization for constant variables

I am currently trying to have a compile-time constant variable that is template specialized for several types. Currently, I am using constant expressions such as the following generalized example: template constexpr T GENERAL_CONSTANT =…
4
votes
2 answers

Where to define class constants in C++?

In C++, to have a tidier code, I want to declare a set of values as constants in my header file, example : constexpr float SIZE_SMALL = 1.5f; constexpr float SIZE_MEDIUM = 2.5f; constexpr std::string COLOR_RED = "RED"; constexpr std::string…
Fakher Mokadem
  • 1,059
  • 1
  • 8
  • 22
4
votes
1 answer

Is it possible to initialize a variable from an environment variable at compilation time?

I would like to initialize a variable during compilation time. For example, I would like to initialize the variable VAR to VALUE when compiling the code: match env::var("VAR") { Ok(value) => println!("Ok {}", value), Err(e) =>…
Dragonight
  • 1,033
  • 2
  • 10
  • 20
4
votes
1 answer

C++: name of a value may have linkage

Extracted from cppreference.com: A name that denotes object, reference, function, type, template, namespace, or value, may have linkage. What is "the name of a value"? Can anybody provide an example where a name of a value is not a name of an…
ABu
  • 10,423
  • 6
  • 52
  • 103
4
votes
1 answer

C# compiler constant on a PER-FILE basis?

Is it possible to define a compiler constant on a PER-FILE/project-item basis ? Background: I want to achieve a Database Abstraction Layer (DAL), that separates all read, and write tasks, but retain a DAL that can do both, but without implementing…
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442