这是对另一个问题的跟进。它涉及相同的问题(我希望如此),但使用完全不同的示例来说明。原因是在先前的示例中,只有实验性GCC 4.9会出现编译器错误。在这个示例中,Clang和GCC 4.8.1也以不同的方式失败:Clang产生意外结果,而GCC 4.8.1报告了不同的错误消息。
之前问题的答案或多或少表示代码是有效的,问题在于GCC的实验版本。但这个结果让我更加怀疑。几个月来,我一直为我怀疑与之相关(或相同)的问题而困扰,这是我第一次有一个小的具体示例来说明。
所以,这里有一些代码。首先,一些通用代码,它将SFINAE应用于由可变模板别名元函数
第二,一个特定的测试,检查给定的类是否定义了一个名为
最后,举个例子:
预期结果应该是
(行号可能会有所不同)。所以,每件事都会以不同的方式失败。
现在,这里有一个解决方法。元函数
现在所有三个编译器都像预期的那样显示了“0,1”。有趣的是,对于GCC的任何版本,我们都必须删除“has_type”(即使我们不使用它),只保留“has_type2”,否则我们会遇到类似的错误。
总之,我认为问题出在一个模板上,它期望一个形式为可变模板参数的模板。
之前问题的答案或多或少表示代码是有效的,问题在于GCC的实验版本。但这个结果让我更加怀疑。几个月来,我一直为我怀疑与之相关(或相同)的问题而困扰,这是我第一次有一个小的具体示例来说明。
所以,这里有一些代码。首先,一些通用代码,它将SFINAE应用于由可变模板别名元函数
F
指定的任意测试:#include <iostream>
using namespace std;
using _true = integral_constant <bool, true>;
using _false = integral_constant <bool, false>;
template <typename T> using pass = _true;
template <template <typename...> class F>
struct test
{
template <typename... A> static _false _(...);
template <typename... A> static pass <F <A...> > _(int);
};
template <template <typename...> class F, typename... A>
using sfinae = decltype(test <F>::template _<A...>(0));
第二,一个特定的测试,检查给定的类是否定义了一个名为
type
的类型:template <typename T> using type_of = typename T::type;
template <typename T> using has_type = sfinae <type_of, T>;
最后,举个例子:
struct A { using type = double; };
int main()
{
cout << has_type <int>() << ", ";
cout << has_type <A>() << endl;
}
预期结果应该是
0, 1
,Clang显示0, 0
,GCC 4.8.1显示tst.cpp: In substitution of ‘template<class T> using type_of = typename T::type [with T = A ...]’:
tst.cpp:15:51: required from ‘struct test<type_of>’
tst.cpp:19:67: required by substitution of ‘template<template<class ...> class F, class ... A> using sfinae = decltype (test:: _<A ...>(0)) [with F = type_of; A = {T}]’
tst.cpp:24:58: required from here
tst.cpp:23:56: error: ‘A ...’ is not a class, struct, or union type
template <typename T> using type_of = typename T::type;
^
而 GCC 4.9 则表示
tst.cpp:19:67: required by substitution of ‘template<template<class ...> class F, class ... A> using sfinae = decltype (test:: _<A ...>(0)) [with F = type_of; A = {T}]’
tst.cpp:24:58: required from here
tst.cpp:15:51: error: pack expansion argument for non-pack parameter ‘T’ of alias template ‘template<class T> using type_of = typename T::type’
template <typename... A> static pass <F <A...> > _(int);
^
(行号可能会有所不同)。所以,每件事都会以不同的方式失败。
现在,这里有一个解决方法。元函数
car
从给定的包中选择第一个类型,然后将测试重新定义为具有可变参数的type_of2
:template <typename... T> struct car_t;
template <typename... T> using car = type_of <car_t <T...> >;
template <typename T, typename... Tn>
struct car_t <T, Tn...> { using type = T; };
template <typename... T> using type_of2 = typename car <T...>::type;
template <typename T> using has_type2 = sfinae <type_of2, T>;
int main()
{
cout << has_type2 <int>() << ", ";
cout << has_type2 <A>() << endl;
}
现在所有三个编译器都像预期的那样显示了“0,1”。有趣的是,对于GCC的任何版本,我们都必须删除“has_type”(即使我们不使用它),只保留“has_type2”,否则我们会遇到类似的错误。
总之,我认为问题出在一个模板上,它期望一个形式为可变模板参数的模板。
template <typename...> class F
我们实际上将一个非变参模板别名作为输入,形式如下:
template <typename T> using alias = // ... anything including T or not
最后,按照可变参数的方式调用F
:
F <A...>
到目前为止,意见认为这是有效的,但现在似乎有三个编译器不同意。所以问题再次出现:这是否有效?
对我来说很重要,因为我有几十个文件的现有代码,基于这个假设是有效的,现在无论如何都需要重新设计(由于这些编译器存在实际问题),但确切的重新设计取决于答案。