Wednesday, September 14, 2011

Volatility

The "volatile" type modifier presents an interesting problem.

From what I understand, "volatile" is only meaningful on l-values - something like "(volatile int) 1" doesn't make a lot of sense. Unfortunately, when you pass a type to a template as a template parameter, you don't always know whether the template is going to use the type as an l-value or not. So for example if you wanted to create something like "List[volatile(int)]", we would have to allow the "volatile" qualifier on any type, l-value or not - it would basically be a no-op in most cases.

The other approach is to say that "volatile" is an attribute of a variable or field, rather than being an attribute of a type. This makes more sense, but the downside is that you can no longer pass it as a template parameter. Thus, in order to have an array of volatile ints, you have to create a special VolatileArray[int] class, which is an exact clone of Array except for adding the volatile qualifier to the array's internal member declarations.

No comments:

Post a Comment