2001

Grusel++ of the month

Home
Download Source
/******************************************************************************/
/*                                                                            */
/*                                                         FILE: november.cpp */
/*                                                                            */
/*  printf, C++ - style                                                       */
/*  ===================                                                       */
/*                                                                            */
/*  Compiled and tested with Visual C++ V7.0 BETA                             */
/*  When compiled with VC6.0, it will NOT work as intended !!                 */
/*                                                                            */
/*  V1.00   30-NOV-2001   P. Tellenbach   https://www.heimetli.ch/            */
/*                                                                            */
/******************************************************************************/

#include <iostream>

using std::cout ;
using std::endl ;

char *s = "Hello world !" ;

template< char *str >
int printf( const char *, ... )
{
   cout << str << endl ;

   return int() ;
}

int main( void )
{
   return printf<s>( "printf( %d, %c, %s ) ;\n", 1, '2', "3" ) ;
}

Update 28. September 2021

char * is no longer acceptable as a template argument. Using a reference fixed this problem. The string literal required the use of const.

/******************************************************************************/
/*                                                                            */
/*                                                         FILE: november.cpp */
/*                                                                            */
/*  printf, C++ - style                                                       */
/*  ===================                                                       */
/*                                                                            */
/*  Compiled and tested with Visual C++ V7.0 BETA                             */
/*                                                                            */
/*  V1.00   30-NOV-2001   P. Tellenbach   https://www.heimetli.ch/            */
/*                                                                            */
/*  Compiled and tested with g++ V10.2.1                                      */
/*                                                                            */
/*  V1.00   28-NOV-2021   P. Tellenbach   https://www.heimetli.ch/            */
/*                                                                            */
/******************************************************************************/

#include <iostream>

using std::cout ;
using std::endl ;

const char *s = "Hello world !" ;

template< const char *&str >
int printf( const char *, ... )
{
   cout << str << endl ;

   return int() ;
}

int main( void )
{
   return printf<s>( "printf( %d, %c, %s ) ;\n", 1, '2', "3" ) ;
}