Topic: Optimal standard fakery


Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 1999/09/09
Raw View
"Jerry Swan" <bumpkin@beta2sequence.freeserve.co.uk> writes:

> #ifdef COMPILER_HAS_NO_NAMESPACES
>     #define BEGIN_NAMESPACE( NAME )
>     #define END_NAMESPACE
>     #define USING( NAME )
> #else
>     #define BEGIN_NAMESPACE( NAME ) namespace NAME {
>     #define END_NAMESPACE }
>     #define USING( NAME ) using NAME
> #endif

This is quite right; perhaps you need

  #define Q(NAME)

  #define Q(NAME)   NAME::

as well

> #ifndef COMPILER_HAS_NO_ANSI_CASTS
>     #define ANSI_REINTERPRET_CAST( dest, source ) ((dest)(source))
>     // etc
> #endif

Depending on the template support in your compiler, you can do better
than that:

#ifndef COMPILER_HAS_NO_ANSI_CASTS
template<class D, class B>
D static_cast(B p)
{
  return (D)p;
}
#endif

If you use that as in

struct A{
};

struct B:A{
};

int main()
{
  A *a = new B;
  B *b = static_cast<B*>(a);
}

it will work with both an ISO C compiler, and an older one.

Hope this helps,
Martin
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1999/09/09
Raw View
Jerry Swan wrote:
>
> Would anyone care to make a definitive statement on the best that can be
> done to fake namespaces and "new-style" casts in the absence of compiler
> support?

You can cull lots of good (and some bad) portability macros from existing
commercial and freeware libraries. For instance, the SGI STL stuff has a ton
of configurable stuff, based on #defines that are all set in one place.

--

Ciao,                       Paul D. DeRocco
Paul                        mailto:pderocco@ix.netcom.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "John Lund" <jlund@allaire.com>
Date: 1999/09/09
Raw View
Jerry Swan wrote:
>Would anyone care to make a definitive statement on the best that can be
>done to fake namespaces and "new-style" casts in the absence of compiler
>support?

Definitive? Not me, but in addition to the USING macro you cite I
define-away std for compilers that lack namespace support so I can still
qualify names like std::string in my headers.

#if defined(NO_NAMESPACES)
    #define std
#endif

-john, jlund@allaire.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]