我可以将引用类型传递给模板以指定以下非类型模板参数的类型吗?
Can I pass a reference type to a template to specify following non-type template parameters' types?
考虑一个例子:
#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");
}
这可以用 clang 正常编译,但不能用 gcc 编译。问题在一行:
static_assert(has_duplicates<int&, a, a, b>::value, "has_duplicates<int&, a, a, b>::value");
其中编译器提示 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");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
那么...哪个编译器是正确的?
编辑:
澄清一下,我 不是 试图检查传递给 has_duplicates
的变量背后的运行时值是否仅在有重复引用传递给此特征时才包含重复项。 . 例如下面的代码在 gcc 和 clang 中都能成功编译:
template <int &X>
struct a { };
int b;
int main() {
a<b> c;
}
首先,这绝对是 gcc 中的错误。您对错误性质的诊断几乎是正确的,但这并不是 gcc 不接受引用类型的非类型模板参数,而是 gcc 未能 recognise引用类型非类型模板参数作为 class 模板偏特化,其中引用类型是先前的模板参数:
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
是 #1
的完全合法的部分特化,应根据 [temp.class.spec.match] 和 [temp.deduct 与实例化 #3
匹配].
幸运的是,有一个简单的解决方法,即为引用类型提供进一步的部分特化:
template<class R, R& A> struct S<0, R&, A> {};
像 clang 这样的正确 编译器也可以。
在您的情况下,进一步的部分专业化将是:
template <class R, R& First, R&... Others>
struct has_duplicates<R&, First, Others...> // ...
template <class R, R& Last>
struct has_duplicates<R&, Last> // ...
考虑一个例子:
#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");
}
这可以用 clang 正常编译,但不能用 gcc 编译。问题在一行:
static_assert(has_duplicates<int&, a, a, b>::value, "has_duplicates<int&, a, a, b>::value");
其中编译器提示 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"); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
那么...哪个编译器是正确的?
编辑:
澄清一下,我 不是 试图检查传递给 has_duplicates
的变量背后的运行时值是否仅在有重复引用传递给此特征时才包含重复项。 . 例如下面的代码在 gcc 和 clang 中都能成功编译:
template <int &X>
struct a { };
int b;
int main() {
a<b> c;
}
首先,这绝对是 gcc 中的错误。您对错误性质的诊断几乎是正确的,但这并不是 gcc 不接受引用类型的非类型模板参数,而是 gcc 未能 recognise引用类型非类型模板参数作为 class 模板偏特化,其中引用类型是先前的模板参数:
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
是 #1
的完全合法的部分特化,应根据 [temp.class.spec.match] 和 [temp.deduct 与实例化 #3
匹配].
幸运的是,有一个简单的解决方法,即为引用类型提供进一步的部分特化:
template<class R, R& A> struct S<0, R&, A> {};
像 clang 这样的正确 编译器也可以。
在您的情况下,进一步的部分专业化将是:
template <class R, R& First, R&... Others>
struct has_duplicates<R&, First, Others...> // ...
template <class R, R& Last>
struct has_duplicates<R&, Last> // ...