illegal token on right side of ‘::’

libLAS - ASPRS LiDAR data translation toolsetOne of libLAS users reported that when use of #include <liblas/lasreader.hpp> in his application compiled with Visual C++ 10.0 from Visual Studio 2010 cause this error:

utility.hpp(253): error C2589: '(' : illegal token on right side of '::'

The error is an incarnation of a very well-known problem in Visual C++ when using the C++ Standard Library elements, especially the Standard Template Library, in Windows API-based programs. As libLAS library does use the C++ library, so does a user’s application if includes libLAS headers.

The problem is caused by conflicting definitions of min() and max() macros defined in windef.h header. Macros in C++ are scope-less evil, especially if defined in public headers using such extremely unique names as min or max. The fact that Microsoft defined it way before C++ was born absolves them at large, but for the Spirit sake, they should learn the lesson and disable it for good in C++ mode (but not yet another MS-specific way!). No one who’s sane need or want to use them!

Pie in the sky. In the meantime, C++ programmers as the libLAS user who reported this problem have to deal with it on their own. The easiest way is to check CodeProject or Q143208 or search (not google) for solution like #define NOMINMAX for Visual C++ compiler.

However, is another option is to apply a simple trick to call of *::min() or *::max() functions (i.e. std::min() or std::max() which effectively prevents macro substitution, so the Visual C++ compiler (or any other compiler with similar problem) does not complain about illegal token. The trick is to wrap function name, fully qualified name, with parentheses:

(std::min)(x, y);

In most cases of use of C++ Standard Library as described above, it is required for the following functions:

(std::min)(x, y);
(std::max)(x, y);
(std::numeric_limits<T>::min)();
(std::numeric_limits<T>::max)();

In case a user-defined type has a member function with exactly the same name as a macro present in global scope (macros always live in global scope!), it may be necessary to apply the very same trick when a member function is called on an object:

template <typename T, int Size>
struct Series
{
  T min() { return *(std::min_element(s, s + Size); }
  T& operator[](int index) { return s[index]; }
private:
  T s[Size];
};

Series<int, 3> s;
s[0] = 2;
s[1] = 3;
s[2] = 1;

int m = (s.min)(); // long way, but here is the trick

There is one side effect which may be an inconvenience. This trick disables argument dependent name lookup (ADL, aka Koenig lookup).

Talk on Geometry Template Library

Another video from BoostCon’09 conference. This time it is about the Geometry Template Library (GTL) for STL-like 2D operations by Lucanus Simonson and Gyuszi Suto:

Here is PDF document with slides from the conference talk. The GTL library was submitted to Boost C++ Libraries for formal review as Boost Polygon library. Performance was at large a significant subject of posted reviews and discussions. Barend and Bruno from Generic Geometry Library (GGL) have been working hard to build a comprehensive performance study of Open Source geometry libraries available on the market. Results and source code are available.

Size of enumeration type in C++

In C++ enumeration is used to define set of named constants. Each enumeration is a distinct compound type (enumerated type) and is subject to all rules of type system defined in C++ language. Internally, enumeration is represented with so called underlying type. The C++ Standard ISO/IEC 14882:2003 (Section 7.1/5) specifies it as follows:

The underlying type of an enumeration is an integral type that can represent all the enumerator values defined in the enumeration.

It is not specified which particular integral type is used to represent an enumerated type. Given that number of bytes representing fundamental integral types is implementation defined in C++ and only sizeof(char), sizeof(signed char) and sizeof(unsigned char) are guaranteed to be always 1, value reported by sizeof operator applied on enumeration is considered as implementation defined as well. Sometimes one may need to control size of enumerated type in memory, for instance to serialise and de-serialise objects easier.

Some compilers allow to specify underlying type of enumeration. For instance, Microsoft Visual C++ provides dedicated language extension:

enum Color : unsigned char
{
   red, green, blue
};
// assert(sizeof(Color) == 1);

Obviously, this technique is not portable across other implementations of C++ language.

In order to solve this problem in portable way, a simple class template can be defined:

template <class E, class T>
struct enumeration
{
    typedef T type;
    typedef E enum_type;

    enumeration()
        : e_(E())
    {}

    enumeration(E e)
        : e_(static_cast<T>(e))
    {}

    operator E() const
    { return static_cast<E>(e_); }

private:
    T e_;
};

The enumeration class can be used as a portable way to wrap any enumerated type and to specify (or limit) amount of bytes used to represent it. It is also interchangeable with actual enumeration being wrapped:

#include <cassert>

enum Color { red, green, blue };

int main()
{
    enumeration<Color, unsigned char> color(blue);
    assert(sizeof(color) == 1);

    Color c = color;
    assert(c == blue);
}

It’s also possible to provide default argument for template parameter T specifying underlying type of wrapped enumeration. For instance, if most constant values in a project range from 0 to 255, default type value of T can be unsigned char.

Got new toy

I’ve just got new toy tool. Since yesterday, I’m a happy owner of the most standards-conformant C++ compiler that ever existed – Comeau C/C++ from Comeau Computing.

I’ve successfully installed Comeau C/C++ 4.3.10.1 Beta compiler and libcomo36, accompanying Standard C++ Library, on 32-bit version of latest Ubuntu 9.04.

I’m going to use Comeau C/C++ for purposes like analysis and review of C++ source code I write or use, verification of code portability as well as for learning elements of C++ and experimenting. This is excellent compiler for learning C++ language. Among loads of features, one of the coolest is ability to give clear and informative error messages. This is very helpful while working with complex class and function templates.

Continue reading

Geospatial bits on BoostCon’09

On the 3rd of May 2009, the best event dedicated to C++ programmers starts in Aspen, US. It is BoostCon’09. The conference gathers developers and users of Boost C++ Libraries.

This year the event brings some flavour of geospatial technologies. Barend Gehrels and Bruno Lalande – core developers – are presenting their Generic Geometry Library

    uff. :-)

GGL on Ohloh.net

I’ve added Generic Geometry Library (GGL) to the software stack on Ohloh.net here. The code analysis is being generated from Preview releases frequently uploaded to Sandbox in SVN repository of Boost C++ Libraries. This repository is not the development mainstream, but it is being updated every time new preview is issued..

SOCI 3.0.0 RPM Packages

Denis Arnaud prepared RPM packages for SOCI library version 3.0.0. As Denis explains, these RPM packages primarily target Fedora family, but should work on other RPM-based distributions.

Currently, SOCI 3.0.0 RPM packages are available to download from Open Travel Request Parser project website. By the way, I’ve revealed Open Travel Request Parser uses SOCI, it’s really nice.

SOCI is a database access library for C++ that makes the illusion of embedding SQL queries in the regular C++ code, staying entirely within the Standard C++. It also fits very well to the world of templates defined in C++ Standard Library and offers extensive integration with data types from Boost C++ Libraries like optional, tuple and fusion:

Rowset rs = (sql.prepare << "select name from persons");
std::copy(rs.begin(), rs.end(), std::ostream_iterator(cout, "\n"));

Not to mention the coolness of flexible support for user-defined types.