我在D中有一个模板类,它接受另一个模板作为参数,它的开头是这样的:
class RuleVars(alias RuleType, RuleRange, SubstitutionRange)
if (__traits(isTemplate, RuleType)) {
import std.range.primitives;
import std.traits;
import Capsicum.PatternMatching.Engine;
private {
static if (isAssociativeArray!RuleRange) {
alias RuleType = ElementType!(typeof(ruleRange.values));
} else {
alias RuleType = ElementType!RuleRange;
}
static if (isAssociativeArray!SubstitutionRange) {
alias SubstitutionType = ElementType!(typeof(ruleRange.values));
} else {
alias RuleType = ElementType!RuleRange;
}
alias MatchingPolicy = RuleType!(Element, RuleElement);
...需要注意的是,如果RuleType不是模板,类将无法实例化。作为必要,我有一个函数来创建这个类的实例:
RuleVars!(RuleType, RuleRange, SubstitutionRange) CreateRuleVars(alias RuleType, RuleRange, SubstitutionRange)(RuleRange leftHandSide, SubstitutionRange rightHandSide) {
return new RuleVars!(RuleType, RuleRange, SubstitutionRange)(leftHandSide, rightHandSide);
}但是,当我试图像这样设置类时:
CreateRuleVars!UnboundRules([0 : 'a', 2 : 'b', 4 : 'a', 6 : 'b'],
[1 :'a', 3 : 'a', 4 : 'b', 6 : 'b'])我得到以下错误:
source\Capsicum\PatternMatching\RuleSet.d(25,26): Error: template instance RuleType!(Element, RuleElement) RuleType is not a template declaration, it is a alias
source\Capsicum\PatternMatching\RuleSet.d(73,1): Error: template instance Capsicum.PatternMatching.RuleSet.RuleVars!(UnboundRules, char[int], char[int]) error instantiating
source\Capsicum\PatternMatching\RuleSet.d(127,31): instantiated from here: CreateRuleVars!(UnboundRules, char[int], char[int])它在抱怨这条具体的路线:
alias MatchingPolicy = RuleType!(Element, RuleElement);这里传递的特定模板在自己使用和实例化时被证明是有效的,所以它不应该是一个问题。它显然也是一个模板,否则模板参数匹配就会失败。官方D文章显示,模板可以作为模板参数传递,如下所示:
class Foo(T, alias C)
{
C!(T) x;
}据我所知,我做得对。有什么想法吗?
发布于 2018-02-15 22:38:17
问题在于:
alias RuleType = ElementType!(typeof(ruleRange.values));您可以重新定义一个隐藏参数的本地符号RuleType,因此后面的行引用的是该本地别名,而不是要使用的参数。
有趣的是,这可能是编译器中的诊断错误。如果你用普通的平行词做类似的事情:
void main(string[] args) {
int args;
}编译器将标记它:
Error: variable args is shadowing variable aa.sub.main.args因为这几乎肯定是一个错误;您肯定打算使用两个单独的名称,所以仍然可以引用参数(否则只能引用局部变量!)。但是编译器似乎没有对模板参数执行这样的测试。
https://stackoverflow.com/questions/48815199
复制相似问题