Issue 3750 - Template specialization based on shared doesn't seem to work
Summary: Template specialization based on shared doesn't seem to work
Status: RESOLVED DUPLICATE of issue 6269
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-01-28 20:00 UTC by Jason House
Modified: 2019-08-18 23:03 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jason House 2010-01-28 20:00:51 UTC
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 ag0aep6g 2019-08-18 23:03:19 UTC
(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 ***