string file = __FILE__ considered harmful (and solution)
Walter Bright
newshound2 at digitalmars.com
Fri Jun 1 03:22:44 UTC 2018
On 5/30/2018 1:27 AM, FeepingCreature wrote:
> There's a very common idiom where in order to report line numbers of an error or
> a log line at the callsite of a function, you pass __FILE__ and __LINE__ as
> default parameters:
>
> void foo(string file = __FILE__, size_t line = __LINE__);
>
> What's wrong with this?
>
> Say you add a string parameter, such as
>
> void foo(string msg, string file = __FILE__, size_t line = __LINE__);
>
> foo("Hello World");
>
> Now when you accidentally grab an old version of the library, your new code will
> still run, but it will believe that it's being called from file "Hello World",
> line 15.
A solution:
enum E { reserved }
void foo(E e = E.reserved, string file = __FILE__, size_t line = __LINE__);
void foo(string msg, E e = E.reserved, string file = __FILE__, size_t line =
__LINE__);
More information about the Digitalmars-d
mailing list