Capturing caller's file/line number in variadic template functions

Mantis mail.mantis.88 at gmail.com
Fri Mar 16 11:26:30 PDT 2012


16.03.2012 20:23, H. S. Teoh пишет:
> I'm writing some unittests with very repetitive tests for a myriad of
> different types, so I wrote a helper function:
>
> 	version(unittest) {
> 		void checkConsistency(T...)(T args) {
> 			foreach (a; args) {
> 				assert(isConsistent(a));
> 			}
> 		}
> 	}
> 	unittest {
> 		A a;
> 		B b;
> 		C c;
> 		checkConsistency(a,b,c);
> 	}
>
> However, when a consistency check fails, the assert error points to
> checkConsistency instead of the unittest, so it's a pain trying to
> figure out exactly which test case failed. I tried adding default
> arguments to checkConsistency:
>
> 	void checkConsistency(T...)(T args, string file=__FILE__,
> 		size_t line=__LINE__) { ... }
>
> but this causes compile errors because when C==string, then the call is
> ambiguous.
>
> Is there an easy of working around this?
>
>
> T
>
What happens if you insert some dummy type ?:

void checkConsistency(T...)(T args, Delimiter dm = Delimiter.init,
   string file=__FILE__, size_t line=__LINE__) { ... }




More information about the Digitalmars-d-learn mailing list