compile-time-constant

constexpr overloading

[亡魂溺海] 提交于 2020-01-08 16:02:54
问题 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 signature, but have one be constexpr and the other not constexpr. In other words, it would be very helpful if I could have, for example, a constexpr std::string constructor that takes constexpr arguments only, and a non-constexpr std::string constructor for non-constexpr arguments. Another example

Difference between final variables and compile time constant

此生再无相见时 提交于 2019-12-31 08:49:46
问题 What is the difference between final variables and compile time constants? Consider the following code final int a = 5; final int b; b=6; int x=0; switch(x) { case a: //no error case b: //compiler error } What does this mean? When and how are final variables assigned a value? What happens at run time and what happens at compile time? Why should we give switch a compile time constant? What other structures of java demands a compile time constant? 回答1: The problem is, that all case: statements

error: initializer element is not a compile-time constant

早过忘川 提交于 2019-12-24 00:52:47
问题 I have been looking for answers but could not find anything to make this code run. I get av[1] highlighted by the compiler in the main function when declaring: static char const *str = av[1]; Here is the code I tried to run with gcc: #include <stdio.h> #include <stdlib.h> char *ft_strjoin(char const *s1, char const *s2); void fct(char **av) { static char const *str = av[1]; str = ft_strjoin(av[1], av[1]); printf("%s\n", str); } int main(int ac, char **av) { fct(&av[1]); fct(&av[1]); fct(&av[1

how to initialize a large array in Fortran?

蓝咒 提交于 2019-12-23 21:17:59
问题 I have a Fortran function in which I would like to initialize a large array at compile time. A simplified working example is below, where the parameter coeff in fill_coefficients has been reduced in size greatly. How do I write similar code when coeff is large, without exceeding the maximum of 255 continuation lines, or the maximum of 132 characters per line? Here fill_coefficients should really be PURE , which probably makes it impossible to read coeff from a file once during runtime, and

Compile-time array constants

懵懂的女人 提交于 2019-12-23 07:13:12
问题 I seem to be missing something rather fundamental. I'm trying to use const array members at compile-time. const int list[3] = { 2, 5, 7 }; const int a = list[2]; // this doesn't error? template<int N1, int N2> struct tmax { enum { value = ((N1 > N2) ? N1 : N2) }; }; const int b = tmax<2,4>::value; const int c = tmax<list[0],list[1]>::value; // error is here int main() { return 0; } Errors: prog.cpp:10:24: error: 'list' cannot appear in a constant-expression prog.cpp:10:30: error: an array

constexpr does not work/apply inside function call

早过忘川 提交于 2019-12-23 03:25:16
问题 I have implemented a constexpr compile-time hash-function, which works fine (i.e. is evaluated at compile-time) if called as constexpr auto hash = CompileTimeHash( "aha" ); but I need to use it in actual code as an argument to a function as in foo( CompileTimeHash( "aha" ) ); // foo is NOT constexpr For a specific reason, I cannot use the long version constexpr auto hash = CompileTimeHash( "aha" ); foo( hash ); The compiler (VC++) will not compile-time hash in the short (first) case. Is there

constexpr does not work/apply inside function call

廉价感情. 提交于 2019-12-23 03:25:04
问题 I have implemented a constexpr compile-time hash-function, which works fine (i.e. is evaluated at compile-time) if called as constexpr auto hash = CompileTimeHash( "aha" ); but I need to use it in actual code as an argument to a function as in foo( CompileTimeHash( "aha" ) ); // foo is NOT constexpr For a specific reason, I cannot use the long version constexpr auto hash = CompileTimeHash( "aha" ); foo( hash ); The compiler (VC++) will not compile-time hash in the short (first) case. Is there

Macro for use in expression while enforcing its arguments to be compile time constants

試著忘記壹切 提交于 2019-12-23 01:44:11
问题 I am looking for a way to #define a macro that enforces its arguments to be compile time constants, and at the same time can be used in an expression. The method should be working under C90 and be upward compatible - if possible also portable for the different C++ variants. Also a 0-footprint to memory is preferable. Consider a compile-time minimum macro as an example. The behavior should be: #define CT_MIN(CTC_A, CTC B) <<<MAGIC>>> int a = 1; int b = a + CT_MIN(1,4); /* OK */ int c = a + CT

constexpr c string concatenation, parameters used in a constexpr context

…衆ロ難τιáo~ 提交于 2019-12-22 07:22:14
问题 I was exploring how far I could take the constexpr char const* concatenation from this answer: constexpr to concatenate two or more char strings I have the following user code that shows exactly what I'm trying to do. It seems that the compiler can't see that the function parameters (a and b) are being passed in as constexpr. Can anyone see a way to make the two I indicate don't work below, actually work? It would be extremely convenient to be able to combine character arrays through

constexpr c string concatenation, parameters used in a constexpr context

北慕城南 提交于 2019-12-22 07:21:41
问题 I was exploring how far I could take the constexpr char const* concatenation from this answer: constexpr to concatenate two or more char strings I have the following user code that shows exactly what I'm trying to do. It seems that the compiler can't see that the function parameters (a and b) are being passed in as constexpr. Can anyone see a way to make the two I indicate don't work below, actually work? It would be extremely convenient to be able to combine character arrays through