Questions tagged [constant-expression]

Constant expressions can be evaluated at compile time.

Many languages require certain initializers to be constant expressions. For example:

  • Array bounds
  • Selectors in case statements
  • Bit-field length specification
  • Enumeration initializers
  • Non-type template arguments

A greatly restricted set of operands are allowed in constant expressions. Generally, variables are not allowed. For example, C++ allows:

  • Literals
  • Enumeration constants
  • Values declared as const that are initialized with constant expressions
  • sizeof expressions

Questions with this tag usually need help with error messages that indicate lines that require constant expressions.

215 questions
1
vote
3 answers

Is it possible to initialize a static array using a ternary operator in an initializer?

Is it possible to initialize a static array with a ternary operator in an initializer? For example: // Array of functions static const Callback_t CallbackArray[] = { GenConfig, GenInfo, /* here is the ternary operator to chose a…
1
vote
0 answers

Compile python source without constant folding optimization

I have a python snippet as follows 5 + 6 * 8 Compiling this to AST gives: >>> ast.dump(compile('5+6*8', '', 'eval', ast.PyCF_ONLY_AST)) Expression(body=BinOp(left=Num(n=5), op=Add(), right=BinOp(left=Num(n=6), op=Mult(), right=Num(n=8)))) Note…
1
vote
1 answer

NameError in ProjectsController#index

In my rails app I am getting the following error: NameError in ProjectsController#index app/controllers/projects_controller.rb:6:in `index' My routes look like this: RailsStarter::Application.routes.draw do resources :projects root :to =>…
1
vote
1 answer

`core constant expression` vs `constant expression`

The definition of a core constant expression depends on the concept of a constant expression as can be seen in bullet points (2.7.1) and (2.9.1) of N4140. §5.19/2: A conditional-expression e is a core constant expression unless the evaluation…
Ayrosa
  • 3,385
  • 1
  • 18
  • 29
1
vote
1 answer

Constant Expression Parameters

I have a function that creates a std::bitset, whose length is a constant function parameter. It doesn't work because the constant isn't an "integral constant expression". Is there any way I can make this work? For reference: void Foo(const…
Woody1193
  • 7,252
  • 5
  • 40
  • 90
1
vote
0 answers

insert View into RelativeLayout's childerne Xamarin Forms

I'm in need of inserting a view obj to the children array of a relativeLayout. but the problem is that i don't know how i can do it so it's Relative To Parent. like i do when i added it, like shown below layout.Children.Add(BoxView,…
DaCh
  • 921
  • 1
  • 14
  • 48
1
vote
2 answers

Python constant folding with labels

I would like to do something that is similar to constant folding using Python. Python has a convenient built function, eval(), so that constant only equations can be easily folded in by applying eval(). Example: s = '4 + (5) * 2' reduced_s =…
user2756376
  • 117
  • 9
1
vote
2 answers

Creating an array with a variable

I have the following code: #include using namespace std; int main() { unsigned int endx = 5; unsigned int endy = 5; unsigned int endz = 5; int Matrix[endx+1][endy+1][endz+1] = {}; return 0; } I get error C2057:…
Peter Parker
  • 93
  • 1
  • 8
1
vote
3 answers

Constant expression with custom object

I'm trying to use an instant of a custom class as a template parameter. class X { public: X() {}; }; template struct Bar { }; const X x; Bar foo; The compiler states that x cannot appear in a constant expression. Why…
nils
  • 4,649
  • 3
  • 19
  • 8
1
vote
2 answers

Writting a mathematical calculation as the value of a constant expression in a variable inicialization increases the computational work?

In C++, does a mathematical declaration in a constant variable initialization costs some extra processing? Or modern compilers would automatically put the result of the mathematical calculation inside the variable when creating the .exe file? So…
Momergil
  • 2,213
  • 5
  • 29
  • 59
1
vote
2 answers

Constant initialization in a dynamic way

I think that the the variable declared as const applies only Static Initialization. I've written the following: #include #include struct A{ }; const A *i = new A(); int main(){ } and it works fine. Ideone But I expected…
user2953119
1
vote
3 answers

Declaring symbolic constants in header without initializing?

It is common practice to define symbolic constants in a header file: #define T_FOO 1 #define T_BAR 2 Ugly. static const int T_FOO = 1; static const int T_BAR = 2; Better, since not preprocessor. enum { T_FOO = 1, T_BAR } T_Type; Better…
DevSolar
  • 67,862
  • 21
  • 134
  • 209
1
vote
1 answer

Return a constant text string for every row in a column (dynamic) SQL Server 2008

Thank you in advance for your time and expertise. I use the following query SELECT wh_id, item_number FROM table_1 To get a table result that looks like this wh_id item_number 1 12341 2 12342 3 12343 4 …
Mr.Drew
  • 31
  • 1
  • 6
1
vote
2 answers

Expression Tree how do I capture a local variable

I'm currently working towards creating dynamic expressions and I have the following scenario, which I'd like help to achieve. given: public class planet { public string name { get;set; } } class someTestClass { [Test] public void…
ermagana
  • 1,090
  • 6
  • 11
1
vote
1 answer

Unterminated string constant in Google Map API using PHP

i have problem with unterminated string constant. here is my code in PHP: 'html' => '
Iwan PC
  • 11
  • 3