🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3441

Article for comp.std.c++: Eliminating #ifdef: if const

#3441
From: Mark Russell
Date: Fri, 16 Feb 1996 17:03
87 lines
3150 bytes
Subject: Eliminating #ifdef: if const

Here's an idea I had for a non-preprocessor replacement for #if and  #ifdef.

	if const (<expr>) { <stuff> }

`if const' is a compile time version of `if'.  <expr> is a constant
expression which is convertible to bool.  If it yields true, <stuff>
is processed as normal.  Otherwise <stuff> is parsed into tokens, but
ignored except for counting { and } nesting.  <stuff> is anything that
can appear inside a namespace block.  `if const' does not introduce a
new scope.

The following is also legal, with the obvious meaning:

	if const (<expr>) { <stuff> } else const { <other stuff> }

There are also a number of compile-time pseudo-functions (I hesitate
to call them pragmas):

	bool #os_version(const char* str)

		os version (uname -r) at or later than str

	bool #os_name(const char *str)

		os name (uname -s) is str

	bool #compiler_name(const char *)
	bool #compiler_version(const char*)

		Same tests for the compiler

	bool #header_exists(const char* path)

		Would `#include <path>' succeed?

	bool #have_test(const char* varname)

		Would #varname(str) work (this would be useful to
		allow testing for implementation defined tests.

Using these you could do things like this:

	// Are we running IRIX 5.3
	const bool using_irix_53 = #os_name("IRIX") &&
				   #os_version("5.3") && !#os_version("5.4")

	if const (using_irix_53) {
		// Do some stuff needed just for IRIX 5.3
	}

	if const (#compiler("g++")) {
		// Do some g++ stuff
	}

	if const (#header_exists("sys/frobnitz9000.h")) {
		#include "sys/frobnitz9000.h"
		// Do stuff for the Frobnitz 9000
	}

	if const (#have_test("extension")) {
		const bool have_foo_extension = #extension_exists("foo");
	}
	else const {
		const bool have_foo_extension = false;
	}

	if const (have_foo_extension) {
		// Stuff using the `foo' extension
	}

With this I could personally stop using the preprocessor for anything
except #include.  If `include' (as described in D&E) is also
implemented, I could stop using the preprocessor altogether, which
would be nice.

I realise this is too late for the upcoming version of the standard,
but I think it would be a useful thing for a subsequent version.

Comments?

Mark Russell
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
Technical Details
Message-ID: <199602160807.IAA06126@condor.ukc.ac.uk>
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern
From: Mark Russell <M.T.Russell@ukc.ac.uk>
Newsgroups: comp.std.c++
Subject: Article for comp.std.c++: Eliminating #ifdef: if const
Date: 16 Feb 1996 09:03:51 PST
Organization: -
Approved: austern@isolde.mti.sgi.com
Message-ID: <199602160807.IAA06126@condor.ukc.ac.uk>
NNTP-Posting-Host: isolde.mti.sgi.com
X-Original-Date: Fri, 16 Feb 1996 08:07:47 +0000
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBVAwUBMSS5D0y4NqrwXLNJAQHCMgIAwfzdWgWPjg1FTLNyzj5jBN30uwRyb5EW
 hu4HHUL2176dHKNZ8uJWUlUJ81tzJVgIGL0y8dKhfGNir0AEgoYhTA==
 =//uW
Originator: austern@isolde.mti.sgi.com