我有以下代码(很抱歉代码块很大,但我无法再缩小范围)
template <bool B>
struct enable_if_c {
typedef void type;
};
template <>
struct enable_if_c<false> {};
template <class Cond>
struct enable_if : public enable_if_c<Cond::value> {};
template <typename X>
struct Base { enum { value = 1 }; };
template <typename X, typename Y=Base<X>, typename Z=void>
struct Foo;
template <typename X>
struct Foo<X, Base<X>, void> { enum { value = 0 }; };
template <typename X, typename Y>
struct Foo<X, Y, typename enable_if<Y>::type > { enum { value = 1 }; };
int main(int, char**) {
Foo<int> foo;
}
但是它无法用 gcc (v4.3) 进行编译,并且
foo.cc: In function ‘int main(int, char**)’:
foo.cc:33: error: ambiguous class template instantiation for ‘struct Foo<int, Base<int>, void>’
foo.cc:24: error: candidates are: struct Foo<X, Base<X>, void>
foo.cc:27: error: struct Foo<X, Y, typename enable_if<Y>::type>
foo.cc:33: error: aggregate ‘Foo<int, Base<int>, void> foo’ has incomplete type and cannot be defined
OK,所以它是不明确的。 但我没想到这会成为一个问题,因为在使用专业化时它几乎总是会出现一些歧义。 但是,仅当使用带有 enable_if<...>
的类时才会触发此错误,如果我将其替换为如下所示的类,则没有问题。
template <typename X, typename Y>
struct Foo<X, Y, void > { enum { value = 2 }; };
为什么这个类不会引起歧义,而其他类却会引起歧义? 对于具有 true ::value 的类来说,这两者不是同一件事吗?
不管怎样,任何关于我做错了什么的暗示都会受到赞赏。
感谢您的回答,我真正的问题(让编译器选择我的第一个专业化)是通过将 struct Foo, void> 替换为struct Foo, 类型名enable_if< 碱基 >::type >
似乎按我想要的方式工作。
I have the following code (sorry for the large code chunk, but I could not narrow it down any more)
template <bool B>
struct enable_if_c {
typedef void type;
};
template <>
struct enable_if_c<false> {};
template <class Cond>
struct enable_if : public enable_if_c<Cond::value> {};
template <typename X>
struct Base { enum { value = 1 }; };
template <typename X, typename Y=Base<X>, typename Z=void>
struct Foo;
template <typename X>
struct Foo<X, Base<X>, void> { enum { value = 0 }; };
template <typename X, typename Y>
struct Foo<X, Y, typename enable_if<Y>::type > { enum { value = 1 }; };
int main(int, char**) {
Foo<int> foo;
}
But it fails to compile with gcc (v4.3) with
foo.cc: In function ‘int main(int, char**)’:
foo.cc:33: error: ambiguous class template instantiation for ‘struct Foo<int, Base<int>, void>’
foo.cc:24: error: candidates are: struct Foo<X, Base<X>, void>
foo.cc:27: error: struct Foo<X, Y, typename enable_if<Y>::type>
foo.cc:33: error: aggregate ‘Foo<int, Base<int>, void> foo’ has incomplete type and cannot be defined
OK, so it's ambiguous. but I wasn't expecting it to be a problem as when using specialization it will almost always be some ambiguity. However this error is only triggered when using the class with enable_if<...>
, if I replace it with a class like the following there is no problem.
template <typename X, typename Y>
struct Foo<X, Y, void > { enum { value = 2 }; };
Why does this class not cause an ambiguity while the others do? Isn't the two the same thing for classes with a true ::value?
Anyway, any hints as to what I am doing wrong are appreciated.
Thanks for the answers, my real problem (to get the compiler to select my first specialization) was solved by replacing struct Foo<X, Base<X>, void>
with struct Foo<X, Base<X>, typename enable_if< Base<X> >::type >
which seems to work the way I want.
发布评论
评论(3)
你的问题的要点是你有:
并且你试图将其匹配
显然,两个专业匹配(第一个与
X = int
,第二个与X = int,Y = 基
)。根据该标准第 14.5.4 节,如果有更多匹配的专业化,则在它们之间构建偏序(如 14.5.5.2 中定义)并使用最专业的一个。 然而,就您而言,两者都不比另一个更专业。 (简单地说,如果您可以用某种类型替换后一个模板的每个类型参数,并最终获得前一个模板的签名,那么一个模板比另一个模板更专业。另外,如果您有
whatever:: type
并将Y
替换为Base
你会得到whatever >::type
而不是 < code>void,即不执行处理。)如果将
#2
替换为,则候选集再次包含两个模板,但是,#1 比 #3 更专业,并且如下这样就被选中了。
The gist of your question is that you have:
and you're trying to match it to
Obviously, both specializations match (the first with
X = int
, the second withX = int, Y = Base<int>
).According to the standard, section 14.5.4, if there are more matching specializations, a partial ordering (as defined in 14.5.5.2) among them is constructed and the most specialized one is used. In your case, however, neither one is more specialized than the other. (Simply put, a template is more specialized than another, if you can replace each type parameter of the latter template with some type and in result get the signature of the former. Also, if you have
whatever<Y>::type
and you replaceY
withBase<X>
you getwhatever<Base<X> >::type
notvoid
, i.e. there is not processing performed.)If you replace
#2
withthen the candidate set again contains both templates, however, #1 is more specialized then #3 and as such is selected.
你是不是少了一个
符号?
aren't you missing a
symbol ?
我认为您缺少“<”,模板应如下所示:
I think you are missing a '<', a template should look like: