C++0x Constructor Delegation

Andrew Pinski pinskia@gmail.com
Sat Mar 3 19:27:00 GMT 2007


On 3/3/07, Pedro Lamarão <pedro.lamarao@mndfck.org> wrote:
> This patch implements the "Delegating Constructor" feature in C++0x.
> mode. The feature is included in the current working draft.
>
> With constructor delegation one is allowed to try code such as:
>
> struct A {
>
>      int handle;
>
>      A (int i) : handle(i) { }
>
>      A () : A(-1) { } // Delegate construction.
>
> };
>
> Tested on i686-pc-linux-gnu with no regressions.
>
>
> 2007-03-03  Pedro Lamarão  <pedro.lamarao@mndfck.org>
>
>         * init.c(expand_member_init): accept constructor delegation
>         in C++0x mode.
>         (emit_mem_initializers): check for a target constructor
>         and delegate.
>         (perform_target_ctor): new function.

I think emit_mem_initializers check could be improved, and only have a
loop in the case of the error and also only in C++0x mode because it
might slow down the compiler too much.
Something like:
#define CTOR_DELAGE_P(type)   (TYPE_P (TREE_PURPOSE (init)) \
	   || TREE_CODE (TREE_PURPOSE (init)) == TYPE_DECL) \
	  && same_type_p(TREE_PURPOSE (init), current_class_type))

if (flag_cpp0x)
{
      if (!CTOR_DELAGE_P (TREE_PURPOSE (mem_inits))
          || TREE_CHAIN (mem_inits))
      {
         for (init = mem_inits; init; init = TREE_CHAIN (init))
         {
            if (CTOR_DELAGE_P (TREE_PURPOSE (mem_inits)))
            {
               error ("target constructor must be the only initializer");
               return;
            }
         }
      }
      else
      {
	  perform_target_ctor (TREE_VALUE (mem_inits));
	  return;
     }
}

Note I wrote this in the email editor so I could have mistakes,

Even though the common case in C++0x will be slower, the C++98/03 case
is not going to be much slower.

Thanks,
Andrew Pinski


More information about the Gcc-patches mailing list