🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: rec.games.programmer
Article #2031

Re: cout vs printf speed (was: Should I learn C or C++ first?)

#2031
From: Kevin Cline
Date: Thu, 31 Aug 1995 16:43
80 lines
2753 bytes
In article <423cuc$tab@harbinger.cc.monash.edu.au>,
Mr A. Walker <junaid@silas.cc.monash.edu.au> wrote:
>James Kanze US/ESC 60/3/141 #40763 (kanze@lts.sel.alcatel.de) wrote:
>: 
>: Actually, I doubt it.  Normally, the format string in production code
>: will not be a string literal, but rather something read from a
>: configuration file, dependant on the locale setting.
>: 
>: Or do you really think everyone in the world can read English
>: fluently.
>
>	This run-time dynamic behaviour is unique to sprintf() and
>friends. There is no way to implement this using cout, becuase the
>implicit formatting specification is unknown at compile time. There
>for C++ streams are in fact more restrictive than the C lib I/O .
>
>Junaid

I assume you are talking about an interface like

  MessageId.H:
  ------------
	enum { DOOR_LOCKED, MONSTER_SEEN, ... } MessageId;

  Message.H:	
  ----------
	#include "MessageId.H"
	void message(MessageId, const char* fmt, ...)

with some data file that is compiled to produce MessageId.H, and also
a run time database of message formats for various languages.

For C++, I would compile the data file to produce a class definition
for each message, instead of just an enum identifier, e.g.

  Messages.dat:
	FoundObjectMsg(string color, string type) {
	  ENGLISH(There is a %s %s here,1,2)
	  SPANISH(Aqui esta un %s %s,2,1)
	}

  Msg.H:
	class Msg
	{
	  static void setLanguage(Language l) {
	    language = l;
	  }

	protected:
	  void print(int msg, ...) {
	    vprintf(format(msg, language), arglist)
	  }
	private:
	  Language language;
	}
	
  Messages.H: (generated code)
	class IllegalEntryMsg : public Msg 
	{
	private:
	  enum { msg_no = 1717; }
	public:
	  IllegalEntryMsg(const char* field,
			  const char* data)
	  { 
	    print(msg_no, field, data);
	  }
	}

Sorry for my pathetic monolingualism.

This scheme is typesafe, and no more expensive in time or space
than non-typesafe methods.  There is a minimal cost at compile time.

I haven't implemented this yet.  If anyone actually implements
a compiler to convert message definitions into class definitions,
please let me know.

-- 
Kevin Cline
Technical Details
Message-ID: <424os9$1ci@sun132.spd.dsccc.com>
Path: arc1-ca.pugleaf.net!camelot.dsccc.com!not-for-mail
References: <DDJ2Lp.3DG@pentagon-ai.army.mil> <41afe9$d10@newsbf02.news.aol.com> <423cuc$tab@harbinger.cc.monash.edu.au>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!camelot.dsccc.com!not-for-mail
From: kcline@sun132.spd.dsccc.com (Kevin Cline)
Newsgroups: rec.games.programmer,comp.lang.c++
Subject: Re: cout vs printf speed (was: Should I learn C or C++ first?)
Date: 31 Aug 1995 11:43:53 -0500
Organization: DSC Communications Corporation Switch Products Division
Message-ID: <424os9$1ci@sun132.spd.dsccc.com>
References: <DDJ2Lp.3DG@pentagon-ai.army.mil> <41afe9$d10@newsbf02.news.aol.com> <423cuc$tab@harbinger.cc.monash.edu.au>
NNTP-Posting-Host: sun132.spd.dsccc.com