TIP 557: C++ support for Tcl

Login
Bounty program for improvements to Tcl and certain Tcl packages.
Author:         Jan Nijtmans <jan.nijtmans@gmail.com>
State:          Final
Type:           Project
Vote:           Done
Created:        13-Dec-2019
Post-History:
Keywords:       Tcl
Tcl-Version:    8.7
Tcl-Branch:     cplusplus
Vote-Summary:	Accepted 2/0/3
Votes-For:	JN, KW
Votes-Against:	none
Votes-Present:	DGP, FV, SL

Abstract

Every once in a while there is a request to be able to compile Tcl with a C++ compiler. This would - in theory - allow C++ exceptions thrown by extensions to be handled by Tcl. This TIP is NOT about adding any code to Tcl doing that. But - as a first step - at least, Tcl needs to compile well with a C++ compiler even though we don't use any C++ features.

This TIP proposes making Tcl compilable with a C++ compiler, without actually using any C++ features.

Implementation

The Tcl source-code has been strictified in the following ways:

  • No C++ keywords are allowed any more, such as new or class, for variable names.

  • No automatic casts from (void *) to other types any more, so all malloc's must be preceded by a type casts. And a clientData variable cannot be assigned directly any more to a variable of another type. This situation requires a type cast from now on in the Tcl core.

  • All core C-code now compiles warning-free when using the (gcc) options -Wall -Wextra -Wwrite-strings -Wpointer-arith.

  • Travis build sections are added, which compile Tcl with g++ (UNIX/Windows). So, any future commit which violates C++ compilation will lead to a failed build.

Nothing is done yet in the Tcl code to handle C++ exception, neither is any of the API now available as C++ classes.

Tk has a cplusplus branch as well, doing the same. This work is complete for UNIX, but not yet for Windows and MacOS.

Caveats

C++ is more strict in using enum's than C is. Enums cannot be stored in ints (and reverse) without type casts in C++. Tcl doesn't use enums, but there was one external API function (in ttk) which actually used enum but its argument in the public API was int: Ttk_GetOrientFromObj. This TIP deprecates Ttk_GetOrientFromObj, and adds a new internal function TtkGetOrientFromObj with the correct signature. I didn't find any extension using Ttk_GetOrientFromObj.

Copyright

This document has been placed in the public domain.

History