compile-time-constant

How to declare a constant Guid in C#?

匆匆过客 提交于 2019-12-01 02:02:40
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 ? No. The const modifier only applies to "primitive" types (bool, int, float, double, long, decimal, short, byte) and strings. Basically anything you can declare as a literal. Declare it as static readonly Guid rather than const Guid public static readonly Guid Users = new Guid("5C60F693-BEF5-E011-A485-80EE7300C695"); and that's that. While you can't seem to do that you can do that to be parsed whenever you need it: const string

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

≡放荡痞女 提交于 2019-11-30 12:02:13
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? In C++11, you can have non-integral constant expressions thanks to constexpr : private: static constexpr double some_double = 1.0; Declare it in the header, and initialize it in one compilation unit (the .cpp for the class is sensible). //my_class.hpp private: static const double some_double; //my_class.cpp const double my_class::some_double = 1.0; I've worked around this issue by doing this: //my_class.hpp const

M_PI flagged as undeclared identifier

假装没事ソ 提交于 2019-11-30 02:59:58
When I compile the code below, I got these error messages: (Error 1 error C2065: 'M_PI' : undeclared identifier 2 IntelliSense: identifier "M_PI" is undefined) What is this? #include <iostream> #include <math.h> using namespace std; double my_sqrt1( double n );`enter code here` int main() { double k[5] = {-100, -10, -1, 10, 100}; int i; for ( i = 0; i < 5; i++ ) { double val = M_PI * pow( 10.0, k[i] ); cout << "n: " << val << "\tmysqrt: " << my_sqrt1(val) << "\tsqrt: " << sqrt(val) << endl; } return 0; } double my_sqrt1( double n ) { int i; double x = 1; for ( i = 0; i < 10; i++ ) { x = ( x +

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

梦想与她 提交于 2019-11-29 18:04:36
问题 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? 回答1: In C++11, you can have non-integral constant expressions thanks to constexpr : private: static constexpr double some_double = 1.0; 回答2: Declare it in the header, and initialize it in one compilation unit (the .cpp for the class is sensible). //my_class.hpp private: static const double some_double; //my

“Constant expressions” prior to C++11

自闭症网瘾萝莉.ら 提交于 2019-11-29 06:01:06
The constexpr keyword was introduced in C++11, as (I think) was the corresponding idea of "constant expressions." However, this concept was implicitly present in C++98/c++03, since array declarations require a constant expression: // valid: int a[sizeof(int)]; int b[3+7]; int c[13/4]; const int n = 3; int d[n]; // invalid: int m = 4; int e[m]; There are other "constant expressions", i.e., expressions that can be (and/or must be) evaluated at compile-time; one example is template arguments. For pre-C++11, do the following exist, either in the C++98/03 standards or elsewhere? A complete list of

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

て烟熏妆下的殇ゞ 提交于 2019-11-28 11:11:47
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 is not evaluated at compile time. Comming from a C/C++ background I am used to such kind of optimization. What is the reason to NOT perform such optimization (by default) in Haskell / GHC ? What are the advantages, if any? data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show, Read, Eq) elementToTree :: a -> Tree a elementToTree x =

“Constant expressions” prior to C++11

浪尽此生 提交于 2019-11-27 23:34:11
问题 The constexpr keyword was introduced in C++11, as (I think) was the corresponding idea of "constant expressions." However, this concept was implicitly present in C++98/c++03, since array declarations require a constant expression: // valid: int a[sizeof(int)]; int b[3+7]; int c[13/4]; const int n = 3; int d[n]; // invalid: int m = 4; int e[m]; There are other "constant expressions", i.e., expressions that can be (and/or must be) evaluated at compile-time; one example is template arguments.

Constant expression contains invalid operations [duplicate]

无人久伴 提交于 2019-11-27 08:53:35
This question already has an answer here: PHP Error : Fatal error: Constant expression contains invalid operations 4 answers 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 Laravel framework. <?php namespace App; class Amazon { protected $serviceURL = config('api.amazon.service_url'); public function __construct() { } } As described here Class member variables are called "properties". You may also see them referred to using other terms such as "attributes

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

跟風遠走 提交于 2019-11-27 05:59:37
问题 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 is not evaluated at compile time. Comming from a C/C++ background I am used to such kind of optimization. What is the reason to NOT perform such optimization (by default) in Haskell / GHC ? What are the advantages, if any? data Tree a = EmptyTree

C++ compile-time constant detection

旧城冷巷雨未停 提交于 2019-11-27 03:25:30
问题 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 (eg. use static arrays instead of heap allocation), but for that its necessary to determine whether something is a constant first (or maybe define some macros, but its less convenient). So here's a working implementation. Update: also here: http:/