typeinfo and exceptions

2005-03-07 Thread Tim Janik
hi all.
i really fail to see what i'm doing wrong in the following code:
struct Exception : std::exception {
  Exception (const char *format, ...) __attribute__((__format__ (__printf__, 2, 
3)));
  [...]
};
struct Obj {
  Obj () { throw Exception ("%s: error", typeid (this).name()); }
};
try { Obj a; } catch (std::exception &e) {
  printf ("exception: what(): %s\n", e.what());
}
it does however produce garbage (with 3.3 and 3.4) instead of Obj's typename:
exception: what(): 44`l$@: error
i have attached a full test case with 2 throw statements that
will both produce a garbage typename. i'm not sure this isn't
a compiler bug, so any help is apprechiated.
---
ciaoTJ#include 
#include 
#include 
#include 
#include 

struct Exception : std::exception {
  Exception (const char *format, ...) __attribute__((__format__ (__printf__, 2, 3)));
  virtual const char* what() const throw() { return reason ? reason : "out of memory"; }
  Exception (const Exception &e) : reason (e.reason ? strdup (e.reason) : NULL) {}
  ~Exception() throw() { if (reason) free (reason); }
private:
  char *reason;
  Exception& operator= (const Exception&);
};

Exception::Exception (const char *format, ...)
{
  va_list args;
  va_start (args, format);
  reason = NULL;
  if (asprintf (&reason, format, args) < 0 || !reason)
reason = NULL; /* indicate "out of memory" */
  va_end (args);
}

struct Obj {
  Obj ()
  {
const char *thisname = typeid (this).name();
printf ("A: thisname: %s\n", thisname);
throw Exception ("%s: error", typeid (this).name());
throw Exception ("%s: error", thisname);
  }
};

int
main (int   argc,
  char *argv[])
{
  try {
Obj a;
  } catch (std::exception &e) {
printf ("exception: what(): %s\n", e.what());
  }
  return 0;
}


aren't specialized templates templates?

2005-02-12 Thread Tim Janik
hi all.
the code snippet below is extracted from a much more
complicated piece of code. basically the problem is that
g++ (3.3 and 3.4) demand different typedef syntax inside
template bodies, depending on whether full or partial
specialization is used.
is this really the correct behaviour and standard conform?
(to me it seems, line20 should still be considered part of
a template and thus accept the "typename")
snip-
template
struct Base {
  typedef C* Iterator;
};
template
struct Derived : Base {
  typedef typename Base::Iterator Iterator;
};
#define BASE_ITER(BASE) typename BASE :: Iterator
template
struct Derived : Base {
  typedef BASE_ITER (Base) Iterator; // line15
};
template<>
struct Derived : Base {
  typedef BASE_ITER (Base) Iterator;   // line20
};
// test.cc:20: error: using `typename' outside of template
snip-
---
ciaoTJ