compile-time-constant

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

▼魔方 西西 提交于 2019-12-11 17:30:06
问题 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) => println!("Error ({})", e), }; However, I wanted to do it in a no_std context, therefore, I cannot use std::env to access the environment. Is it possible to do this? 回答1: env::var does not get evaluated at compile time: Fetches the environment variable key from the

Error 1046:Type was not found or was not a compile-time constant

橙三吉。 提交于 2019-12-11 02:29:23
问题 I'm trying to make a interactive flash video in CS6 for a class I am taking. I briefly talked with the professor about this and he could not figure out the issue either. The weird thing is it says the errors are on lines 2 and 3. When I remove the code on those lines it still says the error is on those lines. Take a look at my AS and tell me what you think. import flash.events.MouseEvent; import flash.display.MovieClip; import flash.display.Stage; import flash.events.*; public class Essay1

How to protect enum assignment

社会主义新天地 提交于 2019-12-10 19:20:59
问题 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 example_function(void) { enum example_enum the_enum = ENUM_VAL3; // correct the_enum = 41; // will work the_enum = 43; // also will work bar(the_enum); // this function assumes that input parameter is correct } Is there easy, efficient way to check if assignment to enum is correct? I could test value by

Will const and constexpr eventually be the same thing?

人盡茶涼 提交于 2019-12-10 15:24:38
问题 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 will evolve to figure it out on their own. Finally, I know that Java compilers and JVMs work hard to figure out that classes (or any variable maybe) are immutable after construction - without you explicitly saying so - and doing all sorts of wicked

A function that accepts only compile time known expressions?

放肆的年华 提交于 2019-12-10 13:43:32
问题 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 expression is pretty ugly. I am trying to develop some sort of tuples myself (hoping to make it to turn them into compile time dictionaries), so that, say, they expose a method in the form: my_dict.get<0>(); Now, what I would like to do is to

Is it possible to have a recursive function computed at compile-time in Rust?

人走茶凉 提交于 2019-12-10 13:31:26
问题 I want to compute the factorial of a const . I want to end up with something like this: const N: usize = 4; const N_PERMUTATIONS = factorial(N); The obvious solutions (that don't work) are: const fn – conditional statements are not allowed (or at least not implemented) in const fn , neither of the below will compile: const fn factorial(n: usize) -> usize { match n { 0 => 1, _ => n * factorial(n-1) } } const fn factorial(n: usize) -> usize { if n == 0 { 1 } else { n * factorial(n-1) } } macros

Why must default method parameters be compile-time constants in C# [closed]

一笑奈何 提交于 2019-12-10 02:58:46
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . EDIT 1: I know there are alternatives such as telescoping, this was a purely educational question. I know that this is true, but why must it be? It seems like with something like this: public class Foo{ private int bar; public void SetBar(int baz = ThatOtherClass.GetBaz(3)){

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

╄→гoц情女王★ 提交于 2019-12-09 02:33:07
问题 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 type constant and what is meant by compiled into the clients ? Here s' an example of such a constant : public static final int APPLE_FUJI = 0; 回答1: Suppose you have two files: Foo.java: public class Foo { public static final int SOMETHING = 1; } Bar.java:

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

我是研究僧i 提交于 2019-12-07 07:29:00
问题 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, 7); assert(5 == n); n = magic_get<3>(1, 3, 5, 7); assert(7 == n); How to implement magic_get ? 回答1: template <size_t N, typename... Args> decltype(auto) magic_get(Args&&... as) noexcept { return std::get<N>(std::forward_as_tuple(std::forward<Args>(as)...));

Top-level expression evaluation at compile time

和自甴很熟 提交于 2019-12-06 23:12:48
问题 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] 回答1: If what you're evaluating is an instance of Lift, you can evaluate it at compile time using TemplateHaskell: {-# LANGUAGE TemplateHaskell #-} module Sort where import Data.List import Language.Haskell.TH.Syntax myList :: [Int] myList = $(lift (sort [3,2,0,1] :: [Int])) If you want, you can check what it has compiled to with -ddump-splices : $ ghc