November 23, 2010
Dear Digital Mars users,

Why does the following program fail to compile while Borland C++ 5.5 eats it.

dmc_wincall.c++(22) : Error: constant initializer expected
--- errorlevel 1


#include <windows.h>

typedef __declspec(dllimport)
  BOOL  __stdcall (*WriteFile_funptr)
  ( HANDLE, LPCVOID, DWORD, LPDWORD, LPOVERLAPPED );

template < WriteFile_funptr function >
void wrapper( HANDLE p0, LPCVOID p1, DWORD p2, LPDWORD p3, LPOVERLAPPED p4 )
{
  function( p0, p1, p2, p3, p4 );
}

int main( int argc, char *argv[] )
{
  HANDLE Stdout = GetStdHandle( STD_OUTPUT_HANDLE );
  DWORD written;
  wrapper< WriteFile >( Stdout, "Hello\n", 6, &written, NULL );

  return 0;
}


The compiler is:

Digital Mars C/C++ Compiler Version 8.52.5n
Copyright (C) Digital Mars 2000-2010.  All Rights Reserved.
Written by Walter Bright
http://www.digitalmars.com


Johann

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/
November 26, 2010
Johann 'Myrkraverk' Oskarsson wrote:
> Dear Digital Mars users,
> 
> Why does the following program fail to compile while Borland C++ 5.5 eats it.
> 
> dmc_wincall.c++(22) : Error: constant initializer expected
> --- errorlevel 1

The problem is in the _declspec(dllimport). Such get compiled with an extra level of indirection, which the compiler regards as a non-constant variable (the contents are set by the linker).