compile-time-constant Questions

10

Related: Function returning constexpr does not compile I feel like constexpr is limited in usefulness in C++11 because of the inability to define two functions that would otherwise have the same s...
Caruso asked 20/1, 2012 at 3:54

5

Solved

I have a user-defined literal operator that only makes sense for strings of a specific length, like this: constexpr uint16_t operator "" _int(const char* s, std::size_t len) { return len == 2 ? s...

2

Solved

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 <T> ...
Burin asked 12/7, 2023 at 17:9

6

Solved

Is it possible to declare a constant Guid in C#? I understand that I can declare a static readonly Guid, but is there a syntax that allows me to write const Guid?
Horticulture asked 7/2, 2011 at 21:5

1

Solved

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 "Whi...
Nidorf asked 23/2, 2022 at 18:5

2

Solved

Is the following code legitimate? template <int N> class foo { public: constexpr foo() { for (int i = 0; i < N; ++i) { v_[i] = i; } } private: int v_[N]; }; constexpr foo<5&gt...

14

Solved

So, I am working on this class that has a few static constants: public abstract class Foo { ... public static final int BAR; public static final int BAZ; public static final int BAM; ... } ...
Flinty asked 30/9, 2010 at 3:2

1

Solved

I have been trying to understand how static variables are initialized. And noted a contradiction about the order of constant initialization and zero initialization at cppref and enseignement. At cp...

1

Solved

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]; } ...
Refugia asked 8/9, 2021 at 19:55

3

Solved

I want to compute the factorial of a const: const N: usize = 4; const N_PERMUTATIONS = factorial(N); The solutions I've thought of that don't work in Rust 1.18 are: const fn — conditional stateme...

0

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 const...
Woodham asked 9/7, 2020 at 19:11

8

Solved

The Java language documentation says: If a primitive type or a string is defined as a constant and the value is known at compile time, the compiler replaces the constant name everywhere in the...

2

Solved

#include <iostream> #include <cstdint> using namespace std; static_assert(-1 == numeric_limits<uint64_t>::max()); // ok static_assert(-1 == numeric_limits<uint32_t>::max()...

2

Solved

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. Woul...

1

Solved

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: Ra...
Impending asked 20/1, 2020 at 16:13

1

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. M...
Pyroconductivity asked 30/12, 2019 at 18:26

5

I have a code like template <size_t N> class A { template <size_t N> someFunctions() {}; }; Now I want to create instances of the class and call the functions in it in a for loop ...

5

Solved

I have a Perl module that I have declared some constants: use constant BASE_PATH => "/data/monitor/"; In live operation the constant will never change but I wish to be able to modify it in my...

1

Solved

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 ...
Soldo asked 27/10, 2019 at 5:26

3

Solved

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()); usi...

1

Solved

#include <type_traits> int main() { std::is_constructible_v<int&, const int&>; // false, as expected. std::is_copy_constructible_v<int&>; // true, NOT as expected! ...
Woaded asked 1/2, 2019 at 7:19

2

Solved

I understood what a compile time constant rule is from Compile-time constants and variables. declared as final have a primitive or String type initialized at the same time as the declaration init...
Translatable asked 22/12, 2018 at 16:57

1

Solved

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...
Kaylil asked 31/7, 2018 at 19:32

3

Solved

I have the following code, where I get the error "PHP Fatal Error: Constant expression contains invalid operations". It works fine when I define the variable in the constructor. I am using La...
Wolfsbane asked 27/11, 2016 at 10:24

3

Solved

I want to prevent invalid value enum assignment. I know if i even assign value that is not in enum it will work. Example: enum example_enum { ENUM_VAL0, ENUM_VAL1, ENUM_VAL2, ENUM_VAL3 }; void...
Wohlen asked 27/9, 2016 at 12:58

© 2022 - 2024 — McMap. All rights reserved.