Bug 3750 – Template specialization based on shared doesn't seem to work

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2010-01-28T20:00:51Z
Last change time
2019-08-18T23:03:19Z
Assigned to
No Owner
Creator
Jason House

Comments

Comment #0 by jason.james.house — 2010-01-28T20:00:51Z
Example #1: template foo( T U == shared ){ enum U foo = 1; } static assert(is(foo!(shared int) == int)); ------------------------------------------------ error.d(1): found '==' when expecting ')' error.d(1): members of template declaration expected error.d(1): Declaration expected, not ')' error.d(1): unrecognized declaration Example #2: template foo( T ) if (is(T U == shared)){ enum U foo = 1; } static assert(is(foo!(shared int) == int)); ------------------------------------------------- alt.d(2): Error: static assert (is(foo!(shared(int)) == int)) is false Example #3: template foo( T ) if (is(T U == shared)){ enum U foo = 1; } pgragma(msg, foo!(shared int)); -------------------------------------------------- alt.d(1): Error: identifier 'U' is not defined alt.d(1): Error: U is used as a type alt.d(1): Error: variable alt.foo!(shared(int)).foo voids have no value alt.d(2): Error: template instance alt.foo!(shared(int)) error instantiating 1 It seems like is expressions work alright: void main(){ shared int x; static if (is(x y == shared)) { static assert( is (y == int) ); static assert( !is (y == shared) ); } } ... although this example (incomplete) fails: template isValidNumericType( T ) { static if (is(T U == shared)){ enum bool isValidNumericType = isIntegerType!( U ) || isPointerType!( U ); } else { enum bool isValidNumericType = isIntegerType!( T ) || isPointerType!( T ); } } ----------------------------------------- tango/core/Atomic.d(828): Error: static assert (isValidNumericType!(shared(int))) is false
Comment #1 by ag0aep6g — 2019-08-18T23:03:19Z
(In reply to Jason House from comment #0) > Example #1: > template foo( T U == shared ){ enum U foo = 1; } > static assert(is(foo!(shared int) == int)); This example is invalid. Template specialization syntax doesn't use `==`. It uses `:`. And there is no form with two identifiers on the left hand side. That's `is` expression syntax. > Example #2: > template foo( T ) if (is(T U == shared)){ enum U foo = 1; } > static assert(is(foo!(shared int) == int)); [...] > Example #3: > template foo( T ) if (is(T U == shared)){ enum U foo = 1; } > pgragma(msg, foo!(shared int)); Ignoring minor mistakes, these examples show that `is` expressions in template constraints don't propagate their newly defined aliases into the template body. Issue 6269 is dedicatead to that. > ... although this example (incomplete) fails: > template isValidNumericType( T ) > { > static if (is(T U == shared)){ > enum bool isValidNumericType = isIntegerType!( U ) || > isPointerType!( U ); > } > else > { > enum bool isValidNumericType = isIntegerType!( T ) || > isPointerType!( T ); > } > } > ----------------------------------------- > tango/core/Atomic.d(828): Error: static assert > (isValidNumericType!(shared(int))) is false Works for me when isIntegerType and isPointerType are defined as follows: template isIntegerType(T) { enum isIntegerType = is(T : ulong); } template isPointerType(T) { enum isPointerType = is(T : void*); } Examples #2 and #3 seem to be the only valid ones, and the underlying issue is better described in issue 6269. So I'm closing this as a duplicate. *** This issue has been marked as a duplicate of issue 6269 ***