TIP 641: Let Tcl_GetBoolean(FromObj) handle (C99) bool.

Login
Author:         Jan Nijtmans <[email protected]>
State:          Final
Type:           Project
Vote:           Done
Created:        02-Oct-2022
Tcl-Version:    8.7
Tcl-Branch:	    tip-641
Vote-Summary:  Accepted 3/0/0
Votes-For:     JN, KBK SL
Votes-Against: none
Votes-Present: none

Abstract

This TIP proposes new behavior for public routines Tcl_GetBoolean and Tcl_GetBooleanFromObj: It's boolPtr argument can now point at a bool as well as an int.

Specification

Since C99 (and also recent C++ compilers), there's a new bool type, which can only have the values true or false. Tcl predates this, therefore it doesn't have an API to handle this type directly: In stead of bool the only portable way to use it, is use an int temporary variable, and convert it manually to the desired type. For example, see here (which uses the Microsoft-specific BOOL, but the idea is the same).

Some platforms use sizeof(bool) == sizeof(char), other platforms use sizeof(bool) == sizeof(int). Therefore, we can implement this with already existing functions (the char variant depends on TIP #618). We just define 2 macro's Tcl_GetBoolean/Tcl_GetBooleanFromObj, which map to the original functions if sizeof(*(boolPtr)) == sizeof(int) and to Tcl_GetBool/Tcl_GetBoolFromObj when sizeof(*(boolPtr)) == sizeof(char). A panic will result it none of those two match (but that's actually impossible when using bool).

History

This implementation used to be part of TIP #618, but doing that led to a lot of discussion. Since that TIP did multiple unrelated proposals, it was decided to split it: TIP #618 introduces the new API which makes this TIP possible at all. This TIP explains how the new API can be used to handle the C99/C++ bool type, just be defining two new macro's.

Compatibility

This is 100% upwards compatible.

Reference Implementation

Under development on the tip-641 branch.

Copyright

This document has been placed in the public domain.