模棱两可的模板怪异

发布于 2024-07-27 18:53:48 字数 1841 浏览 4 评论 0 原文

我有以下代码(很抱歉代码块很大,但我无法再缩小范围)

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只为守护你 2024-08-03 18:53:48

你的问题的要点是你有:

template <typename X, typename Y, typename Z>
struct Foo {};

template <typename X>
struct Foo<X, Base<X>, void> {};                   // #1

template <typename X, typename Y>
struct Foo<X, Y, typename whatever<Y>::type> {};   // #2

并且你试图将其匹配

Foo<int, Base<int>, void>

显然,两个专业匹配(第一个与X = int,第二个与X = int,Y = 基)。

根据该标准第 14.5.4 节,如果有更多匹配的专业化,则在它们之间构建偏序(如 14.5.5.2 中定义)并使用最专业的一个。 然而,就您而言,两者都不比另一个更专业。 (简单地说,如果您可以用某种类型替换后一个模板的每个类型参数,并最终获得前一个模板的签名,那么一个模板比另一个模板更专业。另外,如果您有 whatever:: type 并将 Y 替换为 Base 你会得到 whatever >::type 而不是 < code>void,即不执行处理。)

如果将 #2 替换为,

template <typename X, typename Y>
struct Foo<X, Y, void > {};                        // #3

则候选集再次包含两个模板,但是,#1 比 #3 更专业,并且如下这样就被选中了。

The gist of your question is that you have:

template <typename X, typename Y, typename Z>
struct Foo {};

template <typename X>
struct Foo<X, Base<X>, void> {};                   // #1

template <typename X, typename Y>
struct Foo<X, Y, typename whatever<Y>::type> {};   // #2

and you're trying to match it to

Foo<int, Base<int>, void>

Obviously, both specializations match (the first with X = int, the second with X = 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 replace Y with Base<X> you get whatever<Base<X> >::type not void, i.e. there is not processing performed.)

If you replace #2 with

template <typename X, typename Y>
struct Foo<X, Y, void > {};                        // #3

then the candidate set again contains both templates, however, #1 is more specialized then #3 and as such is selected.

请别遗忘我 2024-08-03 18:53:48

你是不是少了一个

<

符号?

aren't you missing a

<

symbol ?

梦屿孤独相伴 2024-08-03 18:53:48

我认为您缺少“<”,模板应如下所示:

template< typename T >
struct myStruct
{};

//OR

template< class T >
struct myStruct
{};

I think you are missing a '<', a template should look like:

template< typename T >
struct myStruct
{};

//OR

template< class T >
struct myStruct
{};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文