What Does a A() Mean

What does a = a || [] mean?

Yes.

var x = x || [];

This means if x has any value, assign it to x or initialise with empty array [];

var x = 5;
x = x || 6;

x will equal to 5 in this case.

var x;

x is undefined now.

var x = x || [];

x was not previously defined, so initialised with empty array [];

What does A a() mean?

This line

A a();

declares a function named a, returning A with no arguments. (See Most vexing parse).

What you want is

A a = A(); // value-initialization
A a{}; // the same but only valid in C++11 (and currently not supported by MSVS)

or

A a; // default initialization

C++11, §8.5/10

Note: Since () is not permitted by the syntax for initializer,

X a();


is not the declaration of a value-initialized object of class X, but the declaration of a function taking no argument and returning an X.

For your class, value-initialization == default-initialization (at least for the outcome).
See my answer here: C++: initialization of int variables by an implicit constructor for Infos on value- vs. default-initialization for POD or built-in types.

Is a^a or a-a undefined behaviour if a is not initialized?

In C11:

  • It's explicitly undefined according to 6.3.2.1/2 if a never has its address taken (quoted below)
  • It could be a trap representation (which causes UB when accessed). 6.2.6.1/5:

Certain object representations need not represent a value of the object type.

Unsigned ints can have trap representations (e.g. if it has 15 precision bits and 1 parity bit, accessing a could cause a parity fault).

6.2.4/6 says that the initial value is indeterminate and the definition of that under 3.19.2 is either an unspecified value or a trap representation.

Further: in C11 6.3.2.1/2, as pointed out by Pascal Cuoq:

If the lvalue designates an object of automatic storage duration that could have been
declared with the register storage class (never had its address taken), and that object
is uninitialized (not declared with an initializer and no assignment to it has been
performed prior to use), the behavior is undefined.

This doesn't have the exception for character types, so this clause appears to supersede the preceding discussion; accessing x is immediately undefined even if no trap representations exist. This clause was added to C11 to support Itanium CPUs which do actually have a trap state for registers.


Systems without trap representations: But what if we throw in &x; so that that 6.3.2.1/2's objection no longer applies, and we are on a system that is known to have no trap representations? Then the value is an unspecified value.
The definition of unspecified value in 3.19.3 is a bit vague, however it is clarified by DR 451, which concludes:

  • An uninitialized value under the conditions described can appear to change its value.
  • Any operation performed on indeterminate values will have an indeterminate value as a result.
  • Library functions will exhibit undefined behavior when used on indeterminate values.
  • These answers are appropriate for all types that do not have trap representations.

Under this resolution, int a; &a; int b = a - a; results in b having indeterminate value still.

Note that if the indeterminate value is not passed to a library function, we are still in the realm of unspecified behaviour (not undefined behaviour). The results may be weird, e.g. if ( j != j ) foo(); could call foo, but the demons must remain ensconced in the nasal cavity.

typedef struct A A in C

First, remember that C and C++ are different languages. C++ is rooted in C, but went in some very different directions. This is one of them.

When declaring or defining a variable of type struct in C, one must always use the keyword struct. For example

struct spBone myspBone;

is required. Why this is required likely made a lot of sense back in the 1970s.

However, the programmer is establishing spBone as an alias to struct spBone so that they can use the alias and

spBone myspBone;

as you can in C++.

What does `Num a = a` mean in Haskell type system?

1 :: Num a => a means that 1 has some type a, where a is an instance of the Num typeclass. Note that Num is not a type, but a typeclass, which describes common properties of various types. The Num typeclass, for example, describes types that are numeric, and so support basic arithmetic. The native machine integer type Int is an instance of Num, as is the arbitrary-sized Integer, the floating point type Double, and even the rational number type Rational.



Related Topics



Leave a reply



Submit