Boost logo

Boost :

Subject: Re: [boost] [Review] GGL review starts today, November 5th
From: Mateusz Loskot (mateusz_at_[hidden])
Date: 2009-11-19 18:17:19

Brandon Kohn wrote:
> Dimensionally qualified quantities can be calculated for geometries for
> whose dimensions the quantity makes no sense (e.g. area( point ),
> length( point ).) These functions return default constructed instances
> of their corresponding return type as shown here:
> template<typename ReturnType, typename Geometry, typename Strategy>
> struct calculate_null
> {
> static inline ReturnType apply(Geometry const& , Strategy const&)
> {
> return ReturnType();
> }
> };
> In my test ReturnType was double. This would seem to silently compile
> and return garbage on these inputs. In practice on vs2008( VC9 ) these
> return values were 0 (with optimizations on) even though the return
> resolves to double();. I'm guessing other platforms won't be so lucky.

This expression is correct and well defined.
If I may, here is reference from the C++ standard document:

5.2.3 Explicit type conversion (functional notation)

2 The expression T(), where T is a simple-type-specifier ( for
a non-array complete object type or the (possibly cv-qualified)
void type, creates an rvalue of the specified type, which is
value-initialized (8.5; no initialization is done for the void() case).


5 To zero-initialize an object of type T means:
  — if T is a scalar type (3.9), the object is set to the value of 0
    (zero) converted to T;
  — if T is a non-union class type, each nonstatic data member and each
     base-class subobject is zero-
  — if T is a union type, the object’s first named data member89) is
  — if T is an array type, each element is zero-initialized;
  — if T is a reference type, no initialization is performed.

  To default-initialize an object of type T means:
  — if T is a non-POD class type (clause 9), the default constructor for
    T is called (and the initialization is
    ill-formed if T has no accessible default constructor);
  — if T is an array type, each element is default-initialized;
  — otherwise, the object is zero-initialized.

  To value-initialize an object of type T means:
  — if T is a class type (clause 9) with a user-declared constructor
    (12.1), then the default constructor for T is
    called (and the initialization is ill-formed if T has no
    accessible default constructor);
  — if T is a non-union class type without a user-declared constructor,
    then every non-static data member and base-class component of T
    is value-initialized;
  — if T is an array type, then each element is value-initialized;
  — otherwise, the object is zero-initialized

A program that calls for default-initialization or value-initialization
of an entity of reference type is ill-formed. If T is a cv-qualified
type, the cv-unqualified version of T is used for these definitions of
zero-initialization, default-initialization, and value-initialization.

Best regards,

Mateusz Loskot,
Charter Member of OSGeo,

Boost list run by bdawes at, gregod at, cpdaniel at, john at