May 11, 2011
On 05/11/2011 12:07 PM, Alexander wrote:
> On 11.05.2011 02:52, Andrei Alexandrescu wrote:
>
>> Note that the macro version has other, arguably larger, drawbacks - e.g. it can't be used as an expression, evaluates the limit multiple times. pollutes the global namespace etc.
>
>    BTW, I don't get it - why *scoped* variable is going to *global* namespace when declared "static"?
>
>    This, IMHO, defeats the value and purpose of scoping...
>
> /Alexander

The macro name itself is polluting the global namespace.

Andrei
May 11, 2011
On 11.05.2011 20:07, Andrei Alexandrescu wrote:

> The macro name itself is polluting the global namespace.

OK, this I understand. But I cannot have something like:

  {
	static int i = 0;
  }
  {
	static int i = 1;
  }

This produces linker warning:

io.o:(.tdata.+0x0): multiple definition of `void io.main(immutable(char)[][]).int i'

Sure, this defined in function scope, not globally, but anyway...

/Alexander
May 11, 2011
On 2011-05-11 03:59, Don wrote:
> Jens Mueller wrote:
> > Jonathan M Davis wrote:
> >> On 2011-05-10 18:06, Jose Armando Garcia wrote:
> >>> Thanks. I should have read http://www.digitalmars.com/d/2.0/template.html more carefully.
> >>> 
> >>> "Multiple instantiations of a TemplateDeclaration with the same TemplateArgumentList, before implicit conversions, all will refer to the same instantiation."
> >>> 
> >>> The default values which are evaluated at the call site are part of the TemplateArgumentList.
> >> 
> >> Yes, default arguments are evaluated at the call site, not where the code is declared. I believe that C++ has the opposite behavior, so it's not uncommon that people miss that. It's _very_ useful though - particularly when dealing with __FILE__ and __LINE__.
> > 
> > I did some little testing (see attached file). The bar() example shows
> > that default arguments are evaluated at call site (even in C++). If it
> > was only evaluated when declared it would always return 1.
> > But __LINE__ does not work as in D. I believe that is due to __LINE__
> > being a macro in C whereas in D it is evaluated at instantiation time
> > (i.e. it is not text replacement). So it seems that instantiation of
> > default arguments is the same in D and C++. The difference is that
> > __LINE__ and __FILE__ are different things in C++ and D. In C++ there is
> > no such thing as evaluating __LINE__. Or let's say they are constant
> > expressions (due to textual replacement) where in D they are evaluated
> > differently depending on the context.
> > Does this make sense?
> 
> __LINE__ and __FILE__ are magical when used as default arguments in D, for exactly this use case, since it is *extremely* useful. In every other case they behave as in C++. (IIRC the magic is not even implemented for default arguments of __LINE__ + 0, only for __LINE__).

Hmm. Good to know. Well regardless of why and how it works with __FILE__ and __LINE__, it's definitely fantastic that they do. It is indeed _extremely_ useful.

- Jonathan M Davis
1 2
Next ›   Last »