compile-time-constant

How to declare a constant Guid in C#?

霸气de小男生 提交于 2019-12-03 23:45:06
问题 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 ? 回答1: 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. 回答2: Declare it as static readonly Guid rather than const Guid 回答3: public static readonly Guid Users = new Guid("5C60F693-BEF5-E011-A485-80EE7300C695");

Defining colors as constants in C#

只谈情不闲聊 提交于 2019-12-03 23:41:22
问题 I've set up some default colors in a C# winforms application like so: readonly Color ERROR = Color.Red; readonly Color WARNING = Color.Orange; readonly Color OK = Color.Green; As far as I am aware, readonly is essentially a constant for my purposes. If I attempt to define these as constants, the compiler indicates that it must be a compile-time constant, which Color is not. Am I good leaving these as-is, or is there some way to define these constants that I should be aware of? (The purpose is

M_PI flagged as undeclared identifier

橙三吉。 提交于 2019-12-03 18:47:10
问题 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; }

Constexpr variable evaluation

北城以北 提交于 2019-12-03 17:06:14
Here is my code and I need clarification on what's happening: constexpr int funct(int x){ return x + 1; } int main(){ int x = funct(10); return 0; } constexpr 's allows compile time calculation, and based on my code above, since funct is declared as constexpr , it is allowed to do compile time calculations if the arguments are constants or constexpr's themselves. The part that I am confused with lies in this part, the int x . Since it is not declared as constexpr , would it mean that int x will get the value at runtime? Does that mean that declaring it to be constexpr int x will mean that int

Dividing by zero in a constant expression

大城市里の小女人 提交于 2019-12-02 20:01:49
My toy compiler crashes if I divide by zero in a constant expression: int x = 1 / 0; Is this behaviour allowed by the C and/or C++ standards? The mere presence of 1 / 0 does not permit the compiler to crash. At most, it is permitted to assume that the expression will never be evaluated, and thus, that execution will never reach the given line. If the expression is guaranteed to be evaluated, the standard imposes no requirements on the program or compiler. Then the compiler can crash. 1 / 0 is only UB if evaluated. The C11 standard gives an explicit example of 1 / 0 being defined behavior when

Different behavior of compilers with array allocation

眉间皱痕 提交于 2019-12-01 17:31:21
I recently found a interesting behaviour of g++ when compared with MSVC++ 2008. Consider this tiny program: #include <cstdlib> const int ARR_LENGTH = 512; void doSomething( int iLen ); int main( int argc, char** argv ) { doSomething( ARR_LENGTH ); return 0; } void doSomething( int iLen ) { int iTest[iLen]; return; } Will it compile? What do you think? According to my knowledge of C (or C++ for that matter), this should NOT compile, since i can call the function doSomething() with any integer i want, so the size of iTest array cannot be determined at compile time. However, when i try to compile

Different behavior of compilers with array allocation

非 Y 不嫁゛ 提交于 2019-12-01 17:05:28
问题 I recently found a interesting behaviour of g++ when compared with MSVC++ 2008. Consider this tiny program: #include <cstdlib> const int ARR_LENGTH = 512; void doSomething( int iLen ); int main( int argc, char** argv ) { doSomething( ARR_LENGTH ); return 0; } void doSomething( int iLen ) { int iTest[iLen]; return; } Will it compile? What do you think? According to my knowledge of C (or C++ for that matter), this should NOT compile, since i can call the function doSomething() with any integer

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

允我心安 提交于 2019-12-01 05:38:46
Visual C++ has #pragma message that outputs a string into compiler output . Now I have a factory: template<class Type> CComPtr<Type> CreateComObject() { CComPtr<Type> newObject( new CComObject<Type> ); //do some tuning to the object return newObject; } and I want to output the size of class that is passed to new (namely sizeof( CComObject<Type> ) into the compiler output. Looks like #pragma message only accepts strings. How can I output a compile-time numeric constant? Nawaz If I understood your question correctly, then I think you can do this: template<size_t size> struct overflow{ operator

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

萝らか妹 提交于 2019-12-01 02:47:39
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; Suppose you have two files: Foo.java: public class Foo { public static final int SOMETHING = 1; } Bar.java: public class Bar { public static void main(String[] args) { System.out.println(Foo.SOMETHING); } } Compile

Defining colors as constants in C#

房东的猫 提交于 2019-12-01 02:29:46
I've set up some default colors in a C# winforms application like so: readonly Color ERROR = Color.Red; readonly Color WARNING = Color.Orange; readonly Color OK = Color.Green; As far as I am aware, readonly is essentially a constant for my purposes. If I attempt to define these as constants, the compiler indicates that it must be a compile-time constant, which Color is not. Am I good leaving these as-is, or is there some way to define these constants that I should be aware of? (The purpose is simply to have a single location in which to change all of the colors for logging purposes.) Only