https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93739

            Bug ID: 93739
           Summary: Ability to print a type name without aborting
                    compilation
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: eyalroz at technion dot ac.il
  Target Milestone: ---

Over the past several years, C++ has seen increased use of type deduction via
auto variables (C++11), auto return type (C++14), template deduction guides and
more. The language already had delicate rules regarding decays, references
being added or removed, etc. All of these motivates the developer to sometimes
want to ascertain what the type of an expression or the value of a template
parameter is, in their program, while it is being compiled - with the
information printed to the standard error stream like warnings and errors are.
(And nota bene: Not at run-time).

This is partly doable today already, e.g. with the kludge in the following
example:

  using mystery  = int;

  // ... etc etc

  template<typename T> struct has_type{};
  using foo = typename has_type<mystery>::mystery;

this results in an error whose first line is:

<source>:6:59: error: 'mystery' in 'struct has_type<int>' does not name a type

and tells us the type of mystery is int. This has several drawbacks:

* Abuse of a mechanism with a different intent (although in C++ that is
sometimes considered a good idea...)
* Irrelevant clutter in the output - you need to pay attention and know what
you're looking for.
* [MOST IMPORTANT] Compilation stops when hitting this type check.

(consequences of the above)
* Can't check more than one type at a time this way.
* Can't be used in compilation log parsing - must be introduced and then
removed.
* Effectively unusable if the same expression has a different type when called
from different locations - i.e. within templates: Compilation will stop with
the first instantiation, not the one you want (and preventing this stoppage is
overkill).

I therefore ask that a feature be added to GCC, of allowing the printing of a
type's name at some appropriate point during compilation. I'm not familiar with
the various passes and stages of parsing and comprehending C++ programs in GCC,
but obviously the type is deduced at some point - the same point where the
error in the above example can be printed. Instead, I suggest for some pragma
(new or existing one) to be able to print type names.

Syntax ideas (a bike-shed issue of course):

#pragma print_type_of( mystery )
#pragma message( typeof(mystery) )

Reply via email to