【发布时间】:2018-05-10 18:59:30
【问题描述】:
有人可以解释这里的歧义吗?
template <typename...> struct thing;
template <typename... Rest>
struct thing<int&, Rest&...> {
thing(int&, Rest&...) { }
};
template <typename First, typename... Rest>
struct thing<First&, Rest&...> {
thing(First&, Rest&...) { }
};
int main() {
int myint;
char mychar;
thing<int&, char&> t(myint, mychar);
}
【问题讨论】:
-
嗯,你指的是哪个?需要 1 + 其他 args 的那个,还是需要 2 + 其他 args 的那个?
-
第一个专业不是比第二个专业吗?它应该自动取第一个,对吧?
-
这可能是编译器错误。
-
您可以使用
template <typename ... Ts> struct thing<Ts&...> : thing_for_ref<Ts...> {}(然后专门化thing_for_ref)作为解决方法。 -
使用 SFINAE 的工作解决方法:wandbox.org/permlink/cPGy0K4I5BR2EWIm
标签: c++ c++11 templates ambiguity