compile-time-constant

How to have a const variable in a for loop for the generation of template classes?

萝らか妹 提交于 2019-12-21 03:32:44
问题 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 for a set of many values like // in main() int main() { for (int i = 1; i <= 100; i++) { const int N = i; // dont know how to do this A<N> a; a.functionCalls(); } } How to do this? Hoping for a method to do this. 回答1: This would require something called a template for which is the expected form expansion statements

Dividing by zero in a constant expression

拜拜、爱过 提交于 2019-12-20 09:46:56
问题 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? 回答1: 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 /

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

自古美人都是妖i 提交于 2019-12-19 07:07:10
问题 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? 回答1: If I understood

Why isn't a final variable always a constant expression?

[亡魂溺海] 提交于 2019-12-18 12:04:32
问题 In the below code: final int a; a=2; byte b=a; // error: possible loss of precision Why do I get this error? Isn't a final variable compile time constant expression and hence implicitly narrowed to byte during the assignment? In other words isn't the above code equivalent to: final int a=2; byte b=a; 回答1: The compiler isn't that smart. We can tell that the value will always be 2. But what if we had something like this? class ABC{ final int a; public ABC(){ if(Math.random() < .5){ a = 2; }

Why isn't a final variable always a constant expression?

。_饼干妹妹 提交于 2019-12-18 12:04:10
问题 In the below code: final int a; a=2; byte b=a; // error: possible loss of precision Why do I get this error? Isn't a final variable compile time constant expression and hence implicitly narrowed to byte during the assignment? In other words isn't the above code equivalent to: final int a=2; byte b=a; 回答1: The compiler isn't that smart. We can tell that the value will always be 2. But what if we had something like this? class ABC{ final int a; public ABC(){ if(Math.random() < .5){ a = 2; }

Constant expression contains invalid operations [duplicate]

自古美人都是妖i 提交于 2019-12-17 06:48:51
问题 This question already has answers here : PHP Error : Fatal error: Constant expression contains invalid operations (4 answers) Closed 10 months ago . 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() { } } I have

Why isn't std::string::max_size a compile-time constant?

▼魔方 西西 提交于 2019-12-13 12:23:56
问题 std::string provides a max_size() method to determine the maximum number of elements it can contain. However, to work out the maximum length of a string in general, the programmer has to create a (possibly empty) string object. If this class doesn't need any information from the programmer, why isn't max_size() available as a compile-time constant ? Is there some kind of runtime information necessary for a string to work out its maximum size ? 回答1: One reason is that the max_size function isn

Why doesn't Debug.WriteLine work without the DEBUG constant?

落爺英雄遲暮 提交于 2019-12-13 08:46:31
问题 I ran into an issue where someone had apparently disabled the DEBUG and TRACE constants on a C# .NET project I was working on, so my calls to Debug.WriteLine were having no effect. (No debug output was shown in the output.) After re-enabling them as described here, I started seeing my output. Knowing how to fix it is helpful, but my question is why ? As far as I understand, DEBUG is a compile time constant , and the Debug class is already compiled when I build my project. So how are my calls

Is this a BUG of VC++ 2010? About declaring a constant object in a header

只愿长相守 提交于 2019-12-13 06:44:10
问题 Several lines of code are worth a thousand words: I have three simple files: header.h, main.cpp, other.cpp ==== CODE BEGIN ==== // header.h #pragma once const void* p = 0; // main.cpp #include "header.h" int main() { return 0; } // other.cpp #include "header.h" ==== CODE END ==== When compiling the simplest project, the VC++ 2010 complains as follows: ClCompile: other.cpp main.cpp Generating Code... other.obj : error LNK2005: "void const * const p" (?p@@3PBXB) already defined in main.obj D:

Cannot create list literal in F#

懵懂的女人 提交于 2019-12-12 12:02:05
问题 I have the following types type StatusCode = | OK = 200 | NoContent = 204 | MovedTemp = 301 | MovedPerm = 302 | SeeOther = 303 | NotModified = 304 | NotFound = 404 | ServerError = 500 [<Literal>] let NoBodyAllowedStatusCodes = [StatusCode.NoContent; StatusCode.NotModified] And I'm getting a compile-time error that says: This is not a valid constant expression or custom attribute value I can't really figure out what's wrong here. 回答1: In F#, and .NET in general, lists cannot be literals