www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Templated class requires values even though having default value

reply tcak <tcak gmail.com> writes:
	public class OpenCLKernel(size_t dim = 1) if( (dim >= 1) && (dim 
<= 3) )
	{
		public this( OpenCLProgram program, in string kernelName )
		{
			// ...
		}
	}

I have a class definition as above. When I want to create an 
instance as below,

	OpenCLKernel k = new OpenCLKernel( program, "kernelName" );

compiler tells me,

	/test2.d(168): Error: template class `OpenCLKernel(ulong dim = 
1) if (dim >= 1 && (dim <= 3))` is used as a type without 
instantiation; to instantiate it use `OpenCLKernel!(arguments)`


The "dim" template parameter has a default value of 1 already. 
Why does it still force me to give a value?

(DMD64 D Compiler v2.096.0)
May 09 2021
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 9 May 2021 at 10:53:49 UTC, tcak wrote:
 The "dim" template parameter has a default value of 1 already. 
 Why does it still force me to give a value?
It doesn't, but it does require you to instantiate the template. You can do `OpenClKernel!()` to use the default value. But without that ! instantation, you're just referring to a template name, not a class. You might want to try something like class GenericOpenClKernel(....) {} alias OpenClKernel = GenericOpenClKernel!(); depending on the intended uses.
May 09 2021