Topic: Default Arg Instantiation


Author: richards_corden@hotmail.com (Richard Corden)
Date: Wed, 7 May 2003 09:33:08 +0000 (UTC)
Raw View
Hi,

Is there something else that should happen for default arguments which
doesn't happen for ordinary expressions being instantiated?

i.e. is 14.7.1/11 basically saying that a default argument gets
instantiated only if necessary?

For example: should the following code compile?

<CODE>
  template<typename T>
  void f(T a = A<T>::a);

  template <typename T>
  struct A
  {
    static const int a = 10;
  };

  void g()
  {
    f<int> ();
  }
</CODE>


Thanks for your time,

Richard

--
Richard Corden
To reply remove 's' from address

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: johnchx2@yahoo.com (johnchx)
Date: Wed, 7 May 2003 17:54:32 +0000 (UTC)
Raw View
richards_corden@hotmail.com (Richard Corden) wrote

> i.e. is 14.7.1/11 basically saying that a default argument gets
> instantiated only if necessary?
>
> For example: should the following code compile?
>
> <CODE>
>   template<typename T>
>   void f(T a = A<T>::a);
>
>   template <typename T>
>   struct A
>   {
>     static const int a = 10;
>   };
>
>   void g()
>   {
>     f<int> ();
>   }
> </CODE>

I don't think so.  The problem is that even though A<T> won't be
instantiated until it is used, it still needs to be parsed as part of
the function declaration.

A forward declaration of A, like the following, should be present
before the declaration of f()

  template <typename T> struct A;

Some compilers (e.g. Borland) will accept the code as written, without
the forward declaration of A.  AFAICT, this is because they don't yet
implement two-phase name lookup correctly (so they don't try to
resolve *any* names referred to in a template at the point of
definition).

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]