[英]Can I pass a reference type to a template to specify following non-type template parameters' types?
Consider an example: 考虑一个例子:
#include <type_traits>
template <class T, T>
struct has_duplicates_info { };
template <class T, T...>
struct has_duplicates;
template <class T, T First, T... Others>
struct has_duplicates<T, First, Others...>:
has_duplicates<T, Others...>,
has_duplicates_info<T, First> {
static constexpr bool value =
std::is_base_of<has_duplicates_info<T, First>, has_duplicates<T, Others...>>::value
|| has_duplicates<T, Others...>::value;
};
template <class T, T Last>
struct has_duplicates<T, Last>: has_duplicates_info<T, Last>, std::false_type { };
int a, b;
int main() {
static_assert(!has_duplicates<int, 0, 1, 2>::value, "has_duplicates<int, 0, 1, 2>::value");
static_assert(has_duplicates<int, 1, 2, 2, 3>::value, "!has_duplicates<int, 1, 2, 2, 3>::value");
static_assert(has_duplicates<int&, a, a, b>::value, "!has_duplicates<int&, a, a, b>::value");
}
This compiles fine with clang but not with gcc. 这可以用clang编译,但是不能用gcc编译。 The problem is in a line: 问题在一行:
static_assert(has_duplicates<int&, a, a, b>::value, "has_duplicates<int&, a, a, b>::value");
where compiler suggests that has_duplicates<int&, a, a, b>
is an incomplete type: 编译器建议has_duplicates<int&, a, a, b>
是不完整的类型:
has_duplicates.cc:26:18: error: incomplete type 'has_duplicates<int&, a, a, b>' used in nested name specifier static_assert(has_duplicates<int&, a, a, b>::value, "has_duplicates<int&, a, a, b>::value"); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
So... which compiler is right? 那么...哪个编译器是正确的?
Edit: 编辑:
To clarify I am not trying to check if the runtime values behind variables passed to has_duplicates
contains duplicates only if there are duplicated references passed to this trait... Eg the code below compiles successfully in both gcc and clang: 为了澄清has_duplicates
我并不是要检查传递给has_duplicates
变量后面的运行has_duplicates
是否仅包含重复项, has_duplicates
是存在传递给该特征的重复引用...例如,下面的代码在gcc和clang中都可以成功编译:
template <int &X>
struct a { };
int b;
int main() {
a<b> c;
}
First off, it's definitely a bug in gcc. 首先,这绝对是gcc中的错误。 You're nearly correct in your diagnosis of the nature of the bug, but it's not that gcc doesn't accept reference-type non-type template parameters, it's rather that gcc fails to recognise reference-type non-type template parameters as a class template partial specialization where the reference type is a previous template parameter: 您对错误本质的诊断几乎是正确的,但这并不是gcc不接受引用类型的非类型模板参数,而是gcc无法将引用类型的非类型模板参数识别为类模板局部特化,其中引用类型是先前的模板参数:
template<int, class T, T> struct S; // #1
template<class T, T A> struct S<0, T, A> {}; // #2
int i;
S<0, int&, i> s; // #3 error: aggregate 'S<0, int&, i> s' has incomplete type
#2
is a perfectly legitimate partial specialization of #1
and should be matched by the instantiation #3
, per [temp.class.spec.match] and [temp.deduct]. #2
是#1
的完全合法的部分专业化,应根据[temp.class.spec.match]和[temp.deduct]与实例#3
匹配。
Fortunately, there's a simple workaround, which is to provide a further partial specialization for reference types: 幸运的是,有一个简单的解决方法,可以为引用类型提供进一步的部分专业化:
template<class R, R& A> struct S<0, R&, A> {};
A correct compiler like clang will also be fine with this. 像clang这样的正确编译器也可以。
In your case the further partial specializations would be: 在您的情况下,进一步的部分专业化将是:
template <class R, R& First, R&... Others>
struct has_duplicates<R&, First, Others...> // ...
template <class R, R& Last>
struct has_duplicates<R&, Last> // ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.