const int a=a;

The following compiles without a comment in Arduino 1.6.7:

const int a = a;
void setup () {}
void loop () {}

The following also compiles, so I don’t think it is simply an optimizer issue:

const int ledPin = ledPin;
void setup() { 
 pinMode (ledPin, OUTPUT);
}
void loop() { }

This does not compile:

void setup() { 
 const int ledPin = ledPin;
 pinMode (ledPin, OUTPUT);
}
void loop() { }

avr-gcc -Wall -Wextra -Os -Wl,–gc-sections -mmcu=atmega328p -o […]

The variable a has an apparent resulting value of 0. Is this a known behavior of C++ or is it a bug?

  • Bruce

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

bdhubbard: This does not compile:

void setup() { 
  const int ledPin = ledPin;
  pinMode (ledPin, OUTPUT);
}

You forgot "static"...

void setup() 
{
  static const int ledPin = ledPin;
  pinMode (ledPin, OUTPUT);
}

Without the "static" ledPin is a pointless thing. Neither a constant nor a variable. With or without the "self initializer".

bdhubbard: The variable a has an apparent resulting value of 0. Is this a known behavior of C++ or is it a bug?

Same with Visual Studio 2008 so I'm going with "known behaviour".