2

As per 6.6.10

10 An implementation may accept other forms of constant expressions.

the implementation may consider const (or even not const as answers are focused on it - but it is not the core of the question) variables as constant expressions.

Is my logic correct? If not why?

Some examples:

//file scope

int a = 5;
const int b = 10;

int c[a];
int d[b];

IMO both meet the requirements. They can be evaluated compile time. The value is known and the static arrays have sizes known compile time.

volatile int x = 5;  //cannot be considered as known compile time. ????
void foo(int a)
{
    static int b[a];
}

In this example a cannot be evaluated compile time - so it cannot be used as constant expression

0___________
  • 60,014
  • 4
  • 34
  • 74
  • 1
    @ryyker: The question is not whether a use of a `const` variable is necessarily a constant in C but whether a C implementation **may** accept a `const` variable as a constant given the license quoted in the question. – Eric Postpischil Apr 08 '21 at 13:13
  • @ryyker It does not answer my question and is rather of off-topic – 0___________ Apr 08 '21 at 13:13
  • https://stackoverflow.com/questions/54135942/why-initializer-element-is-not-a-constant-is-not-working-anymore would the answer there answer your question? :p The conclusion is "GCC evidently implemented an extension" which means: `Is my logic correct?` yes and such implementation even exists. – KamilCuk Apr 08 '21 at 13:18
  • @ryyker if it was so obvious I would not ask this question here. Your citation (or rather that answer one) one of the possible forms of the CE. But as per point 10 it is not limited to this as implementation may have other forms. That answer does not answer my question. – 0___________ Apr 08 '21 at 13:27
  • Nothing in C standard says that they "disqualified" from it. It is is more difficult to implement – 0___________ Apr 08 '21 at 13:44
  • 1
    @ryyker: Re “limited to mean read-only, disqualifying it from being considered one of the "other forms"”: That is a *non sequitur*. The fact that something is read-only does not mean it is disqualified from being an other form. Clearly a `const` variable is not in the core set of constant expressions that must be supported, so it is another form. – Eric Postpischil Apr 08 '21 at 13:47
  • The crux of the question then is whether or not an implementation may consider a C `const` variable as an _other form_ of a _`constant` expression_. Because _`constant` expressions_ have the property of _constantness_, The compiler developer must ask: is a _`const` variable_ worthy to be considered as a form of _`constant` expression_? – ryyker Apr 08 '21 at 14:37
  • ...In an ordered world (i.e. one without chaos.) only those objects that also have the property of constantness should ever be allowed to be considered as another form of _`constant` expression_. But since we do not live in a perfectly ordered world, it is conceivable a compiler developer could choose to include a little chaos into their design. – ryyker Apr 08 '21 at 14:37

2 Answers2

2

Constant expressions doesn't have anything to do with the const qualifier. See 6.6/6 for the definition of an integer constant expression.

6.6/10 refers to the earlier parts of the same chapter speaking of integer constant expressions, constant expressions in initializers and so on. I believe the quoted part refers to various corner case expressions such as this:

static int x;
static int y = (int)&x;

This isn't strictly conforming since &x is not regarded as a integer constant expression but as an address constant. Neither gcc nor clang accepts it. I believe 6.6/10 allows compilers to support code like the above example as an implementation-defined extension.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 3
    6.6.6: "Let him who hath understanding reckon integer constant expressions. For it is a constant number. It shall have integer type and shall only have operands that are integer constants." :) – Lundin Apr 08 '21 at 13:49
  • 1
    How is it related to my question? 6.6.6 is one of the possible form but p.10 allows also other forms. IMO you do not answer my question. – 0___________ Apr 08 '21 at 14:31
  • @0___________ They list various forms of constant expressions through 6.6 but also allow for implementation-defined ones, which is vague. This is common throughout the standard, they list various allowed forms then finish with "or in some implementation-defined manner". Which then refers to the previous parts and not to something completely unrelated. – Lundin Apr 08 '21 at 14:43
  • that `const` is causing confusion. I know the `const` has nothing in common with constant expression and the question is not about it. I understand that you are confused by many beginners questions about why my code is not compiling? – 0___________ Apr 08 '21 at 14:43
  • see my amended question. Maybe examples will explain what I mean – 0___________ Apr 08 '21 at 14:44
  • @0___________ Evaluated at compile-time and constant expression isn't the same. This is a known defect of C which was fixed in C++. For example, we can write lots of macros that are evaluated at compile-time, but still can't be used as array size etc if they don't result in a constant expression. – Lundin Apr 08 '21 at 14:46
  • Here seems to be a confusion between a _constant expression_ (which the question is about) and an _integer constant expression_ (which is just one strict form of _constant expression_). Therefore 6.6.6 is not all too relevant. – Armali Apr 08 '21 at 17:49
  • @Armali "6.6/10 refers to the earlier parts of the same chapter speaking of integer constant expressions, constant expressions in initializers and so on." Such as address constants in the given example. – Lundin Apr 08 '21 at 20:22
0

Building on the link from @ryyker 's comment.

In C, an expression is considered constant if it's made entirely out of compile-time constants (i.e. literals). const variables are not accepted because const doesn't necessarily mean "constant"; it just means that a variable is immutable (read-only) from the perspective of the current compilation unit.

Consider a function like this one...

int add(const int a, const int b) {
    return a + b;

You could call this function by passing in compile-time constants, but you could also pass in mutable variables. If you pass in a variable, it is treated as const (read-only) for the purposes of that scope, but its value still isn't known at compile time so it isn't a "constant".

  • 2
    `a` or `b` does not meet the 6.6.2 requirement. The example is not relevant – 0___________ Apr 08 '21 at 13:30
  • 1
    Re “an expression is considered constant if”: Yes, “if,” but but not “if and only if.” The text from the C standard makes it clear this is not an “only if”; there are other forma that may be accepted. This answer does not speak to the question of whether a `const` variable may be one of them. It fails to answer the question. – Eric Postpischil Apr 08 '21 at 13:50
  • 1
    @Just a heads up. After OP pointed out that the link (that you are referring to.) did not address the spirit of the question, and after looking at some of the comments, I had to agree. So it has been removed. – ryyker Apr 08 '21 at 15:30